Search code examples
java3drotationlwjgl

Lwjgl rotation not work


I have an object that can rotate on all axes, my problem is that if you rotate the object on the y axis (eg 90 °) the rotation on the z axis is incorrect because it rotates over the x axis,Instead, rotation on the x axis continues to function perfectly, If the y axis is set to 0 °, the rotation of the z axis returns to function correctly.

to rotate and translate object i use this code:

public static Matrix4f createTransformationMatrix(Vector3f translation , float rx, float ry, float rz , float scale){
    Matrix4f matrix = new Matrix4f();
    matrix.setIdentity();
    Matrix4f.translate(translation,matrix,matrix);
    Matrix4f.rotate((float) Math.toRadians(rx),new Vector3f(1, 0, 0) , matrix,matrix);
    Matrix4f.rotate((float) Math.toRadians(ry),new Vector3f(0, 1, 0) , matrix,matrix);
    Matrix4f.rotate((float) Math.toRadians(rz),new Vector3f(0, 0, 1) , matrix,matrix);
    Matrix4f.scale(new Vector3f(scale, scale, scale), matrix, matrix);
    return matrix;}

Solution

  • That is an intrinsic singularity in Euler angle rotations, called Gimbal lock. Use quaternions for rotations instead (can specify an arbitrary axis).