Category Archives: Mini-courses

How a Kalman filter works, in pictures

I have to tell you about the Kalman filter, because what it does is pretty damn amazing.

Surprisingly few software engineers and scientists seem to know about it, and that makes me sad because it is such a general and powerful tool for combining information in the presence of uncertainty. At times its ability to extract accurate information seems almost magical— and if it sounds like I’m talking this up too much, then take a look at this previously posted video where I demonstrate a Kalman filter figuring out the orientation of a free-floating body by looking at its velocity. Totally neat!

What is it?

You can use a Kalman filter in any place where you have uncertain information about some dynamic system, and you can make an educated guess about what the system is going to do next. Even if messy reality comes along and interferes with the clean motion you guessed about, the Kalman filter will often do a very good job of figuring out what actually happened. And it can take advantage of correlations between crazy phenomena that you maybe wouldn’t have thought to exploit!

Kalman filters are ideal for systems which are continuously changing. They have the Continue reading

3D Rotational Dynamics 1: The Basics

If you’re writing a 3D game, or a VFX simulator, or even trying to solve a 3D kinematics equation, you need to be able to compute how objects react to forces; in particular how they will spin. The mathematics of spinning objects can be quite complicated and subtle, and in this series I will attempt to make the concepts clear.

Let’s start with 2D spinning objects

If you’ve taken high school or college physics, you’ve probably somewhat familiar with how rotation works about a fixed axis: Apply a little bit of torque (twisting force), and the object starts to spin. Apply the opposite torque, and it slows down until it reverses. This animation from Wikipedia shows it nicely enough:

TorqueA little key for what the arrows stand for:

Symbol Meaning
r Direction from the axis to the point of applied force
F Applied (linear) force
τ Torque (twisting force)
p Linear momentum
L Angular (rotational) momentum

Wait, why are we using vectors

You’re right; In 2D, we don’t really need vectors; we could just use numbers. But since we’ll be moving on to 3D, let’s get used to our quantities being vector-valued.

Moving on

This is a little abstract, so let’s use a more concrete example. Since this is the internet, what better way to illustrate this than with a cat gif?

cat_roll_gifHere, the cat is making the toilet paper roll spin— and he’s doing it by applying a downward force with his paw. This downward force is converted to twisting force— torque— by the fact that the toilet roll can spin freely about a fixed axis, but not move freely in space (otherwise, he’d be batting it across the floor).

We compute the amount of twisting force that Mittens here is applying by taking the cross product of the downward force F with a vector r connecting the cat’s paw to the toilet paper axis:

τ = rF

What this means: Twisting force is stronger with more force, and longer lever arm. The axis of twisting force is perpendicular to the force applied.

cat_torqueNote that for a fixed axis, we must take the part of the force vector which is orthogonal to the axis. Since the roll is held in place from left to right, there can be no net force along this direction. We may need to adjust an applied force to ensure this:

Forthogonal = F – (F · â) â
(â is the unit-length fixed axis)

roll_orthoFrom the above equations, it may also be apparent that the torque vector is always aligned with the rotation axis, since F and r are defined to be perpendicular to it. (This is not so for free rotation, but we will get to that later). The length of the torque vector shows how much twisting force is being applied, and therefore how quickly the toilet roll is going to spin up.

Using the knowledge that our vectors are perpendicular, we can come up with a simplified scalar version of τ = rF. We’ll also make use of the identity that |a x b| = |a||b|sin(θ):

τ = rF
|τ| = |r||F|sin(90°)
|τ| = |r||F|

Intuitively, this should all make some sense: You can get more twisting force by using a wrench with a long handle, or better yet, a cheater bar (which is a length of pipe you use to extend a wrench handle), when compared to using your hand. The wrench has much longer r than the bolt you’d be grabbing.

The integral of torque is the angular momentum, which is a vector describing how the mass of the object is currently spinning. It is aligned with the axis about which the mass is spinning.

L(t) = ∫τ(t) dt

What this means:  The longer you apply torque, the more rotational inertia your object will have.

After Mittens has applied some torque for a short moment, that integral will be nonzero, and the toilet roll will be spinning:

cat_LIn the absence of friction, the inertia of the roll will keep it spinning indefinitely. In reality, the roll will rub against the mount and come to a stop after a short time. This tells us that friction can exert torque too!

OK, so that’s great, but if we were coding a game (a cat toilet-roll simulator? See you at the top of the casual gaming charts), how would you compute the position of the toilet roll at each instant of time? Put another way, if I know how hard Mittens/the player swats the roll, and how for how long, can we figure out how much toilet paper has unrolled?

For a fixed rotation axis (like our toilet roll mounted to the wall), the connection between angular momentum and rotation rate ω is pretty simple: There is a factor which relates the two called the moment of inertia, I, which describes the object’s resistance to being spun:

ω(t) = dθ / dt = |L(t)| / I     (2D rotation)

What this means: The rate of rotation of the toilet roll is proportional to the length of the angular momentum vector. The larger the moment of inertia, the more slowly the roll will spin.

xx4Hetb

It’s harder for a heavy thing to get rolling.

The moment of inertia is computed based on the mass of the object and its distribution about the axis of rotation. More on this later.

From this we can very easily figure out the absolute angle by integrating the rate of rotation:

θ(t) = ∫ω(t) dt

 What this means: The rotation angle at any point in time is the cumulative total of the rate of rotation up to that moment.

To this I will add two more equations involving the angular acceleration, α:

α(t) = d/dt ω(t)
τ(t) = I α(t)

Putting it all together

Now we have enough to make a complete picture of 2D rotation:2D-rotation-flowchartFrom this diagram, you can figure out how to relate any aspect of rotation to any other at a glance.

I find this much easier to understand and navigate than a set of written equations; this way it’s clear how the information flows. You could start at the bottom with information about torque, work your way up, and figure out how the object is oriented; or you could start at the top with a changing angle, work downwards, and figure out what the torque is. Because there are some redundant equations, you even have a choice of paths/methods!

Neat!

But all of this is useless for 3D

OK, well, not useless. It’s really pretty important that all the above makes sense so that we can understand things when we get to 3D. And believe me, dear reader, it gets crazy. Take a look at the video, taken on the International Space Station, which inspired me to try and simulate 3D rotational dynamics in the first place:

What. The. Hell.

What’s going on here? I assure you that there is nothing special about that handle; there are no tiny rockets on it, or fluid inside, or anything else weird. It’s just a T-shaped thing. Nothing is touching it to make it flip like that, so there cannot be any torque. What the hell is happening?

I’ll give a little preview of things to come: This phenomenon is called torque-free precession. You rarely see it on Earth because most of the spinny-things you deal with from day to day are fixed to axles, and are governed by the simple equations above.

More complicated cases like spinning tops still have some amount of torque applied, because they are resting on a surface like a table, and the weight of the top tries to tip it over. Pretty much only in free fall or zero-G can you see something precisely like the above, so not surprisingly, we Earth-bound humans have zero intuition for it.

To begin understanding how we can simulate the physics of the T-handle, we’ll have to wait for the next installment!