# EDIT:

This series ended up on Tuts+, and these are sort of deprecated. Please visit Tuts+ to see the finalized articles.

# Personal Update

Beyond C++ reflection I’ve taken a huge liking to physics programming for games. Reflection in C++ is a largely “solved” area of study. Though resources on the internet may be few and far between for creating custom introspection, people that know how to create such systems have explored much of what is to be accomplished.

However physics is another story entirely. Up until just a few years ago physics for games was in a highly primitive state; techniques just hadn’t been created due to the inability of hardware to perform necessary computation. There are a lot of new and exciting problems to solve.

So this next year while attending DigiPen I’ll be focusing my studies around: engine architecture, data oriented design and optimization, C++ introspection and game physics. This last topic being the topic I’m going to start an article series on.

# Game Physics High Level Detail

A game physics engine performs some functionality for ensuring that shapes behave in a particular way. To state this one could say: a physics engine removes degrees of freedom from moving bodies, and enforces these imposed rules.

From here on out, until specified otherwise I’ll be talking specifically about convex rigid bodies within a physics engine. A rigid body is just a shape that is implicitly rigid; the engine does not naturally support deformation of a set of points that make up a shape. Using this implicit definition the mathematics behind manipulating such shapes can be known intuitively and implemented efficiently.

Here’s a short feature list of some features a custom physics system may provide, assuming the engine uses rigid bodies:

• Collision detection
• Collision resolution
• Linear movement, angular rotation and integration
• Raycasting
• Islanding/sleeping/portals
• Friction simulation
• Spatial partitioning/bounding volumes
• Fracturing/splitting
• Multi-part bodies
• Various types of shapes
• Advanced mathematical constraints (joints, motors, contact constraints, etc.)
Raycasting from Box2D.

In this article series I will attempt to talk about all of the above topics in my own time. I’ve learned a lot about physics and physics engines in a short amount of time and by documenting what I have learned I hope to solidify my understanding, as well as help out others to achieve what I have. I would not have learned nearly as much as I currently know without help from others so it is only natural to want to do the same.

The best example of an open source physics engine that employs much of the feature list shown above would be Box2D by Erin Catto. The rest of this article series will be detailing the physics engine that I myself have written. There are of course other methods I choose not to talk about, or just don’t know about.

# Architecture

There are two main objects that make up a physics engine: shapes and bodies. A body is a small package of data that defines intrinsic properties of a physics object. Properties such as density, restitution (elasticity), friction coefficients, inertia tensors, along with any other flags or settings should be stored within the body. These bits of data are properties that can be isolated away from the shape definition. The shape itself is contained with the body through a pointer.

The shape definition defines what sort of shape a physics object represents. Some physics engines can attach multiple shapes to a body, which are referred to as fixtures. A shape stores any data required to define the shape itself, and provides various functions to operate on the shape, such as testing for line or point intersection, or generating a bounding volume.

Together the body and shape represent a physics object, which by the end of this article series will be able to perform a lot of interesting interactions with other physics objects.

A tower of stacked oriented boxes within a custom physics engine called iiD.

All bodies should be contained within what is known as a scene or world. I refer to this object as a scene. The scene should contain a list of all live objects, as well as functionality inserting or removing bodies from the scene. The scene should also have callbacks for processing shape or ray queries. A query just checks to see if any bodies intersect with something like a point or ray.

The scene has one particular function called step, which steps the scene forward in time by a single delta time (dt). This step function steps all objects forward in time by integration. The integration step just moves the objects forward by using their velocity, position and acceleration to determine their next position.

During the step collisions are detected and then resolved. Often times a broadphase of some sort is used to detect possible collisions, and expensive collisions operations are only used when really needed.

All of this organization allows the user of your physics engine to worry about three main operations: creating and removing bodies, and stepping the scene. The rest is automated and taken care of within the physic system’s internals.

The last major isolated system would be the broadphase. There are two major phases in collision detection: the broad and narrow phases. The narrow phase is the one which determines if two shapes intersect or not. The broad phase determines if two shapes can possibly be intersecting or not. The broadphase should be constructed such that intersection queries are very very fast and simple. An axis-aligned bounding box (AABB) will suffice perfectly.

