This doesn't work.

I wrote this VERY simple code. Using the BGE and the default cube, it prints the rotation of the cube initially and after I assign new values, it prints the new matrix with the new value, but the cube itself doesn’t rotate to the new position. I was expecting to see the cube rotate on the Z axis but nothing happens. I’m calling it with an ‘always’ sensor and a python controller. Any ideas?

This is the code:

          import bge
          cont=bge.logic.getCurrentController()
          own=cont.owner

          print (own.worldOrientation)
          own.worldOrientation=[[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,3.7]]

          print (own.worldOrientation)

You’re using the wrong method for setting the orientation:
It should be:
own.worldOrientation=(x,y,z)

I would suggest learning how orientation matrices work. It’s much more complicated than you seem to think. Each value is not, in fact, the rotation in degrees on each axis, it is a point relative to the object center that the axis points to, which always has a distance of ‘1’ away. I believe you can convert this matrix to something more user-friendly with a built in function.

Try doing something like:


import bge
own = bge.logic.getCurrentController().owner
rot = own.orientation.to_euler()
rot[1] += 0.1
own.orientation = rot.to_matrix()

Mokazon,
Thanks. It works. But this is an example of what I was trying to find out in this and my other post about documentation. I’ve read all that I could find about the BGE, and all the links that all of you here have provided. But I have never seen anything that would tell me about what you have used here. I had seen the own.rotation for example, but where did you find out about using the syntax ‘own.orientation.to_euler()’ and ‘own.orientation=own.rot.to_matrix()’ ? I am looking specifically at the to_euler() and to_matrix(). I have never seen this in any documentation, but you obviously know what you’re doing. Are these and other things listed somewhere where I can find them for reference?

a good tip is to use print(dir( )) often. If you do print(dir(own.orientation)), it will show you built-in functions you can run on it.

I have printed some directories, including ‘math’ and ‘mathutils’ but I didn’t see ‘to_euler’ or ‘to_matrix’. I will do what you said. Thank you. Everyone here has been very helpful.

Use the API reference to your benefit. The documentation on the KX_GameObject type (which is what you get from cont.owner) says that KX_GameObject.worldOrientation returns the type mathutils.Matrix. See this link:
http://www.blender.org/documentation/blender_python_api_2_63_17/bge.types.html#bge.types.KX_GameObject.worldOrientation

The documentation for mathutils.Matrix shows that it has a function to_euler().
http://www.blender.org/documentation/blender_python_api_2_63_17/mathutils.html#mathutils.Matrix.to_euler

Also, when printing directories, you have to print them at the level of what you are interested in knowing. Printing a directory for mathutils in general is not going to return the functions specific to mathutils.Matrix. To get what you wanted, you should have done as mokazon said:

print(dir(own.worldOrientation))

Thanks Blendenzo! Going further into the directories will definitely help me. The other part I have been missing is exactly how to read the API. When I saw ‘type’ under all the modules, I had no idea what that meant as far as returning a value, etc. I could never find a tutorial on how to understand the API exactly. Now at least I have an idea.