In this chapter of the tutorial, we will be implementing a physics simulation into our engine! We will not be writing the physics engine ourserlves, however, as that is far too complicated for this series. Instead, we will be writing some classes that are designed to interface with the open source JigLibX physics engine, which is built on XNA. This is great, by the way, because it means that we will be able to maintain compatibility with Xbox.
I have modified the original JigLibX project slightly to make it work better with our engine, and to fix small bugs in the original here and there. We will need to reference this project in our engine project. Download the dll here. Now, open “References” from the project, right click and choose “Add Reference”. Naviagate to the downloaded dll, and choose OK. We now have JigLibX referenced.
Note: If you are interested, you can download the modified JigLibX project here.
Now we need to start integrating JigLibX with our engine. The way this will be setup is as follows:
“Physics” Component: This will be used to manage JigLibX’s “PhysicsSystem” class. It will update the physics simulation and hold the instance of the physics system. We will need to add an instance of “Physics” to the engine’s service container for this to work. However, the benefit here is that if we don’t want to do all the extra phsyics calculations if we aren’t simulating any physics object, then we can simply not add an instance of “Physics” to the engine services.
“PhysicsObject” Component: This is the base type of physics simulation object. It keeps track of the neccessary objects to properly simulate a physical object with JigLibX, and other object types will inherit from it. These types are “Box”, “Capsule”, “Sphere”, “Plane”, and “Triangle Mesh”. The last one simulates every triangle in a model. This is useful for things like levels, which can have a complex design that can’t be simulated with a few boxes and spheres. Another plus here is that we could create new types of physics objects very easily to simulate more complex models without doing work for every triangle.
“PhysicsActor” Component: This component inherits from “Actor”, the key difference being that it takes a PhysicsObject, which is uses for drawing instead of user defined values (like the normal “Actor” class does). It does this by overriding the I3DComponent properties with the position, rotation, scale, and BoundingBox of it’s PhysicsObject.
Lets begin! Add a new class called “Physics”. Set it up as follows:
using JigLibX.Collision;
using JigLibX.Physics;
namespace Innovation
{
public class Physics : Component
{
// The physics simulation
public PhysicsSystem PhysicsSystem = new PhysicsSystem();
// Whether or not we should update
public bool UpdatePhysics = true;
public Physics()
{
// Set up physics system
this.PhysicsSystem.EnableFreezing = true;
this.PhysicsSystem.SolverType = PhysicsSystem.Solver.Normal;
this.PhysicsSystem.CollisionSystem = new CollisionSystemSAP();
}
public override void Update()
{
// Update the physics system
if (UpdatePhysics)
PhysicsSystem.CurrentPhysicsSystem.Integrate(
(float)Engine.GameTime.ElapsedGameTime.TotalSeconds);
}
}
}
This class will be responsible for handling the JigLibX PhysicsSystem and updating it. If we don’t want to simulate physics in a simpler game, for example, we can not create this class and keep extra calculation to a minimum.
The next class we will build is called PhysicsObject. This is the base class that simulated objects will inherit from. These child classes simulate boxes, capsules (pills), planes, spheres, and triangle meshes (simulate every vertex in a model). Here is the code for this class:
using JigLibX.Collision;
using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Physics;
using Microsoft.Xna.Framework;
namespace Innovation
{
// Provides a base object type for physics simulation
public abstract class PhysicsObject : Component, I3DComponent
{
// Local copy of the mass of the object
float mass = 1;
// The Body managed by the PhysicsObject
public Body Body;
// The CollisionSkin managed by the PhysicsObject
public CollisionSkin CollisionSkin;
// The mass of the PhysicsObject
public float Mass
{
get { return mass; }
set {
// Set the new value
mass = value;
// Fix transforms
Vector3 com = SetMass(value);
if (CollisionSkin != null)
CollisionSkin.ApplyLocalTransform(
new JigLibX.Math.Transform(-com, Matrix.Identity));
}
}
// The PhysicsObject's position
public Vector3 Position
{
get { return Body.Position; }
set { Body.MoveTo(value, Body.Orientation); }
}
// The PhysicsObject's rotation as a Matrix
public Matrix Rotation
{
get { return Body.Orientation; }
set { Body.MoveTo(Body.Position, value); }
}
// The PhysicsObject's rotation as a Euler Vector
public Vector3 EulerRotation
{
get { return MathUtil.MatrixToVector3(Rotation); }
set { Rotation = MathUtil.Vector3ToMatrix(value); }
}
// Whether or not the physics object is locked in place
public bool Immovable
{
get { return Body.Immovable; }
set { Body.Immovable = value; }
}
// Returns the PhysicsObject's BoundingBox
public BoundingBox BoundingBox
{
get
{
if (Body.CollisionSkin != null)
return Body.CollisionSkin.WorldBoundingBox;
else
return new BoundingBox(Position - Vector3.One,
Position + Vector3.One);
}
}
// Dummy scale value to satisfy I3DComponent
public Vector3 Scale
{
get { return Vector3.One; }
set { }
}
// The body's velocity
public Vector3 Velocity
{
get { return Body.Velocity; }
set { Body.Velocity = value; }
}
// Constructors
public PhysicsObject() : base(){ }
public PhysicsObject(GameScreen Parent) : base(Parent) { }
// Sets up the body and collision skin
protected void InitializeBody()
{
Body = new Body();
CollisionSkin = new CollisionSkin(Body);
Body.CollisionSkin = this.CollisionSkin;
Body.EnableBody();
}
// Sets the mass of the PhysicsObject
public Vector3 SetMass(float mass)
{
PrimitiveProperties primitiveProperties =
new PrimitiveProperties(
PrimitiveProperties.MassDistributionEnum.Solid,
PrimitiveProperties.MassTypeEnum.Density, mass);
float junk; Vector3 com; Matrix it, itCoM;
CollisionSkin.GetMassProperties(primitiveProperties,
out junk, out com, out it, out itCoM);
Body.BodyInertia = itCoM;
Body.Mass = junk;
return com;
}
// Rotates and moves the model relative to the physics object to
// better align the model with the object
public void OffsetModel(Vector3 PositionOffset,
Matrix RotationOffset)
{
CollisionSkin.ApplyLocalTransform(
new JigLibX.Math.Transform(PositionOffset, RotationOffset));
}
// Disables physics body and component
public override void DisableComponent()
{
Body.DisableBody();
base.DisableComponent();
}
}
}
The first type of object we are going to make is a box. We accept the lengths of the sides of the box, and set up the body. As you can see, most of the work is done by PhysicsObject, and most of the code we write here is just various constructor overloads to allow greater flexibility.
using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Physics;
using Microsoft.Xna.Framework;
namespace Innovation
{
// A box shaped physics object
public class BoxObject : PhysicsObject
{
Vector3 sideLengths;
// The length of the sides of the box
public Vector3 SideLengths
{
get { return sideLengths; }
set
{
// Set the new value
sideLengths = value;
// Update the collision skin
CollisionSkin.RemoveAllPrimitives();
CollisionSkin.AddPrimitive(
new Box(-0.5f * value, Body.Orientation, value),
(int)MaterialTable.MaterialID.UserDefined,
new MaterialProperties(0.8f, 0.8f, 0.7f));
// Set the mass to itself to fix the local transform
// on the CollisionSkin in the set accessor
this.Mass = this.Mass;
}
}
// Constructors
public BoxObject()
: base()
{
InitializeBody();
SideLengths = Vector3.One;
}
public BoxObject(Vector3 SideLengths)
: base()
{
SetupSkin(SideLengths, Vector3.Zero, Vector3.Zero);
}
public BoxObject(Vector3 SideLengths, Vector3 Position,
Vector3 Rotation)
: base()
{
SetupSkin(SideLengths, Position, Rotation);
}
public BoxObject(Vector3 SideLengths, Vector3 Position,
Vector3 Rotation, GameScreen Parent)
: base(Parent)
{
SetupSkin(SideLengths, Position, Rotation);
}
// Sets up the object with the specified parameters
void SetupSkin(Vector3 SideLengths, Vector3 Position,
Vector3 Rotation)
{
// Setup the body
InitializeBody();
// Set properties
this.SideLengths = SideLengths;
this.Position = Position;
this.EulerRotation = Rotation;
}
}
}
For the sake of brevity, I am not going to post the other physics objects in the post, instead, you can download the code files here:
CapsuleObject.cs
PlaneObject.cs
TriangleMeshObject.cs
SphereObject.cs
Now that we have all these object types, we are going to make a new Actor that knows how to deal with them. We will call this “PhysicsActor”. The difference here is that this class will accept a model and physics object, instead of a position. Most of the work will still be done by the base actor class, but we will be overriding the position, rotation, bounding box, and scale properties to take the physics object’s matching properties. In order to do so, we need to change these properties in the base “Actor” class to “virtual”, which simply means that classes that inherit from “Actor” will be able to override them, or replace their functionality. Change the position, rotation, etc. in the “Actor” class to this:
// I3DComponent values
// I3DComponent values
public virtual Vector3 Position { get; set; }
public Vector3 EulerRotation
{
get { return MathUtil.MatrixToVector3(Rotation); }
set { Rotation = MathUtil.Vector3ToMatrix(value); }
}
public virtual Matrix Rotation { get; set; }
public virtual Vector3 Scale { get; set; }
public virtual BoundingBox BoundingBox
{
get
{
return new BoundingBox(
Position - (Scale / 2),
Position + (Scale / 2)
);
}
}
Now add the “PhysicsActor” class. As you can see, all it does is replace the values of position, rotation, etc. to those of the physics object, and does some management of the object.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Innovation
{
// Manages a PhysicsObject and allows the engine to work
// with it
public class PhysicsActor : Actor
{
// The object we are managing
public PhysicsObject PhysicsObject;
// Override the position of the base class to that
// of the physics object
public override Vector3 Position
{
get {
if (PhysicsObject != null)
return PhysicsObject.Position;
else
return Vector3.Zero;
}
set
{
if (PhysicsObject != null)
PhysicsObject.Position = value;
}
}
// Override the rotation of the base class to that
// of the physics object
public override Matrix Rotation
{
get {
if (PhysicsObject != null)
return PhysicsObject.Rotation;
else
return Matrix.Identity;
}
set
{
if (PhysicsObject != null)
PhysicsObject.Rotation = value;
}
}
// Override the BoundingBox of the base class to that
// of the physics object
public override BoundingBox BoundingBox
{
get {
if (PhysicsObject != null)
return PhysicsObject.BoundingBox;
else
return new BoundingBox(-Vector3.One, Vector3.One);
}
}
// Constructors
public PhysicsActor(Model Model, PhysicsObject PhysicsObject)
: base(Model, PhysicsObject.Position)
{
this.PhysicsObject = PhysicsObject;
}
public PhysicsActor(Model Model, PhysicsObject PhysicsObject,
GameScreen Parent)
: base(Model, PhysicsObject.Position, Parent)
{
this.PhysicsObject = PhysicsObject;
}
// Override DisableComponent so we can remove the physics
// object as well
public override void DisableComponent()
{
this.PhysicsObject.DisableComponent();
base.DisableComponent();
}
}
}
We are now done with our basic physics implementation! Now let’s change our demo game so we can test it out! Here is the updated content for this tutorial: Content.zip.
To demo our physics, we are going to create a stack of boxes, then knock it down with another box after a few seconds. Change the load method to look like the following code. Here we are doing all the regular set up stuff, but we are also adding a Physics component, and creating a plane for the ground, and creating the pile of boxes.
protected override void LoadContent()
{
// Setup engine. We do this in the load method
// so that we know graphics will be ready for use
Engine.SetupEngine(graphics);
// Create a new Camera
Camera camera = new Camera();
// Setup its position and target
camera.Position = new Vector3(3, 3, 5);
camera.Target = new Vector3(0, 0, 0);
// Add it to the service container
Engine.Services.AddService(typeof(Camera), camera);
// Setup physics
Engine.Services.AddService(typeof(Physics), new Physics());
// Create the plane and make it immovable
PhysicsActor plane = new PhysicsActor(
Engine.Content.Load<Model>("Content/ig_plane"),
new BoxObject(new Vector3(4, .01f, 4)));
plane.PhysicsObject.Immovable = true;
// Load the model we will use for our boxes
Model model = Engine.Content.Load<Model>("Content/ig_box");
// Create the stack of boxes
for (int y = 0; y < 3; y++)
for (int x = 0; x < 3; x++)
{
PhysicsActor act = new PhysicsActor(model, new BoxObject(
new Vector3(.5f),
new Vector3(- .5f + (x * 0.52f), .5f + (y * 0.52f), -1),
Vector3.Zero));
act.Scale = new Vector3(.5f);
}
}
Now create a variable of type bool called fired, then update the update method (get it? funny right? ha ha ha). The “fired” boolean will keep track of whether or not we have launched our box. After two seconds have passed according to XNA’s GameTime, we will shoot a box at the pile.
bool fired = false;
protected override void Update(GameTime gameTime)
{
// Update the engine and game
Engine.Update(gameTime);
if (gameTime.TotalGameTime.TotalSeconds > 2 && !fired)
{
PhysicsActor act = new PhysicsActor(
Engine.Content.Load<Model>("Content/ig_box"),
new BoxObject(new Vector3(1), new Vector3(0, .5f, 1), Vector3.Zero));
act.Scale = new Vector3(1);
act.PhysicsObject.Mass = 1000;
act.PhysicsObject.Velocity = new Vector3(0, 2, -6);
fired = true;
}
base.Update(gameTime);
}
Run the game (F5), and you should see the pile of boxes get knocked over!
Congratulations, we have successfully built physics into our engine! In the future, our components will be able to take advantage of this and simulate physics of their own. Make sure you understand how everything works so far, because it’s important that you have a good understanding of our engine’s structure. See you next time, and don’t be afraid to ask questions in the comments if you need help!
Download the Code for this Chapter
Back to Game Engine Tutorial Series
« XNA 3.0 Released XNA Game Engine Tutorial Series #5.5 – Component Update »


