Quaternions - fixing orientation to a single plane

Started by
1 comment, last by dangerdaveCS 14 years, 1 month ago
Hey all. I'm trying to ensure that my little critters are always the right way up, despite what the physics engine says. So initially the agent's 'up' vector is, for example, (0,1,0), then the physics engine (bullet) runs, and the up direction can be modified. I want to force the orientation to ensure that up is maintained as (0,1,0), but the component of the orientation within the plane is unchanged. Since I'm using Ogre/Bullet, all orientations are represented as quaternions, so I need a method of taking an object's orientation quaternion and transforming it via other quaternions to force it down to a rotation in the plane. My knowledge of quaternions is barely novice level, I just know how to use them in the simplest cases, and don't have any proper grasp of how they work. Any help much appreciated,
Dave.
Advertisement
Would it be possible to enforce the constraints via Bullet somehow? I only have a little experience with Bullet, but I know in some other 3-d physics libraries it's possible to create the effect of strictly 2-d dynamics. (Or, you could just use a 2-d physics engine, such as Box2D.)

If you want to correct the orientation manually though, it's easy to do. What you want to do is apply a rotation to the orientation quaternion that rotates the local up vector onto the fixed world up vector.

The local up vector can be computed by converting the orientation quaternion to a matrix and then extracting the up vector from the appropriate row or column (which row or column depends on which axis is up in the object's local space). Once you have the local and world up vectors, you compute the quaternion that will rotate the local up vector onto the world up vector over the shortest arc and apply it to the orientation quaternion (at which point you should renormalize the quaternion to prevent numerical drift).

Anyway, that's the short version. This actually gets discussed quite a bit around here, so if you search the forum archives for (e.g.) 'align to normal' or 'align to vector', you should find some informative threads on the subject. If you get stuck though, post back, and I or someone else should be able to describe the process in a bit more detail.
Thanks for the reply. Extracting the local up vector from the transform via a matrix row was exactly what I needed. Plus those search terms you gave me turned up some useful results in google. Seems there a few different methods, some more 'correct' than others (e.g. using a custom constraint in the physics engine), but I've just stuck with a straight quaternion transform.

Thanks again.
Dave.

This topic is closed to new replies.

Advertisement