Hi,
when I use Object.matrix_world in the Blender Python API (bpy), I get a 4x4 matrix which includes translation, rotation and scale. That’s basically what I need.
When I call KX_GameObject.worldOrientation in the BGE, I just get a 3x3 matrix that does not seem to change when the translation or the scale of the object changes. Is there a way to get the bpy-style 4x4 matrix in the game engine?
Thanks so much! And sorry for the about 1000th question about matrices.
I’m pretty sure the Wiki has the components of a 4x4 matrix somwhere.
By the way in a 3D Space the orientation matrix is 3x3 (see http://en.wikipedia.org/wiki/Rotation_matrix)
It contains the scaling too which are the diagonal terms (see http://en.wikipedia.org/wiki/Scaling_(geometry)).
To include the translation transformation you must extend the matrix to 3X4 (or 4x3? I can’t remeber). But it is easier to calculate with 4x4 Matrices so the Matrix can be extended to 4x4. Read http://en.wikipedia.org/wiki/Translation_matrix for details.
I forgot:
you create a Matrix with the scale parts only,
a matrix with the translation parts only and
a matrix with the rotation parts only.
Then multiply them to get the final transformation matrix.
Be aware the order of the multiplication is important (translationscalerotation != scalerotationtranslation).
That is all.
Monster
I guess you could put it together yourself in a function, something like this matches the matrix returned by the bpy
from mathutils import Matrix
def get_4x4(obj):
ori = obj.worldOrientation
pos = obj.worldPosition
scale = obj.scaling
rx = list(scale.x*ori[0])
rx.append(0)
ry = list(scale.y*ori[1])
ry.append(0)
rz = list(scale.z*ori[2])
rz.append(0)
rw = list(pos)
rw.append(1)
return Matrix([rx,ry,rz,rw])