Can someone help me understand quaternions and slerp?

Hi im trying to make a 3D version of snake and from what I’ve researched quaternions and slerp are the best way to make precise rotations but i haven’t been able to find out how to use them. If someone could help me out with this that would be great!

Slerp means spherical linear interpolation. That’s part of the magic of quaternions because with slerp you can compute a quaternion from 2 other quaternions and a value from 0 to 1.
Meaning you can easily interpolate between 2 rotations.
Not sure why you think this makes quaternions more precise or how it can be involved in a snake game where you basically turn 90° everytime.

So what is it you really want to do?

What I have so far is a node filled with cubes that make a larger cube and these cubes will change color depending on the location of the snake and items. I want to make it so that the cube smoothly rotates when the snake moves from one side of the cube to another. I have tried this with the rotation method but I can’t make it stop precisely at 90 degrees because of the speed I want it to rotate and the tpf value. So when I googled what was the best way to rotate a node exactly 90 that was the first thing I saw but if you could tell me an easier way to do this that would be great too. Sorry this is a pretty simple thing that’s troubling me its just that I have recently started programming with 3D stuff and I don’t really know a lot yet.

ok, so you want a rotation animation. So indeed slerp is useful for that.
What you have to do is to make a control for your cube.
take the current rotation of the cube, and the target rotation you want.
choose a duration (like 500ms for example).
When the animation start on each frame, accumulate the tpf. like

time += tpf;

(time being a float)
and compute the interpolation factor from it

float factor = time / duration; //<-- this will give you a value from 0 to 1.

then use this factor to compute the cube rotation.

curentRotation.slerp( startRotation, targetRotation, factor);

then you can use currentRotation for the cube.

Note that we also have a nlerp method that does a slightly different calculation but is faster. It has its drawback though because it may result in non constant speed. But it worth the try.

2 Likes

Thank you so much! I’ll try out both slerp and nlerp.