# setRotation() in GameLogic?

There is a function for setPosition, which I find incredibly useful, why can’t I find a similar function to control rotation? I can control rotation through the object actuator, but I need to be able to set solid global coordinates and not an angular displacement vector. Is there a method for this?

create orientation matrix (3x3) and set it to object using setOrientation() method.

Hmm, is there any simple way to learn how the matrix works? I’ve looked at http://en.wikipedia.org/wiki/Rotation_matrix and it scares me. I’m terrible at math, and I really just want to be able to make my object turn a few degrees.

``````import math

# rotation matrix
def matRot(axis, angle):
angle *= math.pi/180
cos = math.cos(angle)
sin = math.sin(angle)
mat = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
i1 = (axis + 1) % 3
i2 = (i1 + 1) % 3
mat[i1][i1] = cos
mat[i2][i2] = cos
mat[i1][i2] = -sin
mat[i2][i1] = sin
return mat

# matrix multiplication
def matMult(mat1, mat2):
mult = []
for r in range(3):
mult.append([])
for c in range(3):
mult[r].append(0)
for m in range(3):
mult[r][c] += mat1[m][c] * mat2[r][m]
return mult
``````

Then if you need rotation in Z axis by 50 degrees and rotation in X axis by 120 degrees, just use following code:

``````
rotZ = matRot(2, 50)
rotX = matRot(0, 120)
orientMat = matMult(rotZ, rotX)
gameObj.setOrientation(orientMat)
``````

I think setOrientation is expecting a quaternion or something.

With this code:

``````import math

# rotation matrix
def matRot(axis, angle):
angle *= math.pi/180
cos = math.cos(angle)
sin = math.sin(angle)
mat = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
i1 = (axis + 1) % 3
i2 = (i1 + 1) % 3
mat[i1][i1] = cos
mat[i2][i2] = cos
mat[i1][i2] = -sin
mat[i2][i1] = sin
return mat

# matrix multiplication
def matMult(mat1, mat2):
mult = []
for r in range(3):
mult.append([])
for c in range(3):
mult[r].append(0)
for m in range(3):
mult[r][c] += mat1[m][c] * mat2[r][m]
return mult

controller = GameLogic.getCurrentController()

owner = controller.getOwner()

rotZ = matRot(2, 50)
rotX = matRot(0, 120)
orientMat = matMult(rotZ, rotX)
print orientMat
owner.setOrientation(orientMat)
``````

I get this output:

``````[[], [], [0, 0, -0.49999999999999978]]
Python script error from controller "cont#GLOBE#1":
Traceback (most recent call last:)
File "globe", line 36, in &lt;module&gt;
AttributeError: error setting vector, 3 args, should be 4
``````

I think you have indentation problem. This should work correctly:

``````import math

# rotation matrix
def matRot(axis, angle):
angle *= math.pi/180
cos = math.cos(angle)
sin = math.sin(angle)
mat = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
i1 = (axis + 1) % 3
i2 = (i1 + 1) % 3
mat[i1][i1] = cos
mat[i2][i2] = cos
mat[i1][i2] = -sin
mat[i2][i1] = sin
return mat

# matrix multiplication
def matMult(mat1, mat2):
mult = []
for r in range(3):
mult.append([])
for c in range(3):
mult[r].append(0)
for m in range(3):
mult[r][c] += mat1[m][c] * mat2[r][m]
return mult

controller = GameLogic.getCurrentController()

owner = controller.getOwner()

rotZ = matRot(2, 50)
rotX = matRot(0, 120)
orientMat = matMult(rotZ, rotX)
print orientMat
owner.setOrientation(orientMat)
``````

Ah, you’re right, I didn’t have them nested. Thanks! The script works great!

Hi dudes

The script is awesomes…

I’m trying to got the rotY too without success. I’m not a genie in Math.

Could you upload the modiefied code for all the axis (X,Y,Z) and not for the X and Z only please?

Thx,
[email protected] :rolleyes:

I think the code functions by using the list indices X = 0, Y = 1, Z = 2

Pass one of these numbers to the matRot function as the first parameter and the angle to rotate as the second parameter in the form

``````
rotAxis = matRot(axis, angle)

``````

So to rotate on the y axis:

``````
rotY = matRot(1,angle)     #x = 0, y = 1, z = 2

``````

To combine rotations on more than one axis, multiply the matrices together using the matMult function:

``````
orientMat = matMult(orientMat, rotY)

``````

I think the matrix multiplication could be done more easily using the Blender.Mathutils.Matrix module. Research this in the Blender Python API if you’re interested.

I haven’t tried this, but maybe it will work?

Hi