2

To begin, here's my code:

import bge
from bge import logic
import random
from math import degrees

cont = logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
rotation = own.worldOrientation.to_euler()
rotation_x_round = round(degrees(rotation.x), 3)
rotation_y_round = round(degrees(rotation.y), 3)
rotation_z_round = round(degrees(rotation.z), 3)
own["x"] = rotation_x_round
own["y"] = rotation_y_round
own["z"] = rotation_z_round

own.rotation_euler = Euler((10, 10, 10), "XYZ")

I am trying to set the rotation of my object, not add rotation. In the code, own.rotation_euler is what creates the error. It is telling me there is no attribute "rotation_euler," so I tried changing the line to the following: own.worldOrientation = (10, 10, 10). This one works; however, as I've assumed, it didn't rotate it correctly as it rotated the object to a number which seemed independent of my input. I have come up with my code from this question. Why does Blender's rotation_euler not work? Thanks!

Also, the .blend (that you can use as a start) can be found here.

blackhole
  • 2,390
  • 4
  • 26
  • 64

1 Answers1

3

Details

A KX_GameObject has no attribute called "rotation_euler".

What you request for is KX_GameObject.worldOrientation or KX_GameObject.localOrientation as you mentioned in your question description.

Be aware the orientation is a 3x3 matrix. The setter accepts Euler and a 3-tuple with Euler coordinates for your convenience. It will automatically convert it to a 3x3 matrix.

I'm pretty sure your observation of

[...] it rotated the object to a number which seemed independent of my input.

is not correct.

Answer

You answered your question by yourself: own.worldOrientation = (10, 10, 10).

Remarks:

  • A rotation matrix does not deal with angles, it is a linear equation system. It is a Cartesian coordinate system. You will never be able to directly read any angles from it. But you can convert the coordinates to a different coordinate system e.g. polar coordinates via mathutils' Matrix.to_euler(order, euler_compat).

  • Euler coordinates are angles given in rad (not degree). It is a Polar coordinate system.

  • The value 10rad will automatically reduced to the first period of the radian angle measure system's range (-pi ... +pi) which is -2.5664rad or -147°.

  • To convert degree to radian you can use the python library math e.g. math.radian

Monster
  • 8,178
  • 1
  • 11
  • 27
  • No problem, I removed the reference to the old question – Monster Dec 16 '15 at 09:15
  • cool will remove the comment :) – zeffii Dec 16 '15 at 09:16
  • "You answered your question by yourself: own.worldOrientation = (10, 10, 10)." I am a bit confused. I looked up for some information about the orientation, the "3x3 matrix." I couldn't come up with a solution. I was trying to set the object's rotation to 10, 10, 10. I am not sure how it is coming up with -32.958... Again, this could be because I am not familiar with the orientation. Could you explain a little? I'd greatly appreciate it. And as always, thank you for the answer! :) – blackhole Dec 17 '15 at 03:37
  • I added some more details. Does that help you more? – Monster Dec 17 '15 at 05:07
  • Thank you for adding the information. It helped me a lot. It cleared up a lot about what I've been confused about, especially the links. I have finally come up with a conclusion. It has 2 parts, here's part 1: eul = mathutils.Euler((math.radians(10), math.radians(10), math.radians(10)), "XYZ"). Also, here's where the rotation takes place (part 2): own.localOrientation = eul.to_matrix(). Thank you for the information! :) – blackhole Dec 18 '15 at 17:32