## How to use Euler Angles/Quaternions correctly?

Clarisse Scripting related topics

### How to use Euler Angles/Quaternions correctly?

Hello there,

I've been going through many examples here, but still don't get how to use Euler Angles and Quaternions in Clarisse's Python . So I'm hoping that somebody here knows more than me. What I basically want to do is to get the orientation of an object in spac eto move another object along my reference object's direction given through 3 rotation angles. To make it more clearely: consider a guy with a bow. He points the bow in direction of a target and shoots. The arrow will move in direction of the target and this is exactly what I want to achieve.

I know it's possible with Clarisse's on-board tools, but I'm looking for a Python solution. I think I need Euler Angles or Quaternions to do that, but is this possible with the SDK functions in Clarisse? If yes, how do I have to use things like ix.api.GMathMatrix4x4d.compute_euler_angles?

Many thanks for any help,
Gerd
gerdhofer

Posts: 56
Joined: Thu Aug 30, 2018 11:05 am

### Re: How to use Euler Angles/Quaternions correctly?

Hi,

Here is how to use the API:

#### py code

```# the matrix you want to extract the angles from, must be of type GMathMatrix4x4d# here, for example, get the matrix from a specified geometrymatrix = ix.get_item("path/to/geometry_object").get_module().get_global_matrix()# create the output variable that will hold the extracted Euler anglesangles = ix.api.GMathVec3d()# extract the angles# the 3rd parameter "order" is optional and its default value is "ix.api.GMATH_ROTATION_ORDER_ZXY"ix.api.GMathMatrix4x4d.compute_euler_angles(matrix, angles)# do something with the angles...print(angles)
```

Available rotation order values:
Code: Select all
`GMATH_ROTATION_ORDER_ZYXGMATH_ROTATION_ORDER_YZXGMATH_ROTATION_ORDER_XZYGMATH_ROTATION_ORDER_ZXYGMATH_ROTATION_ORDER_YXZGMATH_ROTATION_ORDER_XYZ`

Hope this helps,
Anthony Nemoff
Isotropix
R&D Engineer anemoff

Posts: 354
Joined: Wed Jan 13, 2016 10:10 am

### Re: How to use Euler Angles/Quaternions correctly?

Thanks Anthony, and no worries about the late reply. Since I'm just trying to understand concepts it wasn't really urgent.

Anyway, maybe you can help me one more time as it doesn't solve my "problem" completely. What I basically want to get is a direction vector and I thought Euler angles might be useful here (but obviously they're not). An example:

When I add a basic polygrid in the XZ plane, my direction vector would point upwards along the Y axis: dir_vec = (0,1,0).
With a rotation of 90 degrees around X the direction would be along the Z axis: dir_vec = (0,0,1)

So, is there any way to extract this direction vector, perhaps from the 3 rotation angles of my plane or through a specific Python function? I was trying to use the polygrid's normals, but they keep their initial values from creation time, and don't consider any later rotations.

I hope you understand what I'm looking for. Thank you!
gerdhofer

Posts: 56
Joined: Thu Aug 30, 2018 11:05 am

### Re: How to use Euler Angles/Quaternions correctly?

Hi,

I'm not sure if your question regarding the direction of objects in Clarisse is specific to the polygrid or for any object.

If it's just for the polygrid, consider (0, 1, 0) as its initial direction vector (facing Y-up).
Then you can get the rotated direction using this:

#### python code

```# get rotationrot = ix.get_item('project://scene/polygrid').get_attribute('rotate').get_vec3d()rot_mat = ix.api.GMathMatrix4x4d()rot_mat.make_rotation(rot)rot_quat = ix.api.GMathQuat()rot_quat.set_rotation(rot_mat)# default direction vector: here we use Y-updir_vec = ix.api.GMathVec3d(0, 1, 0)# rotate the default direction vector with the object's rotation quaternionrot_quat.rotate(dir_vec)print(dir_vec)
```

You could do the same for other objects by deciding what initial direction to use, towards X for example with (1, 0, 0).

Hope this helps,
Anthony Nemoff
Isotropix
R&D Engineer anemoff

Posts: 354
Joined: Wed Jan 13, 2016 10:10 am

### Re: How to use Euler Angles/Quaternions correctly?

Fantastic. The polygrid was just meant as an example and I was indeed looking for a solution that covers any type of object. Your code is exactly what I was hoping for, so thanks again for your help. Much appreciated.
Last edited by gerdhofer on Tue Jul 21, 2020 9:55 am, edited 1 time in total.
gerdhofer

Posts: 56
Joined: Thu Aug 30, 2018 11:05 am

### Re: How to use Euler Angles/Quaternions correctly?  