Once the broadphase chooses which objects could perhaps be colliding, it sends them off to the narrow phase. The narrow phase performs much more intensive operations to determine if two objects are colliding or not. The whole point of this is to reduce the amount of times the narrow phase has to be used, as it is expensive to operate with.

Once the narrow phase finds a pair of bodies to be colliding information about the collision is gathered and placed into a small container called the manifold. Do not ask why it is called a manifold, for I have no idea and neither does anyone else seem to! The manifold contains three important pieces of information:

• Collision penetration depth
• Direction to resolve in
• Points of contact

These pieces of information are used to fill in formulas that are used to resolve the penetration and solve for a single unknown: the magnitude of the resolution vector. Here’s a small diagram:

c – Collision contact
n – Resolution normal
d – Penetration distance

It is also useful to to store pointers or handles to the two objects that formed this collision info. This allows some useful functions to placed into the manifold object directly: solve and resolve. Solve would be the function to collect the three pieces of collision information. If no contact points are found, then there is no collision. If contact points are found, then resolving performs a resolution operation to force both objects to not be intersecting after integration.

# Velocity

Complex physics manipulation is performed on the velocities of objects. Trying to manipulate the positions of objects directly is very difficult, as it poses a problem that isn’t linear. By using derived position equations for velocity, the problem is thus simplified. Most of the time we will be only dealing with velocity manipulation.

# Impulse Resolution in 2D (No Rotation or Friction)

The act of resolving collisions is something that isn’t covered very well on the internet. There are some scattered documents and details, though the information isn’t always easy to find. Most of what I know I learned by talking with other people, but I know most people will not have such an opportunity. Hopefully this article series can provide a strong resource for understanding and constructing a simple physics engine.

The toughest place to code is in my opinion resolution of collision. There exists tons of information on collision detection and broadphase, and thus creating these portions of a physics engine is in my opinion not too difficult. Some resources for collision detection are: Real-Time Collision Detection by Christer Ericson, and Game Physics Engine Developement by Ian Millington. I have both of these books right next to me as I write this :)

Generating contact manifolds and resolving such manifolds are what most programmers get caught up in. So lets hit the ground running and tackle a portion of code that will bring your entire physics system to life: contact resolution.

The best type of contact resolution to start with is impulse resolution. The idea behind impulse resolution is to take your contact manifold and solve for a single velocity vector that can be used to add into an object’s current velocity, in order to make it not penetrating the other object during the next frame. The reason for starting with impulse resolution is that it’s quite simple and easy to get up and running, and more complicated and advanced techniques require you to understand impulse resolution anyway.

2D impulse simulation.

Now the contact manifold is assumed to contain the direction of our velocity vector we are solving for. I will cover how to generate the contact manifold in another article in this series. The only unknown left to solve for is the magnitude of this vector. It so happens that it’s possible to solve for this magnitude in relation to both objects. If this magnitude is known, you add velocity along the normal scaled by the magnitude for one object, and you do the same operation to the other object in the direction opposite to the manifold normal. Lets start from the top.

We have two objects moving along in our scene, and there exists a relative velocity between the two, with object A as the reference object at the origin:

The relative velocity can be expressed in terms of the collision normal (from the collision manifold) with a dot product:

This can be thought of as the relative normal velocity between the two objects. The next thing to include in this derivation is the coefficient of restitution (elasticity factor). Newton’s Law of Restitution states that:

Often times the restitution identifier is specified by an e, or epsilon symbol. Knowing this it’s fairly simple to include it within our current equation:

Now we need to go to another topic and model an impulse. I have said the term “impulse” quite a few times without defining it, so here is the definition I use: an impulse is an instantaneous change in velocity. We will use an impulse to change the velocity of an object. Here’s how you could use an impulse to modify the velocity of a single object:

Here Impulse would be a scalar floating point value. This isn’t too useful however, as it only scales an object’s current velocity, and thusly makes the object move slower or faster along the positive or negative direction of the vector.

