How to implement Farseer Physics?

Dec 15, 2011 at 9:39 AM

Hello everyone,

I'm working on an Angry Birds type game and I wanted to use Farseer for the physics.

I made my own class structure. It basically consists of a World class, which has a GameState variable (GameState.Menu, GameState.Play etc...) and a list with a Menu and Play object. If the game state is GameState.Menu, the Menu (which consists of a MenuState variable and a list with Buttons and other objects) is updated and drawn, if the game state is GameState.Play, the Play object (similar to the Menu object) is updated and drawn.

I am using the Microsoft XNA framework.

My question is: is there any way to implement Farseer Physics without having to entirely rewrite my existing structure, and if so, how?

Please ask me if you need more information. I don't know if it's possible to add my project to this discussion, but if it is, please tell me how to do it. :)

Thanks,

Wouter Florijn

Dec 15, 2011 at 1:57 PM

Just include the header in your game class, and use a (don't remember the name, a joint?) to link you graphical objects with FPE physics objects. Also you physical objects need to be in a world object.

Dec 15, 2011 at 2:29 PM
victorspain wrote:

Just include the header in your game class, and use a (don't remember the name, a joint?) to link you graphical objects with FPE physics objects. Also you physical objects need to be in a world object.

Hi victorspain, thanks for your reply.

I am not exactly sure what you mean though. Do you mean I should add "using FarseerPhysics;" (or something similar) to every class that uses the physics engine?

Could you maybe tell me step by step how I should add a floor and a simple object like a cube to my game? I'm not very experienced with Farseer Physics nor C#. I could send you my project through e-mail if you want.

Thanks again!

Dec 16, 2011 at 10:31 PM

I'm not in a dev PC so I don't have any of my files or programs. But I'll try to write what i remember:

 

1. Yes, include the "using Farseer Psycis" and some more libraries into the classes of your game which will use physic objects. My game uses that ones:

 

using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics.Factories;

 

 

2. In the constructor of your main or game class write

World.Gravity = new Vector2(0f, 10f);  //Which will create a world object with gravity. This is needed for FPE to work. All objects must be in a world.

And the for the ground you may use a just a line. This is done by setting a list of vertices (2 for a line, various for a shaped ground), and make a body using this list of vertices.

Something like this:

Body ground = new Body(World);
            {
                Vertices terrain = new Vertices();
                terrain.Add(new Vector2(-20f, -5f)); //Vertice 1
                terrain.Add(new Vector2(-20f, 0f)); //Vertice 2
// etc...

                for (int i = 0; i < terrain.Count - 1; ++i)
                {
                    FixtureFactory.AttachEdge(terrain[i], terrain[i + 1], ground);
                }

                ground.Friction = 0.6f;
            }

If you want to draw the ground I think you'll need the ground to be a rectangle (not a line), and set a texture to it. I'm not sure of this. Or another option for the ground is to just draw the texture in the same coordinates where you set your ground.

The you should have a ground. Now you'll need object to collide with it xD.

Anyway, you should take a look at the FPE examples (Downloads section). You should learn a lot from them.
If you want to email me you project I could take a look at them and set up the basics for you to continue. My email is http://img694.imageshack.us/img694/7789/emailcf.jpg

Dec 17, 2011 at 12:05 PM

Hi, thanks for the explanation!

However, I still can't get it right. I've tried several things but I always get a lot of errors. I added a reference to the Farseer Physics XNA project and added the usings, but it doesn't seem recognize the World object.

I'll send you the project. It would be great if you could add some basic things, I think I'll be able to do the rest myself.