Back to basic. Connecting Bodies.

Nov 22, 2009 at 12:15 PM


I’m all a bit new to game programming and Farseer physics, so please forgive me for asking the simplest of things for a short while!  I’ve programmed in java for a number of years but have decided to use my spare time to try XNA and Farseer, so far I’m having good fun!

My goal is to create a simple side-view car/truck.  So what I am aiming for first is a simple suspension model.  So far I have been able to create a wheel and apply torque, I’ve also created a few other small things as well.

What I can’t understand is how to ‘attach’ bodies to each other.  For example, if I have a rotating wheel and I also have a joint that I want to attach to the wheel, how do I go about doing this?  I’ve had a look at the documentation, and also the samples but I can’t get my head around it.  I know it’s simple but I just can’t figure it out!

Any guidance would be great!



Nov 22, 2009 at 1:54 PM

Bodies are points in space. Connecting them requires some sort of constraint. One contraint might be to keep the two points (bodies) a fixed distance apart. For this we have the PinJoint.

The trouble most of the time is to figure out what joint/springs to combine and how. We have had several posts about how to create a truck/tank suspension and a lot of suggestions along with working suspensions were proposed. Try searching on the forums.

To answer your question, you simple use the JointFactory to create a constraint between 2 bodies:

JointFactory.Instance.CreatePinJoint(physicsSimulator, _body1, anchor1, _body2, anchor2);

it is as simple as that. Now _body1 and _body2 are a fixed distance apart.

Nov 22, 2009 at 3:30 PM
Edited Nov 22, 2009 at 4:49 PM

Thanks for the reply.

I can't get your suggestion to work for me.  Everythng complies, but the joint just doesn't work.

I have tried a few different ways which I've found through looking through the forum but none have worked.

Example:  JointFactory.Instance.CreatePinJoint(_connectionBody, new Vector2(0, 0), _fWheelBody,  new Vector2(0, 0));

^^ _connectionBody is a rectangle and _fWheelBody is a circle.

I think it's because I'm not 'using' the code correctly.  Where should I use the above code (providing that it is correct)?



 //Sorted.  Didn't realise you had to use



Nov 22, 2009 at 7:25 PM

Either you use PhysicsSimulator.Add() to add the joint to the physics world or you use the CreateXXXJoint() with the PhysicsSimulator overload as I wrote in my example above.