What is needed is a way to do component-wise modification of the vector, so we can make it point slightly in one direction or another, allowing objects to make turns and change directions.

In the above equation we can take a direction vector with a magnitude of 1, and scale it by our impulse. By adding this new vector to our velocity we can then modify the velocity in any way we wish. Our end goal is to solve for our Impulse scalar that will separate two objects from collision, so in the end we’ll need to distribute this scalar across two equations in terms of velocity.

Lets start with a simple momentum equation:

An impulse is defined to be a change in momentum. Thus we get:

To isolate our velocity after we can rearrange into:

Now lets change equation 4 into one that contains velocities under the influence of impulses. However we’ll want to express our VelocityNew as one that is acted upon by impulse, and substitute in equation 9:

Remember that the impulse is a scalar. Also note that all values on the right hand side of the equation are all known, including the Direction which was solved for by the collision detection.

All that is left here is to distribute this scalar impulse over each object’s velocity vector proportional to the total mass of the system (system being collision between both objects).

The total mass is MassA + MassB, so to get an even distribution you do: impulse * Mass / TotalMass. To simplify this one could use the following:

This can be done twice, once per object. The total impulse will be applied, except only a portion of the impulse will be applied to each object. This ensures smaller objects move more than larger ones during impact.

One thing you must ensure is that if the velocities are separating (objects moving away from one another) that you do nothing. Here’s a sample version of finalized code for impulse resolution in 2D without rotation or friction:

# Rotation

Now that we have covered resolution without these two factors adding them in to our final equation (equation 10) will be quite a bit simpler. We will need to understand the concept of “mass” in terms of rotations. The inertia tensor of an object describes how “hard it is to turn” along some axis. In 2D there’s only a single axis of rotation (along the z) so a single tensor is all that’s needed. Here’s a new version of equation 11.

For the sake of brevity here’s the final equation, where r is the vector from center of mass to a point on the body (contact point). The velocity of a point on the body relative to it’s center is given by:

Final equation (Direction substituted for n):

And there we have it! This will solve for a separating impulse given a specific contact point. Now you might have noticed there are a couple cross products that are kinda strange in 2D. I won’t cover what they are here, but they still hold true. I’ll just give them to you:

# Friction

Friction is the simplest thing to do in this entire resolution article, assuming we’re dealing with 2D! I actually haven’t derived this portion myself, so again I’m going to just throw the equations at you. Assuming we have our resolution direction in the manifold (the collision normal), we can calculate a tangent vector, which is a vector perpendicular to the collision normal. Just replace all instances of n in equation 14 with this tangent vector.

To solve for the tangent vector you do:

Again, just take the above TangentVector and replace it in equation 14 for n.

There is something I have missed. When solving for the force of friction there’s a max, which is the manifold normal * the coefficient of friction (from the body definition). This is due to the Coloumb friction law. Treat the coefficient of friction * normal as your “friction cap” when solving for your tangential impulse. This is just a simple capping of the final impulse vector.

# Penetration Resolution

Due to floating point error energy will always be lost from the system over time. As such, when objects start coming to a rest they will sink into each other due to gravity. How to solve? Well the solution is to actually just handle the leftover penetration manually by just moving the objects apart directly. This will prevent objects from sinking into one another, though doesn’t add any energy into the system.

To resolve penetration, simply move each object along the manifold normal in opposite directions. However there’s an art to doing so; you need to be gentle. If you just resolve 100% of the penetration each frame objects underneath other objects will jitter violently due to the naive penetration resolution scheme I am presenting. To avoid this, try only resolving a percentage of the leftover penetration, perhaps 20 to 80%.

Additionally you only want to resolve penetration if the penetration depth after the impulse is applied is above some constant arbitrary (but small) threshold, (try 0.01 to 0.1). If the penetration is below this, then don’t move either object.

This method of penetration resolution is called linear projection. Here’s a snippet of C++ code demonstrating this:

# Iterative Solving

