Physics Problem in a simple Pong game

Hey there,

I’m trying to create a simple pong game, but am coming up against a Physics problem I can’t diagnose.

Once the ball goes offscreen I remove it, set it to ‘nil’, and then create a new ball and ‘serve’ it. As you can see in this short video, this new ball gets stuck.

https://dl.dropboxusercontent.com/u/761630/pong-physics-problem.mov

I can see in my terminal debugs (also in the video) that the yVelocity is positive, but as soon as it hits the bottom wall it should be inverted…

– If the ball hits the top and bottom walls

local function topBottomCollision( self, event )

  if( event.phase == “began” ) then

    yVelocity = yVelocity * -1 – reverse the Y direction

    ball:setLinearVelocity( xVelocity, yVelocity )

    return true

  end

end

I’ve attached game.lua and the two class files it refers to (). There’s not much to it. Most of the code is creating the walls for the ball to bounce off. 

I’m using Corona Version 2014.2189 (2014.3.6).

Thanks so much for any help you can offer!

Rich

I might suggest just repositioning the ball instead of deleting and recreating it. Also, for a game so simple, physics is probably overkill, unless you’re planning on adding obstacles.

You might try just adding simple velocity to the ball, reversing it if the ball’s Y is over a certain amount or under a certain amount, etc.

  • Caleb

Thanks Caleb.

I hope to plan obstacles, power ups, multiple balls etc., and also just want to understand the physics engine. This seemed like a good place to start.

Although it’s ‘Pong’ the few extra bells and whistles will (hopefully!) teach me a bunch of new things. 

I guess I just want to understand where I’m going wrong before abandoning it completely :slight_smile: If you can have a look at the code and  point me in the right direction I’d really appreciate it.

Rich

Ok, here goes:

  1. You’ll want to, instead of relying on a collision event, set the bounce property of your walls to 1.0. It’s just a better way to do it, because it’s built into Box2D.
  2. You should also set your paddles’ bounce to 1.0 instead of using a collision event (which I assume you’re using)
  3. You’ll need to keep the balls’ speeds constant. So you’ll calculate the speed of each ball each frame (or each several frames, to boost performance) and the angle they’re traveling, and :setLinearVelocity() to them to make sure their speed remains constant. Box2D has a horrible way of making velocity decay, because of approximations to boost performance.

Once you’ve got a constant velocity going, you can go on from there. You’ll have a good, accurate and firm base to build your game on.

Hope this helps :slight_smile:

  • Caleb

Hey Caleb,

So far, so good! Turns out your advice is better than Walter’s - https://github.com/coronalabs/PongTutorial :wink:

Thanks so much, you’ve helped out a bunch. :smiley:

Rich

No problem :slight_smile:

  • Caleb

I might suggest just repositioning the ball instead of deleting and recreating it. Also, for a game so simple, physics is probably overkill, unless you’re planning on adding obstacles.

You might try just adding simple velocity to the ball, reversing it if the ball’s Y is over a certain amount or under a certain amount, etc.

  • Caleb

Thanks Caleb.

I hope to plan obstacles, power ups, multiple balls etc., and also just want to understand the physics engine. This seemed like a good place to start.

Although it’s ‘Pong’ the few extra bells and whistles will (hopefully!) teach me a bunch of new things. 

I guess I just want to understand where I’m going wrong before abandoning it completely :slight_smile: If you can have a look at the code and  point me in the right direction I’d really appreciate it.

Rich

Ok, here goes:

  1. You’ll want to, instead of relying on a collision event, set the bounce property of your walls to 1.0. It’s just a better way to do it, because it’s built into Box2D.
  2. You should also set your paddles’ bounce to 1.0 instead of using a collision event (which I assume you’re using)
  3. You’ll need to keep the balls’ speeds constant. So you’ll calculate the speed of each ball each frame (or each several frames, to boost performance) and the angle they’re traveling, and :setLinearVelocity() to them to make sure their speed remains constant. Box2D has a horrible way of making velocity decay, because of approximations to boost performance.

Once you’ve got a constant velocity going, you can go on from there. You’ll have a good, accurate and firm base to build your game on.

Hope this helps :slight_smile:

  • Caleb

Hey Caleb,

So far, so good! Turns out your advice is better than Walter’s - https://github.com/coronalabs/PongTutorial :wink:

Thanks so much, you’ve helped out a bunch. :smiley:

Rich

No problem :slight_smile:

  • Caleb