# SLERP questions

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Good morning folks, I have some questions today about the usage of quaternions and SLERPing. Currently I use quaternions to represent the orientation of my 3D objects in DirectX, which I modify by rotating the quaternion some number of radians around the 3 basic dimensions. This seems to work fairly well. What I have heard though, is that the best way to transition the quaternion from one orientation to another is to SLERP. The only thing I really know about SLERP is that it means "Spherical Linear Interpolation", and that it is a way of producing smooth transitions. How is this done? What exactly are the benefits of SLERPing over time, as opposed to just rotating on an axis by small amounts over time? I had heard that SLERPing is the real reason why the quaternion representation for orientation is better than using an absolute rotation around an axis, and I'd like to know more about it. Thanks much for anyone who can lend insight =) Mike

##### Share on other sites
Good mörning!

When "slering" from one quaternion to another it is the same as taking the shortest rotation from one rotational frame to another. If you are interpolating euler angels it will produce a more "curbed" rotation. Even worse, you can get gimal locks.

Från wikipedia:
"gimbal lock is caused by the alignment of two of the three gimbals together so that one of the rotation references (pitch/yaw/roll, often yaw) is cancelled."

An advantage of using quaternions over matrices is that it takes less space in memory, and is comutationally less intesive to normalize. Normalization of quaternions and matrices is used to get rid of errors that normaly accumulate at each timestep when updating the rotation a little bit each frame.

Tha downside of using quaternions is that they need to be converted into a matrix before transforming any vertices (not realy need, but it is what you want to do).

Try googleing for slerp, quaternion and gimbal lock.

or checkout wikipedia at:
http://en.wikipedia.org/wiki/Gimbal_lock
http://en.wikipedia.org/wiki/Slerp
http://en.wikipedia.org/wiki/Quaternion

##### Share on other sites
Quote:
 Original post by Michael KronHow is this done? What exactly are the benefits of SLERPing over time, as opposed to just rotating on an axis by small amounts over time?

Slerp is effectively the same as rotating around an axis. The benefit of slerp is that determining the axis and the angle is expensive, but it is unnecessary when using slerp.

##### Share on other sites
Hmm. So can you give me an example of how I might use it? It seems that the usage is pretty straightforward for something like, "object A has orientation Q, and I want it to have the arbitrary orientation Q' in 2 seconds". Then you just SLERP from Q to Q' over 2 seconds right? Pretty intuitive.

But what about SLERPing based on player input? If I want this model to rotate while the player holds down directional keys, what am I SLERPing "to" exactly? There is no predefined quaternion here that I can use as a Q'. My instinct would be to just rotate the quaternion a small amount on one the desired axis every frame, until the button is let up. How would this be done with SLERPing, or is SLERPing not a good solution for this situation?

Thanks,
Mike

##### Share on other sites
Yeah, there you won't be SLERPing; you'll simply be modifying the quaternion by a small amount whenever the "turn" button is held.

##### Share on other sites
SLERPing, in my experience, which is often wrong, is more used for AI controlled entities that need to rotate to face a player, or scripted sequences, or with a (scripted) camera.

##### Share on other sites
Hmm, ok. So where do you run into this SLERP-friendly situation? Off the top of my head I can think of a scenario in a spaceship game: you want an AI controlled ship to turn to face an enemy ship in a smooth transition over time. So you figure out what your final quaternion orientation would be when facing the enemy, and then you SLERP that ship from it's current quaternion to the final quaternion over time.

Are there any other common examples? Do most all of them come from free-space flying, or are there practical appliations for earthbound models as well?

Thanks again,
Mike

##### Share on other sites
Slerping, in my experience, is mostly used for animation rather than motion. Skeletal animation, for instance, makes use of it. (Spherical quadratic interpolation, or SQUAD, is also used there.)

##### Share on other sites
Cool, that makse sense. There are a lot of parts of the body that need to do some pretty free-form rotations now that I think about it.

So out of curiosity, how does one acquire the quaternions for SLERPing "to"? Take the example of one entity facing another. It's pretty easy to get a vector representing direction from point A to point B in 3D space, but how would you get a quaternion? My initial thinking is: you just subtract that vector from your initial facing vector and then multiply by PI/2 for two of the rotation angles. Then you use some "standard" rotation for whatever axis you lost due to Gimbal Lock. ( the axis your model faces down initially )

Is there a better way to construct a quaternion that represents one point facing another?

##### Share on other sites
Quote:
 So out of curiosity, how does one acquire the quaternions for SLERPing "to"? Take the example of one entity facing another. It's pretty easy to get a vector representing direction from point A to point B in 3D space, but how would you get a quaternion? My initial thinking is: you just subtract that vector from your initial facing vector and then multiply by PI/2 for two of the rotation angles. Then you use some "standard" rotation for whatever axis you lost due to Gimbal Lock. ( the axis your model faces down initially )Is there a better way to construct a quaternion that represents one point facing another?
One method for finding the goal quaternion that aims at a target is to create a quaternion that rotates your current forward axis onto the position-to-target vector, and multiply it with your current quaternion. All you need is a function that creates a quaternion to rotate one vector onto another. There are various algorithms for this. One that's easy to implement (but requires the input vectors to be unit-length) is:

// a and c are input vectors
Vector3 b = normalize(a+c);
quat.v = a.Cross(b);
quat.w = a.Dot(b);

I might have gotten the cross product terms backwards - I'm not sure. In any case, whatever algorithm you use will fail when the input vectors are directly opposed, so you have to handle that case separately.

(Just as a caveat, this isn't the best algorithm IMO. But it's fine for unit-length input.)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 27
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634098
• Total Posts
3015525
×