There is one one additional tweak you can do to increase the believability of your physics simulation: iterate over all contacts and solve + resolve the impulses many times (perhaps 5 to 20 iterations). Since the large equation 14 has relative velocity within it, each iteration will feed in the previous result to come up with a new one.

This will allow the step to propagate energy throughout multiple objects contacting one another within a single timestep. This is essential for allowing the “billiards balls” effect to ensue.

This simple iteration is a very easy way to vastly improve the results of resolution.

• http://chrishecker.com/images/e/e7/Gdmphys3.pdf
• http://www.cs.cmu.edu/~baraff/sigcourse/notesd2.pdf
Share

## 19 thoughts on “Custom Physics Engine – Part 1: Impulse Resolution”

1. Vittorio Romeo

Hey, great article. Gamedevtuts brought me here.
I implemented my version of it and it mostly works, except the “Iterative Solving” part. Can you post your implementation of it?
Thanks.

1. Randy Gaul Post author

Thanks! Here, I hope this helps:

2. Vittorio Romeo

Thanks for the quick reply – unfortunately I’m still confused, as many of the methods’ implementations in your snippet are unknown to me. My code structure is very different and I can’t really correlate them.

Hope I’m not being too pretentious, but I’d really, really appreciate if you could post the entire engine source code (if that’s ok with you), either as an archive to download or a git (GitHub?) repository.

I’ve been working on a physics engine for quite a while and I would love too see how some things have been implemented in your own.

3. Josh

Not a bad intro to the topic! :)  I feel I should point out, however, that there are some issues with your mathematical language.  For example, your Eq. 12 shows division by InertiaTensor; you can’t do that.  Furthermore, just above that you say that in 2D there is only one tensor since you can only rotate around the z axis.  This is slightly sloppy language, since even in 3D you only ever deal with one inertia tensor per object.  What you want to say instead is that because you only deal with rotation along one axis, the inertia tensor reduces to just a scalar value.

4. namuol

Thanks for the guide; lots of practical advice!

I think I may be missing a few pieces, though. I apply a position correction, but objects still sink (a better word would be squeeze) into each other. They do not continue to sink — rather, the amount they “squeeze” into the object beneath them is proportional to the number of objects stacked on their top.

Could it have something to do with the order of things? Here is my approach:

This mostly works. It looks like I’ve got my impulse calculations right with zero gravity, and I get the same results with friction disabled.

I’m wondering if the only way to achieve that nice rigid stacking behavior is to take an iterative approach, which I’m trying to avoid. Thoughts?

Thanks again!

1. Randy Gaul Post author

That approach will do decently well, but it’s best to iterate over solutions. The best professional physics engines are iteratively solving these sorts of physics problems over the course of many frames. It gets so important to iteratively solve, that previous calculation can be used to jumpstart future calculations. This is called “warm starting”.

For your physics engine, if you want better stack you’ll need to do some tricks in order to get good stacking. This is because impulse resolution is a pretty naive resolution approach, and linear projection (the penetration correction I talk about in this post) is even more naive.

Feel free to email me with more specific questions!

5. Akshay

I am only a third way into it and it has already helped me clear many confusions regarding architecture. This and qu3e are a good resource!

6. Erlend

Hi!

I have a problem understanding how you deduced Eq. 10 given Eq. 4 and 9. I suppose ‘Direction’ here is what was formerly ‘ManifoldNormal’?

Is ‘VelocityRelativeAtoB’ just a substitute for ‘VelocityA – VelocityB’, which would make the right side equal to earlier, or is it implied that also these velocities undergo an impulse?

I’m also wondering what happened to the ‘ManifoldNormal’ on the left side, and why VelocityB is negative, but not its respective impulse vector.

Thanks!

7. Erlend

I found the answer to my confusions, I think. However, I still don’t know why VelocityB is negative. For anyone else being confused, here is a correction:

8. Lawrence

Hi Randy,

thanks for the cool blog.

In eq.10:

VelocityA + Impulse(Direction) / MassA – VelocityB + Impulse(Direction) / MassB = -Restitution(VelocityRelativeAtoB) * Direction

