## How to move to quaternion implementation ?

Jun 19, 2012 at 5:40pm

# How to move to quaternion implementation ?

Hello here,

now I have made my basic prototype (it works fine, globally : http://www.youtube.com/watch?v=ZCexHD297O0), I need to move to quaternion.

Indeed, and as dtr named it has coordinates singularity, I have some azimuth calculations problem.
I read some people implemented that exactly as I did.
Indeed, it isn’t really annoying to have sounds flipping a bit when the sound source is exactly up above or under the camera (considering the lookat direction of the cam)

but I don’t like that.

I implemented my azimuth (and elevation) calculations like that:
- I have a huge matrix with x, y, z coordinates of all objects.
- I process the whole matrix through a jit.gen

The jit.gen takes as inputs:
- x,y,z for all objects
- cam position (x,y,z)
- cam lookat point (x,y,z)

I need that 4th dimension to avoid that Gimbal lock :-/

What do I need more to improve my calculation …?

Any helps or leads would be appreciate :)

#46095
Jun 19, 2012 at 7:39pm

oops for the bad img tag…

[attachment=197035,4063]

###### Attachments:
#166063
Jun 20, 2012 at 10:30am

as far as I understood, http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm can help.

In that link, there is a non-normalised version of the cod which calculates heading from quaternions.

``` /** q1 can be non-normalised quaternion */```

public void set(Quat4d q1) {
double sqw = q1.w*q1.w;
double sqx = q1.x*q1.x;
double sqy = q1.y*q1.y;
double sqz = q1.z*q1.z;
double unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
double test = q1.x*q1.y + q1.z*q1.w;
if (test > 0.499*unit) { // singularity at north pole
heading = 2 * atan2(q1.x,q1.w);
attitude = Math.PI/2;
bank = 0;
return;
}
if (test < -0.499*unit) { // singularity at south pole
heading = -2 * atan2(q1.x,q1.w);
attitude = -Math.PI/2;
bank = 0;
return;
}
heading = atan2(2*q1.y*q1.w-2*q1.x*q1.z , sqx - sqy - sqz + sqw);
attitude = asin(2*test/unit);
bank = atan2(2*q1.x*q1.w-2*q1.y*q1.z , -sqx + sqy - sqz + sqw)
}

I can see the singularity test for south & north poles.
It means I could also do that in my code, but I guess, considering quaternion operations, quaternions stuff would be more efficient.
Am I right here ?

About heading, and because I want the angle between my cam view direction and the vector between the object & the cam, I’m a bit confused.

what I would make:
- calculate the heading of my cam, providing its quaternion grabbed from jit.anim.node
- calculate the heading of the vector define by the segment from the cam to object (I’d need a quat here :-/)
- substract one to the other

Is it the way to go ?
The last substraction drives me intuitively to think I’d meet again a gimbal lock, making this operation outside of the quat world…

#166064

You must be logged in to reply to this topic.