Search code examples
javaopengljoml

Why is my 3D cube distorting when rotating?


The problem is that when rotating my 3D cube on more than one axes, it distorts weirdly roughly halfway through. I am using the JOML math library for matrices.

// This is the model matrix for the rotation of a textured cube
Matrix4f model = new Matrix4f();
model.identity();
model.rotate((float)(glfwGetTime() * Math.toRadians(50.0f)), new Vector3f(0.5f, 1.0f, 0.0f), model);
// Other matrices for coordinate system
Matrix4f view = new Matrix4f();
view.identity();
view.translate(new Vector3f(0.0f, 0.0f, -3.0f), view);
Matrix4f projection = new Matrix4f();
projection.identity();
projection.perspective((float)Math.toRadians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); // FOV is 45

This is a gif of the distortion:

gif of the distortion


Solution

  • The main problem is that your rotation axis (0.5f, 1.0f, 0.0f) is not unit/normalized, (as is also required by its JavaDoc)

    Parameters:

    axis - the rotation axis (needs to be normalized)

    To solve the problem and still use a matrix, you simply need to normalize the rotation axis.

    Also (but this is irrelevant to the error):

    • JOML matrices are identity by default after instantiation - you do not need to call identity() on them)
    • you can omit supplying model as the argument to the dest parameter of rotate()

    So:

    // This is the model matrix for the rotation of a textured cube
    Matrix4f model = new Matrix4f();
    model.rotate((float)(glfwGetTime() * Math.toRadians(50.0f)),
                 new Vector3f(0.5f, 1.0f, 0.0f).normalize());