does VelocityA and VelocityB refer to the “old” velocity (before collision) and VelocityRelativeAtoB the “new” velocity (after collision)?
Cheers

1. Randy Gaul Post author

Almost. VelocityA is the velocity of body A. VelocityB is the velocity of body B. AtoB implies vector subtraction. To find a vector that goes from A to B we do: endpoint – startpoint. A to B means we start at A and end at B, so we do: B – A. So, VelocityRelativeAtoB is VelocityB – VelocityA.

9. I

Hi Randy.

Thanks for this Tutorial, but I have two questions.

1.
In Eq 1 you write ‘VelocityRelative = VelocityA – VelocityB’, but I think you mean ‘VelocityRelative = VelocityB – VelocityA’. Right ?

2.
What do you mean with ‘Collision Normal’ and how I calculate it ?
Do you mean that I have to get the vector from A-Position to B-Position and build then one of the normals of it ?

1. I

I have read your articles on tutsplus. You say that we need the collision normal and the penetration depth, but you don’t explain how to get the collision normal. Or am I blind ? :O

2. I

Ahh…I found it. :D
But why don’t you explain this before you use this normal ?
I think it would be better when you explain it step by step. Now it’s very confusing and make it hard to follow you.

10. Joseph Sigbeku

Hey Randy

I’ve been working on a physics engine in java for a while now. For some reason I can never seem to get rotation correct. I’ve tried so many ways. Could you look at this code and tell me what I’m doing wrong?

This is the code for managing the collision response between a ball and the floor.
Am I right in taking r to be the vector from the center of the ball to the point of collison?
Am I also right in taking n to be the same vector but in the opposite direction since its the collision normal?
p is the point of collision btw

Could you tell me what I’m calculating wrong. I’m using the equations from: http://www.randygaul.net/2013/03/27/game-physics-engine-part-1-impulse-resolution/
I would appreciate your help so much. I’ve been stuck on this for months and I have no clue what I’m doing wrong.

vector velocity = new vector(getSpeedX(),getSpeedY());

vector r = new vector(new Point(xLoc,yLoc),p);
System.out.println(“R: ” + r);
new Edge(new Point(xLoc,yLoc),p).drawVector2(g); //drawing to check if vector is correct
vector normal = vector.unitVector(new vector(p,new Point(xLoc,yLoc)));
System.out.println(“normal: ” + normal);
float eq = (1+getRest())*-1;
vector wxr = vector.crossScalar(getAngularVelocity(),r);
vector vAP = wxr.Add(velocity);
System.out.println(“CURRENT LINEAR VELOCITY: ” + velocity);
System.out.println(“LINEAR VELOCITY OF COLLISION POINT: ” + vAP);

if (vector.dot(vAP, normal)>=0) {
System.out.println(“moving appart”);
return;
}

float numer = eq * vector.dot(vAP, normal);
float ex0 = 1/getMass();
float rxn = vector.cross(r,normal);
System.out.println(“CROSS PRODUCT OF R AND NORMAL: ” + rxn);

float ex1 = (float) (rxn*rxn)/getmomentOfInertia();
float denominator = ex0 + ex1;

float impulseJ = numer/denominator;

float axspeed=(float) (getSpeedX()+((impulseJ/getMass())*normal.getI()));
float ayspeed=(float) (getSpeedY()+((impulseJ/getMass())*normal.getJ()));

setSpeedX(axspeed);
setSpeedY(ayspeed);
setAngularVelocity(getAngularVelocity() + vector.cross(r,vector.multiply(impulseJ, normal))/getmomentOfInertia());

1. Randy Gaul Post author

Sorry, I don’t have time to debug anyone’s code. My suggestion would be to use a line rendering and visually debug different mathematic expressions. Debug render your vectors, cross product results, or other quantities. Also there exists a few different open source solutions that you can compare your code against. This should help you figure out what is or isn’t working.

11. Dan

For some weird reason my objects just refuse to rotate unless I alter the formula of the impulse calculation from RAcrossN * RAcrossN to just RAcrossN (and the same comes for B)… Any ideas? Thanks for this tutorial btw.