Adding 2D physics to your XNA Game Studio game. Part 1
One of my hobby projects (again, not Microsoft official) is a port of the Box2D physics engine. My brother Nathan Furtwangler and I have been porting Box2D to C#/XNA and we recently posted it on CodePlex under the MIT license. We call it Box2D.XNA. It’s our goal to keep it in sync with the latest Box2D changes and to optimize it for the Xbox 360. In this post, I’ll give you a basic overview of Box2D.XNA and show how to use it in your game.
Lets start with the basic hierarchy of the objects you’ll be working with:
World – the root object for the physics simulation. Step this once a frame & you’re simulating physics!
Body – Multiple bodies can be added to the world. These are the objects that interact with each other via collisions.
Fixture – A body is made up of multiple fixtures. Fixtures combine a shape with material properties to form the overall “look and feel” of the body. An example of a fixtures is a bouncy, slippery circle that’s highly massive.
Shape – Each fixture has a single shape object that defines its mathematical bounds. Box2D supports circles and convex polygons. Multiple fixtures can be added to a body to make more complicated shapes.
Joint – The world has a set of joints that bind Bodies together. Different joints do this using various constraints. For example a Distance joint will limit the distance that two points on two bodies can separate. Box2D.XNA has lots of joint types that we can talk more about in another post.
Contact – While stepping the world, Bodies will collide & respond to collisions realistically. This is accomplished using Contacts. You can listen in to the contact being generated and use this information in your game. Want to know when the players sword strikes the skull of the final boss? Listen for a contact between the two, and you’re good to go.
Now that we’re familiar with the basic objects of a physics simulation, lets get our hands dirty and make something cool!
To get started you’ll need to download/install XNA Game Studio 3.1 if you don’t already have it. Next extract the zip above and open the project in Visual Studio.
The goal for this project is to take an image and approximate it with little circles. Then when a key is pressed we’ll start simulating the physics and the balls will drop/bounce all over the place. Sounds like fun, lets get started…
The first thing we need to do is to create the physics World. The first parameter is the gravity, and the second argument tells the simulation to make objects go to sleep if they aren’t interacting with anything.
_physicsWorld = new World(new Vector2(0, 100), true);
I used a gravity of 100 because I’m dealing with pixels/s^2. Normally you’d want to work in meters & seconds or else things get a bit unstable.
Next we need to add bodies to it. In this case I will randomly position some circles…you can look at the code to see how I decide on a location, but here’s the code to add the Body.
private void AddCircle(Vector2 position, Color color)
var circle = new CircleShape();
circle._radius = _circleRadius;
var fd = new FixtureDef();
fd.shape = circle;
fd.restitution = 0.98f;
fd.friction = 0.1f;
fd.density = 1.0f;
BodyDef bd = new BodyDef();
bd.type = BodyType.Dynamic;
bd.position = position;
var body = _physicsWorld.CreateBody(bd);
Now we just need to step the simulation every update (after you press a key):
_physicsWorld.Step((float)gameTime.ElapsedGameTime.TotalSeconds, 10, 3);
The arguments to this method control the time step, and how accurate the simulation is. Feel free to experiment with the last two for your scene.
That’s it! We just made a really cool app in only 10 minutes of coding! Feel free to use Box2D.XNA in your XNA projects, and if you make anything cool leave a comment with a link.