In previous articles, we have seen many basics which should help to make a game : scrolling, moving shapes, collision, inputs (mouse, keyboard, gamepad)…
However, you may sometimes need to implement some physic principles : gravity, friction, restitution, etc.
This is where Farseer Physics Engine comes to the rescue.
Farseer Physics Engine is a collision detection system with realistic physics responses.
2 importants concepts to start with :
–World
The world object is the manager of it all. It iterates all the objects in the world each time you call the Step() function and makes sure everything is consistent and stable.
–Body
The body keeps track of world position. It is basically a point is space that is affected by forces such as impulses from collisions and gravity.
Lets add a project reference to farseer (note : I used msbuild /property:configuration=release « Farseer Physics XNA.csproj » to build it).
Then we will create a world, add a body to it and draw a texture based on the body position.
I encourage you to play with the world properties such as restitution, friction etc to see how it affects your world.
Also, notice how easy it is to detect collision (although I manage only plain shape for now, no convex shapes).
The source code : XNA_DEMO_19
A video to illustrate it.