"Imagination is more important than knowledge." -Albert Einstein

Elegant camera rotation using Quaternions in Papervision3D

Posted: September 24th, 2009 | Author: admin | Filed under: papervision3D, tips and tricks | No Comments »

Quaternions sound really scary… and I admit, they are. I don’t pretend to even begin to understand the math behind it, but using Quaternions in your code will make transitions look much nicer! It is something really helpful if you’re moving your camera around in Papervision3D to match objects’ rotation.

here’s an example that shows the difference between normal approach and the quaternion one:

Picture 1

source

so — what are quaternions?? you can read about it in Wikipedia, but for our purposes, we’re using it to calculate the SHORTEST route to a certain rotation (on all axises). we basically take the beginning angle, end angle, assign a SLERP value (SLERP = spherical linear interpolation) and tween it!

It’s really not that much when it comes down to it. here’s what the code should look like:

private function calculateQuaternions() : void
{
    camTarget.extra = {slerp:0};
    startQuaternion = Quaternion.createFromMatrix(camTarget.transform);
    endQuaternion = Quaternion.createFromMatrix(_selectedPlane.transform);
    TweenMax.to(camTarget.extra, .5, {slerp:1, ease:Quad.easeInOut, onUpdate:slerpMe});
}

private function slerpMe():void
{
    var quaternion:Quaternion = Quaternion.slerp(startQuaternion, endQuaternion, camTarget.extra.slerp);
    camTarget.transform.copy3x3(quaternion.matrix);
}

SOMETHING TO LOOK OUT FOR — it you’re scaling the object you’re copying the transformation from the quaternion calculation will be incorrect.

There are other approaches to this, but I think it’s the simplest one. Here is a good post about using it for an object rather than the camera.



Leave a Reply