thanks physics is just what I wanted.
I would like to download the modified jiglibx project but I get a 404 error.
and did you use the latest source code release (11 Oct) or the latest release(30 Apr)
Sorry! The links have been fixed.
And the library used here was modified from version 14907 in source control. I am going to update it to 18130 in the future.
error on the Content.zip link.
Man.. I’m really bad at linking stuff…
I’ve now checked every link in this post, and the previous posts. No more broken links.
I fell upon your blog and reread your engine series. Personally, I don’t really understand why you need an Engine class, emulating services, or a Component class, emulating Visibility and such. Why not have your engine be a Game subclass? Why not have your components as DrawableGameComponents? I don’t yet see the reasoning behind these choices.
Hey very nice one..tried to do something like this on my own but stopped after some failures =)
There are some missprints in the “stack constructor” (load Content) :
“<” ==
“&&” == &&
and The Engine.Content.Load need a specified type on my maschine…
I used Engine.Content.Load(“Content/ig_box”) -> works fine
but again well done , I´m by now yearning for the next one
regards floAr
ok the webengine mixed up my comment =)
in Load Content u have to replace the “& l t ;” with and the “& a m p ;” with &
hope this will work this time
and the web code seems to swollow the type specification aberfter Engine.Content.Load include a here…
I hope the Page wont crop me this time ^^
ok its cropped again.. mmaybe u can delete this posts sean sorry but they are all cropped up -.-
The webpage misscodes:
ur “smaller than” to unicode or something else
same with ” greater than” and ” logical and”
and it swallows the “smaller than Model greater than” after Engine.Content.Load
hope this time it works
Yay! Use Brian Peek’s Wiimote library (http://blogs.msdn.com/coding4fun/archive/2007/03/14/1879033.aspx) with this, and I feel a Boom Blocks clone coming on…
@David
Well, for starters, when writing an editor, we don’t want to use the Game class, because it doesn’t work well when embedded in a windows form. Without the Game class, we have none of the components or services the game provides.
Second, I want to have absolute control over everything in the game engine. If I want to add certain functionality, I can’t edit the game class, but I can edit the engine I wrote.
Third, we are not always going to be able to use XNA. C++ is still the standard in the games industry, so if we went to work at any major studio, we wouldn’t be able to use XNA. Of course, as independent developers we can choose what tech we use, but we can’t if we work at a larger studio. I want these tutorials to apply whatever language we happen to be using, as once again the game class is not provided for us unless we use XNA.
Last (and this goes along with my third point), if we ever wanted to port the engine to another language, we already have all the structure done. All we would have to do is translate it into another language, instead of recreating the Game class there.
Also, it’s good practice and just useful information to know how to do the kinds of things Game is doing for you in the background, as things like draw orders and grouping can be applied to things other than game programming.
@floAr
WordPress is trying to keep HTML out of the comments to avoid code injection attacks. The less and greater than signs are very common in HTML, and an HTML tag is formatted with the name of the tag in between the two, aka “<table>”. So it saw “<Model>” and just completely removed it because it thought it was an HTML tag.
Same thing is happening in the post. It sees “<”, “>” and “&”, and replaces them with “& l t ;”, “& g t ;” and “& a m p ;” because those are what you would put in the HTML if you wanted to put those characters on the screen and not have it think of them as HTML. The problem is, they are inside a code block, which puts exactly what is inside it on the screen, so it is printing “& g t ;” instead of “& g t ;”. It was also stripping out “<Model>”. The problem is that WordPress is fighting with the code block to protect the page from malicious HTML. I have to fix everything by hand so there are often spots I miss. I fixed them, and added the “<Model>” to the content loads.
Hi,
I like the tutorial but I have one request.
I am following this to understand how to use JigLibX in my own game. Any chance you could let us know exactly what changes you made to JigLibX or release the source?
Thanks
Its linked in the post. The changes are that there is a fixed bug in the Sphere/TriangleMesh collision class, and HeightInfo has been added for terrain later on.
Thanks
Sean, just wanted to point out for some people that inside the CapsuleObject class, the second overload for the constructor method CapsuleObject(float length, float radius) does not call the base class like the rest of the constructors too. May be necessary how you would like the design.
First, I want to thank you for the wonderful tutorials.
I had a question regarding the SphereObject class. In the set accessor of the Radius property, you add a new Sphere primitive to the CollisionSkin. However, when creating a new Sphere, you pass it a position of Vector3.Zero * 5.0f. Isn’t this the same as Vector3.Zero? If not, could you help me understand what this does?
Thank You
Umm, not sure if this is an error of mine (almost must be) but the stack of box’s seem to just fall through the floor !
I have plance.PhysicsObject.Immovable = true;
but that doesnt seem to quite cut it.
Does your engine accept .X files? Also, I have a bridge that was developed in 3DS Max and exported into an .X file. Would I load this in the same way as the plane you use in this example?
Need help. My boxes only show up for a split second and then they are gone. If I make them immovable or if I set the update physics to false, they stay. I thought maybe gravity was working too fast but when I step through it one frame at a time the boxes sit still for 25 updates and then disappear.
I downloaded the tutorial code and it work just fine. I’ve been throught the code till my eyes dried out and I can’t find any differences. I need to find a compare code program like someone else mentioned, until then, if anyone could point me in the right direction I would appreciate it.
WINMERGE FOR THE WIN!
sideLengths SideLengths in BoxObject constructor. I spent 3 hours looking for this problem. Decided to call in reinforcements, googled and found WinMerge, found the problem in less than 10 minutes.
WinMerge is available at http://winmerge.sourceforge.net/
should read “sideLengths not equal to SideLengths”
Awesome tuts. Altho I came across a problem.
I added the JigLibX reference but now coming across things like:
Error 1 The type ‘Microsoft.Xna.Framework.Vector3′ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.Xna.Framework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d’.
After some searching they told me this was caused of different versions of dll´s or building on different platform. I am building on my xbox. Any solution for this?
Before implementing the physics part all was fine and running properly
This happens because you are not referencing an Xbox 360 version of the JigLibX dll. You can build the JigLibX project in Xbox 360 mode to generate an xbox 360 dll.
Because we use a customized version of JigLibX, I will be posting dlls of the new JigLibX 0.3.1 in PC and Xbox versions soon.
Hey just a question… do you need to have two “using JibLibX.Collision;” pieces of code…
Just want it to looks sweet and started seeing double
nice Tutorials by the way. Love working
Hi All, I’m getting the following error
Error 2 No overload for method ‘AddPrimitive’ takes ’3′ arguments C:\Users\Garfinkle\Documents\Visual Studio 2008\Projects\InnovationEngine\InnovationEngine\Physics\BoxObject.cs 24 17 InnovationEngine
Anyone know what is causing it?
Great tutorial btw sean..Been looking at free engine’s for a while to find a good one then came across your tutorials and thought…make my own (well make yours)
Just been looking at the .dll file JigLibX and there are only 2 AddPrimitive Methods
public int AddPrimitive(JigLibX.Geometry.Primitive prim, JigLibX.Collision.MaterialProperties matProps)
Member of JigLibX.Collision.CollisionSkin
and
public int AddPrimitive(JigLibX.Geometry.Primitive prim, int matID)
Member of JigLibX.Collision.CollisionSkin
no of which accept 3 parameters…is there a problem with the dlls in the downloads?
I downloaded the modified version and it works now.
Getting another error though now
Error 1 The type or namespace name ‘CollisionSystemSAP’ could not be found (are you missing a using directive or an assembly reference?) C:\Users\Garfinkle\Documents\Visual Studio 2008\Projects\InnovationEngine\InnovationEngine\Physics\Physics.cs 19 54 InnovationEngine
First off bloody great stuff in these tutorials.
The problem i am having is that content added anywhere outside load content doesnt render. For example in this tutorial the large box that hits the others does not render unless i add it in LoadContent. Any idea what coudl be causing this as i have ran it in debug and nothing appears different to anything else.
oh nevermind i had a clear screen still in there from earlier mucking around. I suppose this is what happens when you stay up to 7am coding. Thanks keep up the awesome stuff
Loving the tutorials, but having an issue with this one. When I compile and run on XBOX 360 (using the jiglibx.dll from the modified project) everything comiles fine but as soon as the call to
Engine.Services.AddService(typeof(Physics), new Physics());
is added the 360 just goes to a black screen and freezes. The same code run on Windows with the Windows jiglib.dll runs a charm. I’m running XNA 3.1. Anybody got any suggestions as I’m losing what little hair I have left with this!!
Cheers folks and keep up the superb work.
Maby someone will find it usefull. XboX do not support librarys that are not written in c# so you can not use any third part libs like JigLIb ,PhysX etc.