Improve Rotation

Hello Guys,



I’m looking for a way to calculate a smooth rotation.



So far I got this:



Quaternion currentRotation = owner.getLocalRotation().clone();

owner.lookAt(targetPosition, Vector3f.UNIT_Y);

Quaternion targetRotation = owner.getLocalRotation();

Quaternion interpolatedRotation = targetRotation.slerp(currentRotation, targetRotation, 0.1f);

owner.setLocalRotation(interpolatedRotation);



I am not satisfied with that solution: The performance is bad and it seems to me that it’s not that smooth. Any suggestions how to improve that piece of code?

You should probably add tpf here:

[java]

targetRotation.slerp(currentRotation, targetRotation, tpf*rotSpeed);

[/java]

For performance you could maybe find the Vector3f direction the owner spatial is looking by subtracting targetPosition with owner position, and then use quaternion.lookat(direction,up) to get the targetRotation (direction is normalized in Quaternion, so no need to do that). lookat in class spatial does a lot of unnecessary calculations for your use.

[java]

public void lookAt(Vector3f position, Vector3f upVector) {

Vector3f worldTranslation = getWorldTranslation();



TempVars vars = TempVars.get();



Vector3f compVecA = vars.vect4;

vars.release();



compVecA.set(position).subtractLocal(worldTranslation);

getLocalRotation().lookAt(compVecA, upVector);



setTransformRefresh();

}

[/java]

are you doing that all in an update() ? if so you should just calculate the beginning and end rotations once outside. Then in the update() use slerp() and setlocalrotation()