# How to apply a quaternion rotation to many objects ?

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 3×3 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 3×3 matrix.

Anyone would help here?

[attachment=197165,4071]

I tried to concatenate my 3 outputs, which provides a 3 triplets elements..

So I tried to glue, which provides a nice 3×3 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 :)

[attachment=197171,4072]

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.

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.

I didn’t even achieve this :-(

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

rotation matrix is 1 plane 3×3

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

I guess this can work:

**all**of the following text. Then, in Max, select

*New From Clipboard*.

```
----------begin_max5_patcher----------
453.3ocyV9rbBBCDF+L7TjImoLPPnZO09Fz6cbbhPTiMjvPBVac7cujEvZcn
yfsUaufr69Q1O9k+f6bcvyUaYZL5NzSHGmctNNPJaBm1XGbNcapfpAYXI6E0
70XulRF1VCjdM23Kn94UBSWMdFToV8M2F0kbgRZjzbFT5gRNUzUQVkykBlAZ
C4H4Z9af7PhevGZUUlSE2jx7ZAq4ExZpY4TSIeK1CgwnosBKnlzUb4xYkrTS
i1vX+Xu5V.+DOttSHRfe.Zp8I165Zu38yQzRQECcepRTkK0nndPUxjyDUQ+M
nZBA.VcvEBUUxBZ5y8QnwmIgBOGBEOLBM7nAvxjHKDufrrwMnRko9NTHZgPQ
MQDTTuKAiRtj.92XIHYDPr10fIjKL2pKW2Z8iJM2vU1MtGHXHJYTOHjDD7em
gsm30rliD1KBgmGK3xS+FALd17elqZUUYZmWNvATvAejwzFtjZo3Qpr6nORz
JdVFCp281mw0z4BFv2fdmhGpgNoUegelbc8S3P7S30yOjg3Gx0wOSFv7k8uW
bklurmMNL+7cluZ1uQKJ1vJ0siIXk5SrVqJsgIdPHW1DBiHtjsg2oO10NZ6c
eGPIHR0u
-----------end_max5_patcher-----------
```

[attachment=197374,4078]

okay.

here is the solution: use jit.pack correctly.

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

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.

[attachment=197409,4080]

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.

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.