# Get npc z rotation (only z)?

I want to get just the z rotation of an NPC to determine their direction. This might also be used to generate a compass for the player, but I do not want to muck around with matrix math too much. Is there a way to just get an accurate number for an object’s z rotation, either via a python command or some simple and fancy math?

Once you get a hold of an object’s rotation matrix you can get a more intuitive representation of the rotation by converting it to an Euler representation. In this representation you get a vector with each element representing the rotation about an axis. By default the Z axis is the third element.

``````
z_rotation = obj.worldOrientation.to_euler()[2]

``````

i dont know what is a euler, here is the cheap junky code for the poor man:

Z_rot = obj.worldOrientation[2] # GLOBAL rotation on the Z axis

note that when you use:

rot = obj.worldOrientation

this returns a list of three floats, these floats are the rotation value in the axis X, Y, Z, in that order, this means that you can do this:

X_rot = obj.worldOrientation[0] # rotation on the X axis
Y_rot = obj.worldOrientation[1] # rotation on the y axis
Z_rot = obj.worldOrientation[2] # rotation on the z axis

you can also use:

rot = obj.localOrientation[2] # LOCAL rotation on the Z axis

if you want to return the local orientation

i hope this is useful

-lucki

Two responses? That is better than I expected, I’ll try both out. I have always wanted to create a fully dynamic world, but I cannot use navimeshes so I wanted to use rays and current rotation to allow navigation.

Euler converts to a angle representation (polar coordinates). They are usually easier readable for humans.

The components of the matrix are the projections of a vector to a specific axis (X,Y,Z). They are commonly known as sin/cos. Indeed they can be used as direction. The matrix representation is faster and less complex to be used by an application but hard to read by humans.

For a compass the matrix is the way to go. As you need a direction.
For displaying an angle the euler is the right choice as it gives you the angles.