I’m reworking the autopilot class of my game and I was thinking it would be nice to have a slerp function for spatials, so this is what I came up with. Nothing too exciting really.

This in itself is only a utility method but since Spatial has others, like lookAt, I thought it might be a good idea to also have a slerp. I think it would a good idea to have this since it auto-updates itself whereas if you want to do it the “old way” you have to make sure to apply the slerp rotation back to the spatial so the transformation is refreshed. This new method does it for you.

Spatial.java

[java] /**

<code>slerp</code> sets the values of this quaternion to the slerp from

itself to rotator by amount then refreshes itself.

*

@param rotator The Quaternion representing the final interpolation.

@param amount The amount difference.

*/

public void slerp(Quaternion rotator, float amount) {

I’m wondering when this comes up. Usually with a slerp you have a start rotation and an end rotation and then you linearly interpolate between them. 0 being all the first rotation, 0.5 being halfway, 1 being all the second rotation, etc…

With this way, the starting rotation is always moving so how do you keep track of how much to slerp? ie: what do you pass in for amount? And presumably rotator is also moving?

Starts at the spatial’s own rotation quaternion and “ends” at the rotator’s quaternion with a stepping of amount. That would actually rotate the spatial by the amount.

It’s actually the same thing I use in my autopilot class.

But the problem is that the spatial’s own rotation keeps changing. So you get some kind of weird curve.

For example, let’s say the spatial starts at 0 rotation and rotator is 90 degrees and you are going to step amount by 0.1… so 0.1 then 0.2, etc.

Normal slerping would end up with degrees: 0, 9, 18, 27, 36, and so on.

This slerping would end up with 0, 9, 25.2, 44.64, etc.

And if you are just passing the same amount every time (say 0.1) then you get a weird curve the other way since 0.1 represents a smaller amount each time.

0, 9, 17.1, 24.39, and so on.

So while you may be seeing an effect that you desire, it’s usually a mistake on the user’s part to interpolate this way when really they mean to actually linearly interpolate between a starting and ending value.