# What type of rotation is this and how do I apply it?

I'm developing a 3D game in Java using LWJGL and JOML. In order to simplify player and NPC coding, I'm representing their orientation as two scalar quantities: "direction" and "angle" (maybe not the best names). "Direction" is the compass direction that the object is facing, and "angle" is the angle of the object relative to a horizontal plane. So for example if a player has a direction of 180 and an angle of 45, it will be looking south at a point halfway between the horizon and the zenith.

First question: what is this kind of rotation called officially?

I've already written code to handle calculations on points/positions represented in this manner, but I don't know how to include the rotation in a transformation matrix for OpenGL. I have the following code to produce a transformation matrix for an object:

Matrix4f world_matrix = new Matrix4f().identity().translate((float) position.x, (float) position.z, (float) -position.y).rotateY((float) Math.toRadians(-direction));


Matrix4f world_matrix = new Matrix4f()
.identity()
.translate((float) position.x, (float) position.z, (float) -position.y)


where position is the location of the object and direction is the aforedescribed "direction" of the object.

Second question: how do I include angle in world_matrix?

You can include your "angle" in the world matrix by rotating around the X axis after the rotation around the Y axis (using the rotateX method at the end of your transformation chain). Note that the transformation methods in JOML act after any existing transformations already applied to the matrix, so rotateX will rotate the object about its local X axis (to set the object's rotation relative to the global coordinate space use one of the "setRotation" methods).
• I've done that for my camera angle but it doesn't work for objects (as I originally expected it wouldn't) because the angle rotation isn't about the global X axis but about the local (object-relative) X axis. .rotationX() rotates the object about the global X axis. Commented Aug 16, 2016 at 15:14
• I don't understand why Matrix4f().identity().rotateX((float) Math.toRadians(-angle)).rotateY((float) Math.toRadians(direction)).translate((float) -position.x, (float) -position.z, (float) position.y) works for the camera matrix but Matrix4f().identity().translate((float) position.x, (float) position.z, (float) -position.y).rotateX((float) Math.toRadians(angle)).rotateY((float) Math.toRadians(-direction)) doesn't work for an object's transformation matrix (the object ends up "sideways" as direction moves away from 0). Commented Aug 16, 2016 at 15:22
• Never mind I've got it now - I put .rotateX() after .rotateY() and now it works. I thought the rotations were independent of each other (i.e. that .rotateX() means "set the rotation about the X (global) axis") but they're not (i.e. .rotateX() actually means "rotate the object about the (local) X axis, taking into account any existing rotations"). Commented Aug 16, 2016 at 15:24