tags:

views:

295

answers:

2

So I have been trying to recreate Pong in Box2D to learn how to use the engine and I think creating pong in it is more complicated than i thought. I have the collision down by itself, i have the ball restitution set at 1.0 , my only problem is I have no idea how to have the ball hit the paddle, bounce back at the right angle and have enough speed to reach the other side. I just want it to look like a natural Pong Game. I have looked at c++,actionscript but it seems like no one has really fully implemented a pong game.

Would I have to just implement my own physics?

+5  A: 

Basically, for an authentic Pong feel, you need to implement your own physics.

Classic games generally implement physics as simple 2D velocity and position (and maybe acceleration), and then fake everything else.

(It's worth noting that even modern games fake as much as they can get away with. It's not worth implementing something the hard way if you can do it the easy way and still get the same gameplay feel.)

The "faked" physics for Pong should be fairly simple. Here is a very basic, not necessaraly complete bit of simplistic physics for you:

Vector2 position;
Vector2 velocity;
protected override void Update(GameTime gameTime)
{
    position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

    if((IsInsideLeftBat(position) && velocity.X <= 0)
            || IsInsideRightBat(position) && velocity.X >= 0)
    {
        velocity.X = -velocity.X; // Bounce the other way
        // To change things up, maybe you can change velocity.Y too.
    }

    // And of course handle the ball going out of the play field, scoring, etc...
}

Now, say you really have need for real physics in your game. A pong game with real physics could be fun (in fact somebody has already done it, even real-er).

The thing I said about "faking it" still applies. Physics in games is usually more about tweaking things to get something that feels right than actual realism. Perhaps you need to increase your bounce coefficient to 1.1, and maybe add some linear drag to the ball so it doesn't fly out of control?

(Or if you're having trouble getting the ball to the other side, try reducing linear drag.)

Actually what is more likely for a "main character" in a game - you want to keep it in the physics simulation so it can interact with things (perhaps your play field is littered with obstacles - that could be fun) - but you want to have complete control over its motion.

To do this simply, after your physics engine update runs, go in and change any values you don't like! In Pong's case this might be something simple like resetting the velocity of the ball so it moves with a constant speed each frame. I imagine you're already doing something like this for the paddles - to set their position each frame.

(I used this approach when I made a platformer on a physics engine.)

Another alternative way to fake things might be to add a collision handler to the paddles that sets the ball's velocity just how you like it.

Andrew Russell
A: 

My other answer still applies when making games using physics engines. But I have also realised that you say:

i have the ball restitution set at 1.0

Do you also have the paddle restitution set to 1.0? I haven't checked Box2D, but in Farseer (what I have on hand and which is related to Box2D) the restituion force used in a collision response calculated as an average (from Arbiter.cs):

_restitution = (GeometryA.RestitutionCoefficient
               + GeometryB.RestitutionCoefficient) * .5f;

If Box2D works the same way, which it probably does doesn't (thanks Mark from comments), your paddles will also need restitution set to 1.0 to get the effect you want.

(Also: this demonstrates how it helps to have the source code to your physics engine on-hand.)

Andrew Russell
In box2d, restitution is the max of the two coefficients (by default).
Mark