# getOrientation() / setOrientation() the solution!

(blendedHKU) #1

Hi everybody

http://www.elysiun.com/board/viewtopic.php?t=184&highlight=euler

You can find two functions written by eeshlo called
mat2euler, which allows you to get the rotation (in radians) of an object.
He also posted a function to convert radians into a matrix, so you can easily set the rotation of objects with the owner.setOrientation() functions

Jasper

(eeshlo) #2

Here is the shorter & faster version:

# extract euler rotation from matrix
def mat2euler(mtx):
angle_y = -asin(mtx[0][2])
C = cos(angle_y)
if C!=0.0: C = 1.0/C
angle_x = atan2(mtx[1][2] * C, mtx[2][2] * C)
angle_z = atan2(mtx[0][1] * C, mtx[0][0] * C)
return (angle_x, angle_y, angle_z)

# creates full 3D rotation matrix
# rx, ry, rz angles in radians
def euler2mat(rx, ry, rz):
A, B = sin(rx), cos(rx)
C, D = sin(ry), cos(ry)
E, F = sin(rz), cos(rz)
AC, BC = A*C, B*C
return [[D*F, 	   D*E, 	 -C],
[AC*F-B*E, AC*E+B*F, A*D],
[BC*F+A*E, BC*E-A*F, B*D]]

(Doc Holiday) #3

Hallo eeshlo. I don’t want to be a Spoilsport! Verry nice Script, really. But useless for gaming i think. 'Cause it’s JUMPING! (And You got a “math domain error”.) I had the same Problems in my Park Demo. That’s why my Script dosen’t work 100%. (Used for Lens Flares and Chrome Mapping.) The Problem with this Euler-Stuff is, there exists always more then one possible Solutions for one Position. (You can turn left and right to look in a fix Direction.) For gaming, there is a constant rising Value necessary. How ever could i react on a fix Position? (e.g. from 32° - 267°, show Object XXX.) I have looked for the ‘Jumps’ in Your Script. You can see them in the List below. If You want to fix them, it would give us more freedom by coding in Blender. I want do that in my Park Demo, but i was at the End with my Knowledge. I hope, You make it better than me.

Good Luck!
Cu, Doc

Always rotating ONE Axis!

Jumps by rotating X:

0°.....90°.....180°....270°....360°
----------------------------------
X: ----&gt; 0            180|-180           0
Y:       0                               0
Z:       0                               0

Jumps by rotating Y:

0°.....90°.....180°....270°....360°
----------------------------------
X:       0     0|180           180|0     0
Y: ----&gt; 0      90       0      -90      0
Z:       0     0|180           180|0     0

Jumps by rotating Z:

0°.....90°.....180°....270°....360°
----------------------------------
X:       0                               0
Y:       0                               0
Z: ----&gt; 0            180|-180           0

(Jump: from|to)
The 'math domain error' comes when Youre Script
is "Jumping". On exact 180° e.g., Your Script don't
know: 180 or -180.

(eeshlo) #4

Which function did you use? The above code or the old code from the python forum? I’m not surprised really that they might give problems, I didn’t know blendedHKU wanted this for the game engine. But like I said in the python forum, the code you see there at the start of the thread actually does ‘jump’ as you call it. The code above seems to be better as well as shorter. If you get a ‘math domain error’ with that, you could try to normalize each vector of the matrix, or you could do something like:

def mat2euler(mtx):
t = mtx[0][2]
if t&lt;-1.0:
angle_y = 1.570796327
elif t&gt;1.0:
angle_y = -1.570796327
else:
angle_y = -asin(t)
..........

But in any case, these functions are almost never used in realtime engines. Quaternions are almost always used instead. I can help with this if you want, but I would need a specific example of what it is you want to do. I don’t have much experience with the game engine.

(Doc Holiday) #5

Hallo eeshlo! I am very happy, that You want to help us. But at first, let me help You, if i can. I have used the normal sin; cos … Functions in my Script. If You want to see that in action, have a look at may Park Demo. You can find it on my Web Page. The Problem i have, is: The Script only works if the Object (Camera) is Parent of an other Fix Object and receive the Z Rotation from that. I would fail on an free, in 3 Axis, rotating Object. That’s why my Script only work in this Situation. I made a little Test File, wich You can download here: http://project-blender.onlinehome.de/EulerTest.zip You can switch between ‘Parent’ and ‘free’ Scene to see the FX. It’s important to have a constan Value in all Directions to get an usefully, and universal placeable Tool. (I don’t know, if it’s mathematical Possible, ever.) But that would be really cool! I hope You know what i mean and can write such a Tool. I failed at the Try. Hmm, maybee i should not drink so much Beer! (But in Bavaria, it’s very hard to keep clean.)

God Luck, have Fun and best Wishes
Cu, Doc

(eeshlo) #6

Thanks for creating a blendfile for me, but I have a hard time trying to see the difference between ‘free’ and ‘parent’. I see the same numbers appearing with both. I also compared my function (converted back to a matrix) with the matrix that getOrientation returns, and they seem to be exactly the same, which I would think is a good thing.
I know your park demo and the others, I like to keep a close watch on what is being done with the game engine too.
Anyway, maybe you can explain more clearly what it is you would expect the function to return. I understand German, so you can mail me in German if you can explain it more clearly that way.

(Doc Holiday) #7

Ok, i try it in german. It’ much easier for me. I send a Mail.
Cu, Doc

(eeshlo) #8

Ok, I now see what the difference is, but I still don’t see what it has to do with the math functions. They really seem to work as they should. I have tried adding another object and using the calculated matrix on it with setOrientation and it rotates exactly the same way as the source object. The parenting of one object with another is basically a multiply of both object rotation matrices. So maybe you could simulate that if you want. But I don’t see why it would be a problem parenting an empty like you did in the park demo. But still, if I seem to be misunderstanding, maybe you can give me a better description of what it is you want…

(Doc Holiday) #9

:o ??? Of which Script You talking about? Your’s or mine?

(eeshlo) #10

My script. I have tried yours too, but rotations seem reversed and when rotating one axis after another or more at the same time it doesn’t work anymore. My math functions seem to work as they should. I wrote that btw before I saw your mail.