As the game’s primary inspiration in terms of gameplay was Peggle, it seemed somewhat important to start with the physics system for the game, creating the gravity, collisions, and bounce of the spit against the obstacles along the way. The best way I know to do this is to create a physics class and have the game objects have it as a member, the physics class would include members such as acceleration, velocity, bounciness, and mass. Its functions would be primarily what happens when it is updated, which would have a reference to the object’s position, allowing it to be changed by the velocity, what happens when it collides with something, which would include the object with which it collided and a 2D vector of the point of contact, allowing the object to determine which direction to bounce where applicable, and a function to add force to the object, which would take a 2-dimensional vector as a direction and a float as a force.

In order to determine which objects are colliding with one another they need to have collision areas, which for this game will all be circles or rectangles. As the only object which is likely to need checking in this game is the spit, I decided to make a method for checking circle against circle collisions, and circle against rectangle collisions. A circle-on-circle collision is a relatively easy check, it’s simply a case of getting the Euclidian distance between the two objects (using a2 + b2 = c2) and checking whether it was less than the sum of both radii. To check a circle-on-rectangle collision was a little trickier, but ultimately I settled on checking the centre point of each edge of the rectangle.

  • If the edge was a horizontal line I would check its Y position against that of the centre of the circle
  • If the edge was a vertical line I would check its X position against that of the centre of the circle
  • If all distances were greater than that of the circle’s radius, there would not be a collision and no further checks were needed
  • If any were within the circle’s radius, a check would be made on the points along the edge to check if they were within the circle’s radius, and if so a collision would occur