The rotation of an object is described by default as a matrix
GameObject.worldOrientation
the mathutils module, from which subclasses such as Vector are derived, shows how you can convert between them
Assuming that you want the X, Y and Z rotations of each axis vector in degrees, you can do this…
Realise that angles are taken anticlockwise as positive. sample.blend (608 KB)
Thanks for the help Agoose but its a little complicated and over my head, anything slightly simpler?. I havent gotten into functions and modules. When I print object.worldOrientation i get like 9 numbers. What I dont understand is when i rotate on 1 axis, 3 of the values move. Im basically just trying to define the y rotation of my object as I roll it.
I must say after tinkering with Agoose’ little script I got this i set up in my script:
import math
matrix_rotation = owner.worldOrientation
euler_rotation = matrix_rotation.to_euler()
degrees_rotation = [math.degrees(a) for a in euler_rotation]
degrees = int(degrees_rotation[1])
print(degrees)
works great and I can get a whole number. Thanks Agoose
It’s actually a simple example, but i bet the module scared you off
Here’s how to use it:
add ‘import math’ to the top of your script (Without the ‘’)
now, copy the code for get_rotation into the top of you python file, below all the import statements:
def get_rotation(object):
matrix_rotation = object.worldOrientation
euler_rotation = matrix_rotation.to_euler()
degrees_rotation = [math.degrees(a) for a in euler_rotation]
return degrees_rotation
now, wherever you want the rotation of an object calling the function will return the x, y, z rotations.
If you just want the rotation about the Z axis, then do something like this:
Ah, I was a little late replying
I’m glad you found it useful. If you get into this stuff, feel free to PM me, for I’ll happily explain why that is the case.
The code you’ve extracted is simply in the form of a function above, as you would use a function in maths. It means that if you get the rotation of an object more than once it may prove useful to use a function rather than copying and pasting code each time.
Next question is, how do you then SET the rotational position of an object? Let’s say for example that I simply want to rotate an object around the z-axis from 0 deg. to 180 deg. using the keyboard? i.e. I hit the A-key and it goes to 0 deg., I hit the S-key and it rotates to 180 deg. - If I hit the A-key again, back to 0 deg. - toggle, rinse, repeat.