It’s Working!

I fixed the collision detection (yaayyy)!! How I fixed it: I reduced the time step of each frame in my game loop, and I fixed the order of the game loop. If that sounds like gibberish now, hopefully it won’t by the time you reach the end of this article.

First, let’s talk about the game loop. In short, the game loop is a repeated sequence of steps the game program follows. For Falldown Rebirth, the game loop is something like:

  1. Process input
  2. Move the Falldown platforms
  3. Move the ball
  4. Check for collision
  5. Process the collision
  6. Draw the frame

Step 1 takes the controller input (right now, the “controller” means the keyboard). Step 2 moves the platforms up the screen. Step 3 accelerates the ball based on the force of gravity in the game, as well as the controller input from Step 1. Step 4 then checks to see if the ball is touching/overlapping a platform. If so, then Step 5 “processes the collision.”

In this game, “collision processing” is as simple as tweaking the position of the ball so it does not overlap with whatever platform it is touching. To keep things simple (and also arcade game-like), this game does not calculate momentum. As a result, there is no bouncing or anything that resembles a realistic physics simulation (except for the depiction of the ball rolling). In a more complicated game, collision processing might include figuring out how hard the ball bounces off of a surface, or some other response.

Once all that is done, the program draws everything to the screen, and then runs the loop again.

So where does the aforementioned “time step” come into play? The time step determines how far the ball and the platforms move during steps 2 and 3. The smaller the time step is, the less movement there is in each frame, and the more accurate the collision detection is. There is more to it than just that, but for now, reducing the time step fixed the problems I had. So now, the game looks like this:

(Side note: I know the video looks choppy. It’s the best I can do right now; I can’t find a screen recorder that captures smooth videos of my Pygame windows.)

Hopefully, this brief overview explains what I’ve been up to. In future revisions of the game, I want to implement more robust collision detection to handle a fast-moving ball and fast-moving platforms.

Stay tuned!

Also, if you’re interested, the code for the game (well, at the time of this writing, most of the code for the game) is up on my GitHub page.

Leave a Reply