Making Pong using Farseer Physics engine II

If you followed my last two posts (basics and part I), you will have a paddle and a ball setup in the game.

However, if you run the game, you’d observe the ball on collision with paddle; just blows it away. Instead, we’d want the ball to just bounce off.

There are two options:

  • BodyType.Kinematic: We can make the paddle bodytype kinematic. This way, the ball would just bounce off the paddle. This however introduces another problem. Kinematic bodies don’t collide with other static/kinematic bodies. Thus, the paddle would not be restricted by the boundaries of the world box (unless, you manually cap it with hardcoded values).
  • Fixed Prismatic Joint: There are good resources online to understand what this is. (Search for prismatic joint in box2d manual and here). It essentially restricts the body(s ) translation along one axis only. This fits the bill perfectly! (Remember bodies have to be dynamic type to be simulated for joint-physics).

Using Fixed prismatic joint we can restrict the paddle to move along only Y axis. Appetizer code follows:

FixedPrismaticJoint fixedPrismJoint = new FixedPrismaticJoint(mBody, 
mBody.Position, new Vector2(0, 1f));

 

translates to: Create a joint for this body, at the given position, along the given axis (0, 1f). This way, the paddle is restricted to this axis, and moves only about the given position.

Setting limits to the movement of this joint:

fixedPrismJoint.LowerLimit = -45f;
fixedPrismJoint.UpperLimit = 45f;
fixedPrismJoint.LimitEnabled = true;

This would restrict (clamp) the body from moving beyond +/45 m along the given axis (0, 1f).

Automating movement using motors:

fixedPrismJoint.MotorSpeed = 5.0f; // in meters / second

fixedPrismJoint.MaxMotorForce = 1000.0f; // maximum force in Newtons
fixedPrismJoint.MotorEnabled = true;
This would put the motor into effect, which would the make body automatically oscillate between the bounds with MotorSpeed.
What good a joint is, if we don’t add it to the world:
Worldphy.AddJoint(fixedPrismJoint);

That’s it! If you do this, you’ll have a paddle which only moves along Y axis, between those bounds and also simulates against the other elements in the world.

Divine Human Intervention:

In Paddle::Update():

if (Input.IsNewKeyPress(Keys.W))
{
mBody.ApplyLinearImpulse(new Vector2(0f, 30.0f));
}
else if (Input.IsNewKeyPress(Keys.S))
{
mBody.ApplyLinearImpulse(new Vector2(0, -30.0f));
}

This makes sure the paddle responds by trying to go up/down when you hit W/S.

This would let you make a complete pong game! But often in your game, you’d want to see debug information about the shapes. For the visual delight, there is a debug library that comes along with farseer. Its really easy to set it up and align your textures with the physics objects. More on that in the next tutorial!

Advertisement


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.