0
votes

When I use rotation matrix to rotate my square around Z axis it is actuall amaking small circles around it and not actually rotating in place. I want it to rotate like a wheel.

I have tried putting rotation first and then translation and then vice versa but it is not working. I've read some questions people have asked online but its all in old OpenGL.

My matrix code:

public static Matrix4f createTranslateMatrix(Vector3f t) {
        Matrix4f tM = new Matrix4f();

        Matrix4f.translate(t, tM, tM);

        return tM;

    }

    public static Matrix4f createRotateMatrixZ(float z) {
        Matrix4f zM = new Matrix4f();

        Matrix4f.rotate((float) Math.toRadians(z), new Vector3f(0,0,1), zM, zM);

        return zM;
    }

Render Code:

GL30.glBindVertexArray(cube_model.vaoID);
                    GL20.glEnableVertexAttribArray(0);
                    GL20.glEnableVertexAttribArray(1);



                        Matrix4f translateM = Maths.createTranslateMatrix(new Vector3f(0.0f, Display.getWidth() - 200f, 0.0f));
                    Matrix4f rotateM = Maths.createRotateMatrixZ(increaseRotation);
                    Matrix4f translateM2 = Maths.createTranslateMatrix(new Vector3f(0.0f, 0.0f, 0.0f));


                    Matrix4f modelM = new Matrix4f();
                    modelM.setIdentity();

                    Matrix4f.mul(translateM2, rotateM, modelM);
                    Matrix4f.mul(modelM, translateM, modelM);


                    shader.loadModelMatrix(modelM);



                        GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);

                        increaseRotation += 1f;
                        if(increaseRotation == 360.0f) {
                            increaseRotation = 0;
                        }


                    GL20.glDisableVertexAttribArray(0);
                    GL20.glDisableVertexAttribArray(1);
            GL30.glBindVertexArray(0);

Quad:

    private static final float vertices[] = {
            -Display.getWidth() / 5, 0.0f, 0.0f,                        1.0f,1.0f,1.0f,1.0f,
            Display.getWidth() / 5, 0.0f, 0.0f,                         0.0f,1.0f,1.0f,1.0f,
            -Display.getWidth() / 5, Display.getHeight() / 5, 0.0f,     1.0f,1.0f,1.0f,1.0f,

            Display.getWidth() / 5, 0.0f, 0.0f,                         1.0f,1.0f,1.0f,1.0f,
            Display.getWidth() / 5, Display.getHeight() /5, 0.0f,       0.0f,1.0f,1.0f,1.0f,
            -Display.getWidth() / 5, Display.getHeight() / 5, 0.0f,     1.0f,1.0f,1.0f,1.0f
    };

Ortho Matrix:

public static Matrix4f createOrthoMatrix(final float l, final float r, final float b, final float t, final float n, final float f ) {
         Matrix4f orthoM = new Matrix4f();

         orthoM.m00 = 2 / (r - l);
         orthoM.m03 = -(r+l)/(r-l);
         orthoM.m11 = 2/(t-b);
         orthoM.m13 = -(t+b)/(t-b);
         orthoM.m22 = -2/(f-n);
         orthoM.m23 = -(f+n)/(f-n);
         orthoM.m33 = 1;


        return orthoM;


    }

Ortho Matrix init:

        loadProjectionMatrix(Maths.createOrthoMatrix(-Display.getWidth(), Display.getWidth(), -Display.getHeight(), Display.getHeight(), 0.1f, 1000));

I want the object to rotate IN place not rotate around some of its edges.

1
I have updated the code there is Quad in the post now.user5712191
Ok. Your triangle is centered around the z-axis. Then why do you use those translation matrices? They basically move the rotation origin away from the world center.BDL
I was trying stuff out . I dont quite understand what you mean? Thank you for helping me can you please tell me what should I do so I can understand? This is not homework its my own project that I am creating in order to learn OpenGL. Just now I've tried using only createRotationMatrix and its doing the same. Its rotating around some point its not rotating in place like a wheel.user5712191

1 Answers

0
votes

Rotations always happen around the origin of the coordinate system. So you first have to translate your geometry to the origin, then rotate and then translate back.