Box2D / Farseer Physics Engine basics

I recently wanted to use a physics engine for the game, and I saw a lot of documentation scattered across manual and the code. This is my effort to document as much as I can and hopefully it helps someone who are about to get started!

I am using Farseer Physics Engine 3.2 which is a port of Box2D (along with some other extra tools). Let’s get started:

Common Objects in the physics system:

- World
- (Rigid)Body
- Shape
- Fixture

World: World is the heart of FPE. It makes the whole Physics world tick. There are multiple parameters that can be tweaked in this for efficiency, performance, accuracy etc.

Rigid Body: Hard chunk of matter that keeps a track of the position of the object

Shape: Object’s Geometrical shape like circle, rectangle, polygon etc

Fixture: Fixture binds a shape to a body and adds material properties to the object. (Material properties such as density(mass per volume), friction, and restitution)

PS: Memory management in Box2D recommends never to "new" / "malloc" Body, Joint and Fixtures, since it has its own memory management implemented(Small Object Allocators). This does not apply to FPE.

World:
- Create world:

mWorld = new World(gravityVector); 

- Update World:

mWorld.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f); -- ticks the world. 

Basics can be read at: Simulating the world; sec 2.4 at http://www.box2d.org/manual.html#_Toc258082968

Rigid Body:

Create a body:

Body myBody;

FPE: We have multiple options here:

myBody = new Body(World); // do we need to add myBody to world afterwards ?

    – using Factory which internally calls new Body with the right parameters

mBody = BodyFactory.CreateBody(...); 

    – referencing body from fixture (assuming the fixture has been created)

    

mBody = myFixture.Body;

Specifying Bodytype:

- Static : DOES NOT MOVE. Infinite mass. Can be set by user

- Kinematic: Respond to velocity and not to forces. They also don’t collide with other kinematic or static bodies. Velocity is set by user

- Dynamic: Fully simulated body.

FPE:

mBody.BodyType = BodyType.Dynamic; 

Shape:

Object can be described by different shapes. Some of the common shapes and how to create them:

FPE
 

CircleShape circleShape = new CircleShape(radius, density ); 

  – PolygonShape which relies on a Vertices (: List<Vector2>) => See PolygonTools to generate vertices and then make PolygonShape using them

  – Using FixtureFactory:: It creates the body and attaches the shape to it and returns the fixture

Fixture:

It ties the geometrical shape to a body. A body is parent to a fixture and can have multiple fixtures, each adding to the mass of the object.

Creating a fixture:

FPE
 

new Fixture(Body, Shape); 

mBody.CreateFixture(Shape); // internally calls the new Fixture 

FixtureFactory.CreateRectangle(...) // creates a body and shape based on the parameters and attaches this fixture to body

Advertisement

One Comment on “Box2D / Farseer Physics Engine basics”

  1. enc says:

    thanks for summing them together! it was a great pain to find all those from all the scattered docs.


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.