Rotational Matrix

I’m not very good at maths. There are too many gaps in my understanding of things like trigonometry etc.

I need a cheat sheet for some rotational matrixes for localPosition method in bge.

How do I rotate 90 degrees on the z axis?
How do I rotate -90 degrees on the z axis?


I’m unsure of what exactly you’re asking for. If you want to purely rotate an object by 90 degrees on the Z axis (globally), then you can just use the object’s applyRotation() function. Use math.radians() to convert 90 degrees into radians, or use math.pi to do it yourself (since pi is 180 degrees, half of pi is 90 degrees).

from bge import logic

import math

cont = logic.getCurrentController()

owner = cont.owner

owner.applyRotation([0, 0, math.pi / 2])

Hi SolarLune, I solved my problem avoiding rotation altogether. Thanks though, that will likely be usefull for me in the future.

I meant the parameters for the localOrientation method not the localPosition method mentioned incorrectly in my original post.

obj.localOrientation = ([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0])
I’m just clueless as to calculating matrix’s to set the orientation.

If you are desperate to use the matrix:

import bge, math
from bge import logic
from math import cos, sin

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

a = own['a']

def z():
    own.worldOrientation[0][0] = cos(a)
    own.worldOrientation[0][1] = -sin(a)
    own.worldOrientation[0][2] = 0

    own.worldOrientation[1][0] = sin(a)
    own.worldOrientation[1][1] = cos(a)
    own.worldOrientation[1][2] = 0

    own.worldOrientation[2][0] = 0
    own.worldOrientation[2][1] = 0
    own.worldOrientation[2][2] = 1

    own['a'] += 0.5

a is the angle you want to change it by.
Here is it in a blend file all set up.
zMatrix.blend (478 KB)

Here is your cheatsheet (courtesy of a Google search)

That is straight from Wikipedia,here’s the link.

BUT don’t do it this way. It’s ridiculously overkill when you could do it the way Solarlune suggested.


zMatrix.blend (489 KB)

I explain the basics of the orientation matrix in this video:

With the above explanations you now know that rotation around the Z-Axis changes the X and Y components of the Matrix (which is not complete btw. but sufficient for rotation).

As you want to rotate in 90° steps the calculation of sin and cos is pretty simple (school math).

sin(90°) = 1
cos(90°) = 0

it means this are special cases making life a bit easier ;).

now fill it in the above rotation matrix around Z:

 0 -1  0
 1  0  0
 0  0  1

to rotate -90°:

 0  1  0
-1  0  0
 0  0  1

Now you multiply this “90° around Z” rotation matrix with the existing orientation matrix to get the rotated orientation.