I'm using a view matrix to do camera movement and rotation.

viewMatrix = new Matrix4f();
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), viewMatrix, viewMatrix);
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), viewMatrix, viewMatrix);
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), viewMatrix, viewMatrix);
Matrix4f.translate(new Vector3f(x * (-1), y * (-1), z * (-1)), viewMatrix, viewMatrix);

I'm manipulating the rotation values around each axis here:

//left and right
public void rotateY(float amount)
    ry += amount;

//Up and down
public void rotateUp(float amount)
    rx += amount;
    if(rx < -90)
        rx = -90;
    if(rx > 90)
        rx = 90;

This all works fine. Now i'm trying to create a more "free" camera so that i always turn left an right and don't simply rotate around the Y-Axis.

For the rotation up you simply remove the if-cause. But for the rotation sidewards you need to calculate the parts for the x- and the z-axis using sin and cos. That's where i get confused. How can i rotate sidewards correctly?


1 Answer 1


Generally, whether you rotate around "local Y" or "world Y" depends on the order you apply the rotations.

If you're "free flying", then your variable ry no longer has a specific meaning, because all the moves are cumulative.

You can keep a matrix, and apply successive rotations and translations to it to fly around. After a long while, there can be cumulative arithmetic errors, and you may need to renormalize the matrix, which is to make sure that the upper 3x3 rotation portion has 3 unit basis vectors at right angles to each other. (This is what I'm doing currently in my project.)

You can also represent rotations as quaternions, which is more stable but introduces a small handful of other math to also understand.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .