Physics

The first step for any collision to occur is that you have to have things, and those things have to be moving. In order to enable this we need to take a look at some Physics. Physics turns out to be a huge subject. When I first started writing my own game I thought "what's the big deal, I know v=d/t, why is that so hard?" Well it turned out to be much more complex then that. I spent a long time and read many books and articles and in the end I got it all working and realized... that it really isn't that big a deal. In this page I'll lay out what you need to know in order to understand the physics in the REngine.

Vectors:

In order to understand all of the motion in the REngine, you're going to need to understand some basic vector math. At this point you should already be comfortable with using vectors to describe linear motion. I'm going to try to get you comfortable with vectors representing rotational motion as well. This second part took me a long time to get comfortable with, so I'm hoping to speed up the process for you.

The first step is to define a vector. A vector is simply 3 numbers that represent a direction in the x, y, and z planes. A vector has a length, and it has a direction, and that's about all it has. Of course there is a lot of math you can do with vectors and you'll have to get comfortable with that if you plan on writing your own Physics Engine, but for now we just want a general feel for how vectors work.

In the linear sense vectors are pretty straighforward. If I'm moving only along the x axis at 5 m/s then my velocity can be expressed as a vector (5,0,0). If I then start moving along the y axis at 1 m/s, or (0,1,0) I now have 2 velocities. It's hard to keep track of them individualy, but you can just add them together and get (5,1,0). If I decide to double my speed, that's simply (10,2,0). This means I'm still going in the same direction, but the length of my vector (which is my speed) has now doubled. This simple idea can be applied to velocity, acceleration, momentum, etc etc. It should be pretty obvious to you.

What might not be so obvious is the application of vectors to rotational motion. The idea now is that the dirction of the vector is the axis which you are rotating around, and the length of the vector is the amount of the rotation. It doesn't seem so complex, but it took me a long time to come to terms with it. If I'm sitting in a desk chair and I suddenly start spinning around, I am spinning around the positive Y axis (0,1,0). If I'm doing one complete revolution (2 pi radians) every second, then my rotational velocity can be represented as a vector (0,2pi,0). If I slow down to half that speed (pi radians) then my vector is (0,pi,0). This starts to get really handy if I need to start adding other rotations to it. Let's say that while I'm spinning I start rocking the chair backward. This would be a rotation around my x axis. If I do it at pi/2 per second then this new rotation is (pi/2, 0, 0). To try and picture both of these rotations in my head is sort of difficult for me, however I can easily add the vectors together and get my new rotation vector, (pi/2, pi, 0). It's not very hard for me to imagine a vector in space that points in this direction (sort of diagonal up and to one side) and then it's not much of a leap to now consider what it would look like to rotate around this new vector. This new rotation is how I am rotating as I sping my chair and lean back. If you can get used to thinking about rotations that way, the physics of rotation starts to get a lot simpler.

At this point you should be feeling comfortable with vectors to represent linear and rotational motion. I'm going to be using them pretty much constantly. If they are still a mystery then you will want to check out the web for further reading.

The Two Halves:

When it comes to describing how an object moves and reacts to the world around it, there is really one set of rules, that you apply to two different kinds of motion, linear and rotational. If you keep this in mind it makes keeping track of what's going on a lot simpler. Or in my case, if you can get your head around linear motion (which is pretty cut and dry) then you can apply those ideas directly to rotational motion (which is sort of mysterious). So the next section will lay out how things apply to linear motion, and then the section after that will lay out the same stuff, only for rotational motion.

Mass, Velocity, and Force:

In my physics engine these are the three basic quantities of linear motion. In reading the literature I was often confused because there are some other things out there (acceleration and momentum to name two) which are often used. I sat down with a pencil and paper and with a lot of head scratching managed to fully convince myself that with a handful of formulas you can move easily from one set of measurements to the next (for example given mass and velocity you can find momentum). Once I was comfortable that I could move between measurements with ease, I decided to choose the smallest set of things that I was comfortable with. I chose these three (and to tell the truth, I still have trouble figuring out what momentum really is in a physical sense). So let's take a quick look at each one of them

Mass is simply a measurement of how much stuff is in something. In gravity it's easy to equate it with weight, but that's not really correct because an object with the same mass will weigh different amounts in different gravities. Also, since I'm making a physics engine for a space simulator, weight is meaningless. However mass is still very important in space. A really massive ship will have to use bigger rockets to move it then a ship with little mass. Likewise if you get hit with a massive object it's gonna hurt more then getting hit with am object of less mass (think baseball versus ping pong ball here).

Velocity is the speed and direction in which you're moving. It's important not to confuse it with speed, which has no directional part. It's appropriate to say my speed is 10 km/h, whereas to describe velocity you have to say 10km/h in which direction. Velocity is represented with vectors where the direction is defined by the X, Y, and Z components, and the length of the vector is the speed. If we define one unit to be one km/h, then a vector with X=1, Y=0, and Z=0 is said to be moving one km/h in the positive X direction. It's moving along the same path as an object that has X=10, Y=0, and Z=0, however it is moving slower.

