Rotation Matrix

hey everyone,

I know there are a lot of problems with the Orientation property in the game engine,
so with some help from math-sites I created a script that calculates the matrix in euler angles and back again.

for more information on euler angles see http://en.wikipedia.org/wiki/Euler_Angles

feel free to use this script, so you don’t have to make it yourself.

Yssie

ps. there’s one problem, the object can’t rotate further than +180 or -180 degrees


from math import sin, cos, pi, atan2, asin
own = GameLogic.getCurrentController().owner
 
##### Change these values to rotate the object ##
x = 0
y = 0
z = 0
 
######## get angle in radian ####################
ori = own.worldOrientation
if ori[1][0] == 1:
 arz = atan2(ori[0][2], ori[2][2])
 ary = pi / 2
 arx = 0
elif ori[1][0] == -1:
 arz = atan2(ori[0][2], ori[2][2])
 ary = -pi / 2
 arx = 0
else:
 arz = atan2(-ori[2][0], ori[0][0])
 ary = asin(ori[1][0])
 arx = atan2(-ori[1][2], ori[1][1])
 
####### set angle from radian in degrees ########
rx = arx*(180/pi)
ry = ary*(180/pi)
rz = arz*(180/pi)
 
######### change the angle #######################
rx = rx + x
ry = ry + y
rz = rz + z
 
###### set angle from degrees in radian #########
arx = rx/(180/pi)
ary = ry/(180/pi)
arz = rz/(180/pi)
 
########## set matrix from angle ################
sa = sin(ary) 
ca = cos(ary) 
sb = sin(arx) 
cb = cos(arx) 
sh = sin(arz) 
ch = cos(arz) 
rot_matrix = [[ch*ca,  -ch*sa*cb + sh*sb,  ch*sa*sb + sh*cb], 
       [sa,    ca*cb,        -ca*sb], 
       [-sh*ca,  sh*sa*cb + ch*sb,  -sh*sa*sb + ch*cb]] 
 
own.worldOrientation = rot_matrix
 
##################################################