I started CS50's Introduction to Game Development and the first game to make is Pong. I've made a Pong game in the past so I decided to be a little ambitious and make Air Hockey. I figured it's basically the same thing, right? There are a few differences to account for but how hard could it be? 😐
After I set up the players and puck and added some collision boundaries I started to realize this wasn't going to be as easy as I initially thought.
With Pong you can make the CPU paddle follow the ball by adjusting its
y value based on where the ball is.
If the ball is moving in the positive
x direction, check the ball's
y value against the middle of the CPU's paddle. If the ball is lower than the middle of the CPU, move the CPU paddle down in the positive
y direction. If the ball is higher than the middle of the CPU, move the CPU up in the negative
With Air Hockey, both players are able to move along the
y axis. For now I decided not to worry about that and start simple by just making it 2 human players.
I was able to keep the players within bounds and give them a slight bounce when bumping the walls by setting their
y value to whatever boundary was bumped, and then adding or substracting acceleration to the velocity in
if self.x < left_boundary then self.x = left_boundary self.vx += acceleration self.vx = mid(-max_bounce, self.vx, max_bounce) self.x += self.vx end
However I ran into trouble when it came to adding puck/player collision. This is the part I didn't really consider when I underestimated the difficulty of making Air Hockey vs Pong.
I figured if the puck is stationary and it collides with the player, it should move in the direction the player was moving: make the puck's velocity equal to the player's velocity. That sort of worked but I think there's more to the physics that I'm unsure of.
The problem then became the moving puck's collision with the other player. I thought it would be similar to bouncing off the walls: just reflect the puck in the opposite direction of the collision buuuut that didn't go as planned. I think my next step will be doing some reading on physics/trigonometry. 😬
I have a couple of questions for anyone reading this that might have an answer:
- Do I need to consider which side of the player is colliding with the puck in order to reflect the puck properly?
- If I loop through the players and check for collisions each frame, player one is always going to be checked first. This seems like it creates an unfair advantage. Am I missing something? When I look it up I can only find information regarding online multiplayer and latency.
This is a WIP. To be continued...