Force is something that moves mass. For example if I turn the thrusters on in my space ship they are applying a force, which causes me to move. Force is also measured with a vector, which means that it also has direction associated with it. If I turn on my main thrusters, and a small retro-rocket I now have 2 forces which are pushing me in different directions. The sum of those forces can be figured out by simply adding the 2 vectors together.

Inertial Tensor, Rotational Velocity, and Torque:

As I said before, these three things are actually the same as Mass, Velocity, and Force only instead of describing how an object moves in a straight line, they describe how it spins around. Also there are rotational equivalents of all the other linear things in physics too. Much like with the linear stuff, I chose the three things I was most comfortable with, and stuck with them. In a way they sort of scare me, however by trying to think of them in the same way I think of their linear equivalents, they aren't such a big deal.

An inertial tensor is a tricky object. For one thing it is represented as a Matrix, which is a pretty mysterious thing to me. Consider a heavy door. A door is an object that has mass and rotates around an axis (the hinge). Depending on how heavy (or how massive) the door is it will be easier of harder to open. While this is simple to grasp it only gives us a feel for mass, and not for the inertial tensor. Consider now where you put your hands when you want to open a door. If you're not an idiot you will put your hands as far from the hinge as you can (where the handle tends to be). This is where the door is easiest to open. If you try to open a door by pressing really close to the hinge, it will be much harder to open. This description of where the mass is in the object is the essence of the inertial tensor. To expand on the idea a bit, imagine if the door you're pushing on is made of solid steel. It will open very differently then if the door is made with the left side steel and the right side wood. The inertial tensor describes this distribution of mass. You need it in a physics engine because it helps describe how things turn when forces are applied. Unfortunately they are computationally intensive to figure out, so I cheat by just describing everything in my engine as a uniform sphere. This speeds things up considerably, however you can see it behave incorrectly if you try to treat a flat plank as if it where a sphere.

I touched on Rotational Velocity in my section about vectors, but it's good to flesh it out a bit here. Rotational velocity is how fast you're spinning, and which axis you're spinning around. I described the motion of sitting in a desk chair and spinning it around, and then leaning back. For a space fighter game you want to expand that idea a bit to consider how a space ship can spin. It can spin around its Y axis, turning left and right like a car does (this is called "yaw"). It can also put its nose up and down by rotating around the X axis to climb or fall (this is "pitch"). Lastly it can spin around its Z axis to perform a barrel roll (this is the "roll"). You can see that the three axis that we can rotate around can all be described by vectors, which means we can add them and manipulate them and generally turn at different speeds. This is all there is to rotational velocity

Just as you can apply a force to move yourself forward or backward, you can apply a torque to change how fast you're spinning. What makes torque a little harder to think about is that where you apply the torque is important. Think about that heavy door again. When you apply the torque to the handle the door opens easily. When you apply the torque near the hinge the door feels much heavier. You may be pushing just as hard in both cases, but the door will open easier if you keep your hand on the handle. This idea of position is included when you work with the torque. This position is included in the formulas that deal with torque, so you don't have to think too hard about it. The simplification is that torque is related to rotational velocity in the same way that force is related to linear velocity.

Impulse:

The preceeding discussion of the three things dealing with linear and rotational motion are all valid, but they miss something. All of the formulas that I use deal with things changing over time. For example when you put your foot on the gas you will speed up, but it will take time. What happens when you drive into a wall? Your velocity changes... extremely quickly. We need something to describe this instantaneous change in velocity, and for that we have impulses. Impulses are sort of like physics that happens in zero time. If you're flying your spaceship along minding your own business and suddenly someone shoots you in the wing, your ship will immediately react. Its linear motion will change, and it will start to spin. This tells us that just like all the preeceding discussion, impulses have a linear part and a rotational part that are sort of the same. Luckily for us, the physics of impulses are pretty simple. When a collision happens you apply some rather nasty looking formula and you get a value for the impulse. Two quick formulas later and you have a linear impulse velocity, and a rotational impulse velocity. Simply add those to your existing velocities and you're all done.

Physics summation:

Okay what has this page described? I presented the basics of the math you need to understand. Then I showed you the stuff that makes things move, both in a straight line, and rotating. Lastly I showed you what happens when you suddenly get hit (which is really important in a collision response system). I hope you are comfortable putting all those things together into a mental picture of how to move ships around. So now what does our collision detection and response look like? It comes down to a few simple steps: That's really all there is to it. Armed with this basic knowledge you should be able to understand how things move around, and how to handle the situation when two things collide. This brings us to the larger topic of how to figure out that two things have collided.

An introduction to Collision Detection...