Search code examples
matrixlwjgltransformationjoml

Object is rotating weidly when using JOML


I am trying to create a 3D engine using LWJGL3 and i keep getting this issue:

When rotating an object it does this: Quad

The quad SHOULD be in the middle, as i didn't change the x coordinate, but it isn't. I actually tried to redo the transformation matrix using the old utilities jar from LWJGL2 and the quad rotated on its axis, not in some sort of orbit around the middle.(btw i am using the latest version of JOML)

When i searched on google about the issue i:

  1. Could not understand anything.
  2. The things that i understood wouldn't work (like updating JOML)

Here is the code that generates the transformation matrix:

public static Matrix4f createTransformationMatrix(Entity entity) {
        Matrix4f matrix = new Matrix4f()
        .identity()
        .translate(new Vector3f(entity.getX(), entity.getY(), entity.getZ()))
        .rotateX((float)Math.toRadians(entity.getRotationX()))
        .rotateY((float)Math.toRadians(entity.getRotationY()))
        .rotateZ((float)Math.toRadians(entity.getRotationZ()))
        .scale(entity.getScale());
        
        return matrix;
}

Here is the code from the vertex shader:

#version 450

in vec3 position;
out vec4 out_color;

uniform mat4 projection;
uniform mat4 transformation;

void main()
{
    gl_Position =  projection * transformation * vec4(position, 1.0);
    out_color = vec4(position.y, position.x, -position.x, 0);
}

Thanks in advance!


Solution

  • I just found out what the problem is.

    This where my quad coordinates:

    float[] vertices = {
            -0.5f, 0.5f, -1f,
            -0.5f, -0.5f, -1f,
            0.5f, -0.5f, -1f,
            0.5f, 0.5f, -1f
    };
    

    So i changed them to:

    float[] vertices = {
            -0.5f, 0.5f, 0f,
            -0.5f, -0.5f, 0f,
            0.5f, -0.5f, 0f,
            0.5f, 0.5f, 0f
    };
    

    And it FINALLY worked but no idea why.