starting with FPE 3.2: doubts

Topics: Developer Forum
Jan 27, 2011 at 4:48 AM
Edited Jan 27, 2011 at 5:14 AM


I am new to using FPE, so please bear with my questions. Also, since 3.2 is based on Box2D, I have been reading their manual and pieces of code scattered in samples to better understand terminology and usage.

Pong is usually my testbed whenever I try to do something new. Here is one of the issue I am running into:

- How can I restrict paddles to move only along Y axis, because the ball comes in and knocks off the paddles and everything floats in space afterwards ? (Box = Rectangle and ball = circle)

- I know MKS is the unit system, but is there a recommendation for sizes/position to be used ? I know this is a very generic question, but it would be good to know a simple set of values that one could use for making a game as simple as pong.

Between box2d and FPE, I have some doubts:

- what is the recommended way of making a body in FPE ? 

  • world.CreateBody() does not exist in FPE
  • Box2d manual recommends never to "new" body(since Box2D uses Small Object allocators), so is there a recommended way in Farseer to create a body (apart from factories) ?

- In box2d, it is recommended to keep a track of the body object, since it is also the parent to fixture(s). Why is it that in most of the examples, the fixture object is tracked ? Is there a reason why body is not tracked ?

If you want, I can post my pong code on the svn, so other beginners can use it to get started.


Jan 27, 2011 at 5:48 AM

The paddle should be a Kinematic body. That means you have to manually set the position and velocity, and it doesnt react to collision (the balls will bounce off the paddle, but the paddle wont move)

The Box2D manual recommends sizes between 0.1 and 10, I think. Read up to make sure.

To create a body in farseer you go:

Body b = new Body(_world);

Have a look at the testbed samples for things like this, and to see how the developers do it. You will also find examples of kinematic bodies there.

Jan 27, 2011 at 11:34 AM
Edited Jan 27, 2011 at 11:38 AM

Regarding the tracking: In the samples most bodies just have one fixture and fixtures store the body they are attached to... so it does not really matter. The main reason for tracking fixtures in the samples is the mouse interaction I guess, since the samples need Fixture.TestPoint() a lot to determine if the user just picked up an object.

Once you have more complex objects with several fixtures it might make more sense to track bodies. It really depends on your game and physics setup.


As for your pong code: Im currently working on restructuring the samples completely and added a new category with little game samples. If you have your code ready and working you could just mail it to me and I'd gladly integrate it in there.

Jan 27, 2011 at 4:35 PM

So, my current configuration:

  • Ball : Dynamic body, Circle-Shape, 0.5f density
  • Paddle: Kinematic body, Rectangle Shape, 0.5f density

So, if I understand this correctly, Kinematic bodies respond to velocity and not to forces. They also don't collide with other kinematic or static bodies ?

When I made the paddle a Kinematic body, I run into two problems:

  • When the ball hits the paddle, its slides along the paddle instead of reflecting off of it (edit: setting the paddle restitution to 1 made it an elastic collision)
  • All of the paddles and ball are surrounded by a static Polygon (rectangle). Now, as soon as I make my paddles kinematic bodies, they are not bounded by this rectangle and go off the screen. What would be a good way to resolve this ?

I will look further in the testbed samples to get a better understanding.

@Elsch: Thanks for the comment on tracking. I can see the reasoning there.

@Pong code: if I can get the above issue resolved, it pretty much works. One thing I'd still want to work out is, good values to pick for the density of the paddles. I can upload it to the svn and maybe someone can take a look at the code, and provide some comments on choosing good values. I don't think it would take too long to write a small tutorial of some sorts, and I can summarize some of the very basic things a beginner could use to get started with FPE.


Jan 27, 2011 at 4:48 PM

You could always check the paddle position manually and set the velocity to 0 if it hits the screen border. A nicer solution would be using a fixed prismatic joint. Have a look at the BodyTypes test in the Testbed. The code used to create the moving platform from that sample should translate wonderfully to your paddle ;)

Jan 27, 2011 at 8:30 PM
Edited Jan 27, 2011 at 11:27 PM

@Elsch: Thanks for pointing out the fixed prismatic joint. I read about it on box2d manual and checked out the bodytypes sample. I understand that it is meant to restrict the translation of a body (or two bodies) along one axis.

I attached a prismatic joint to each paddle, but I have a few doubts about some of the properties:

  • Upper/LowerLimit: Is it suppose to set the max/min limits for the body to move ? If so, I'm missing something here since, I set the limits and even when the body goes beyond these bounds, it continues to move in that direction.
  • Motor: It may be obvious to some, but I can't understand how to use the motor. From the manual it states, A joint motor drives the motion of the connected bodies according to the joint's degrees of freedom. 



Jan 28, 2011 at 4:04 AM

For the limits to work you must set:

_prismaticjoint.EnableLimits = true;

The motor will move the bodies along the axis. A negative value for MotorSpeed will move them towards each other and a positive value will move them away from each other. (remember to set EnableMotor = true, and MaxMotorTorque)

Jan 28, 2011 at 4:19 AM
Edited Jan 28, 2011 at 5:31 AM

This is how I am creating my joint for a paddle:

            FixedPrismaticJoint fixedPrismJoint = new FixedPrismaticJoint(mBody, mBody.Position, new Vector2(0, 1f));
            fixedPrismJoint.LimitEnabled = true;
            fixedPrismJoint.LowerLimit = -25f;
            fixedPrismJoint.UpperLimit = 25f;
            fixedPrismJoint.Enabled = true;

Then, when I hit the Up key, I do the following:

mBody.LinearVelocity = new Vector2(0, 10.0f);

The body just continues to go up and is not restricted by the upperlimit. What am I doing wrong here ?

Jan 28, 2011 at 5:30 AM
Edited Jan 28, 2011 at 5:33 AM

mBody must be Dynamic, for a fixed joint to work on it:

mbody.BodyType = BodyType.Dynamic

All Joints need at least one Dynamic body to work, and Fixed joints only have one body so that body must be Dynamic.

And try using the motor to control the speed instead of setting velocity:

fixedPrismaticJoint.MotorSpeed = 10.0f;

Setting velocity manually should not be done with a dynamic body, or you can upset the collisions and joint forces.

Jan 28, 2011 at 5:34 AM

You beat me to it. I just discovered that. As elsch had recommended me, I was using Bodytype as Kinematic, not realizing that joints need dynamic bodies.

In my pong game, I used motorspeed for the NPC, and it worked like a charm!