How to apply a quaternion rotation to many objects ?

Julien Bayle's icon

Hi there,

I need to apply a quaternion rotation to many objects.

Thus, I have to apply a quaternion rotation to a 3 planes (x,y,z) matrix containing objects positions

I need to :
- convert the quaternion to a rotation matrix ( this helps http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm)
- multiply the rotation matrix by each triplets x,y,z

Basically, I'd prefer to use only 1 jit.gen to calculate the rotation matrix, then I'll apply the rotation outside of gen world, multiplying the rotation matrix by the positions one.

How can I create the 3x3 rotation matrix using jit.gen ?
I have all the calculations ready etc, but I don't know how to wire stuff or objects to use to pop out .. a 3x3 matrix.

Anyone would help here?

4071.quaternionToMatrix.PNG
PNG
Julien Bayle's icon

I tried to concatenate my 3 outputs, which provides a 3 triplets elements..
So I tried to glue, which provides a nice 3x3 matrices (afaik ?! :D)

but now, stuck in jit.gen....
nothing seems to be produced.
I restarted max, was thinking about a mess I did
nothing.

Anyone would point me to my problem ..?

Any help would be much than appreciate :)

4072.quaternionToMatrix2.PNG
PNG
Julien Bayle's icon

this wouldn't be complete without that.

Indeed, I tried to setup the jit.gen param as I thought but maybe the problem comes from here..

4073.jit.genAttr.PNG
PNG
Rob Ramirez's icon

to apply the same quaternion rotation to several objects, you can simply apply the rotation to the parent node (jit.anim.node or jit.gl.node).

if this isn't clear, let me know and i'll show you an example.

Julien Bayle's icon

it is pretty clear Robert.

I didn't explain why I wanted to make that.
Basically, I encountered the gimbal lock problem when I calculated azimuth/elevation for each object relatively to the cam. it happened for position when the object was up above, or under the cam (singularities of atan2 in the azimuth calculation)
Then, I wanted to go further and use quaternion.

The matrix to which I need to apply my quat rot is a global named matrix in which I have ALL my objects' coordinates.
I doubt I could simplify it.

Julien Bayle's icon

I didn't even achieve this :-(

objects coordinates matrix is 3 planes 1xN (N number of objects)
rotation matrix is 1 plane 3x3

how can I ... apply this rotation matrix to all my objects ...?

Julien Bayle's icon
Max Patch
Copy patch and select New From Clipboard in Max.

I guess this can work:

4078.mult.PNG
PNG
Julien Bayle's icon

a little improvement that provides me the way to process the 3 planes resulting matrix in my same jit.gen

BUT ... the last one is 4 planes... I expected 3.
I can unpack 4 to pack 3 .. but why does it contain 4 ...?

4079.Capturedcran2012062222.07.31.PNG
PNG
Julien Bayle's icon

okay.
here is the solution: use jit.pack correctly.

replacing the last [jit.pack] by [jit.pack 3] and it is okay.

Julien Bayle's icon

if it seemed to work, I tested and my azimuths aren't correct :-(
I tested just by looking by the jit.window... 180° or 0° would mean the object projection on cam plane is "in front of me"..
With 2 objects only, in order to not mess all, it doesn't work.

Before to forget about that part and to work on another part because of own saturation, here is exactly what I'm trying to do.
I hope a comprehensive saint guru wise dude could push me into the right way...

I need to calculate azimuth & elevation of objects relatively to my camera direction/plane.
Using only the 3 Euler angles to do that doesn't seem to be enough because of gimbal lock/singularities.

So I decided to go to quaternions.
I attached a snapshot.
I'm requesting position & quaternion in the scheduler (global qmetro)
I'm sending both + the objects matrix to the jit.gen.

The codebox' code comes from this http://fr.wikipedia.org/wiki/Quaternions_et_rotation_dans_l'espace#M.C3.A9thodes_utilis.C3.A9es
I verified and indeed, the quaternion rotation for vector calculation is ok.
Then, I'm taking the resulting vector x and z component to calculate the azimuth via atan2.

Why am I thinking azimuth is wrong?
Whatever the position & rotation of my cam, if I keep the object in the center of my window, the azimuth should be the same, right ??

It doesn't seem to be the case here :-/

Where am I doing wrong ?
any ideas, leads, help, hands would be totally appreciate at this late hour or later of course :)

I'm almost despaired about this part.

4080.arghCore.PNG
PNG
Julien Bayle's icon

I guess the quaternion I'm grabbing is not the good one ... I don't have other explanations and I wouldn't have more (considering my fresh discovering of quaternion)

btw, I can add something which could sound like a clue.
if the cam is reset by anim_reset message, it came back to initial position BUT especially orientation.
In this orientation and if I stay in the plane of the cam (I mean if I don't go a bit up or down but only forward/backward/left & right/panning left right, then my system works perfectly : azimuths are relevant etc.

Julien Bayle's icon

So, some statements.
Someone in gamedev.net forum answered me about Gimbal lock/quaternion/rotation.

First, I read this : http://www.gamedev.net/page/resources/_/technical/math-and-physics/quaternion-powers-r1095
Here is the process to make rotation with quaternions to avoid Gimbal Lock:

The main point you should take note (in the while loop) is
- The orientation of the camera is a quaternion.
- There are 3 angles corresponding to the keypress. Note the angles are meant to be an on/off switch (not accumulative). I reset them inside the while loop. Of course this is not the best way to do it but as I said, it is a quick job.
- I convert the 3 angles to a temporary quaternion.
- I multiply the temporary quaternion to the camera quaternion to obtain the combined orientation. Note the order of multiplication.
- The camera rotation is then converted to the Axis Angle representation for transforming the final matrix.
When a key is pressed, I generate a temporary quaternion corresponding to the key for a small rotation in that particular axis. I then multiply the temporary quaternion into the camera quaternion. This concatenation of rotations in 4D space will avoid gimbal lock. Try it and see for yourself.

In my system, as shown on the patch picture, I'm grabbing the cam quaternion and this is that one I convert to matrix and I apply it to all my objects coordinates.
This is that resulting vector I'm taking for my atan2 based azimuth calculation.

I don't use the temporary vector at all. Maybe my problem comes from here.
I don't use it because my system provides me directly the quaternion from the cam.

An user answered me about the fact I didn't use the temporary vector and it is very interesting for me :

That's the key to avoiding gimbal lock. You need to have some representation of the current attitude and modify it with incremental rotations around the axes. Whether you use quaternions or some other representation is secondary.

If you keep track of a cumulative roll, pitch and yaw and then compose them into an overall rotation, you'll run into gimbal lock regardless of what representation you use.

I'm not sure about "incremental rotations around axes"

Robert, how should I translate it into my system ?
I thought jit.anim.drive would do that.

lyve forms's icon

hi julien, did you manage to solve it? am very interested in this process, too