Search code examples
javalwjglopengl-3

trouble with entity coordinates with lwjgl


I've a trouble with moving my entities in a OpenGL context: when I try to place an entity, the position seems correct, but when the entity starts to move, everything is going wrong, and collisions don't work. I'm new to OpenGL, and I suspect my world matrix or model matrix to be wrong.

Here's the code of the vertex shader:

#version 330 core

layout (location=0) in vec3 position;

out vec3 extColor;

uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;
uniform vec3 inColor;
void main()
{
    gl_Position = projectionMatrix * modelMatrix  * vec4(position, 1.0);
    extColor = inColor;
}

Here is the class that computes most of the Matrix:

public class Transformations {
    private Matrix4f projectionMatrix;
    private  Matrix4f modelMatrix;

    public Transformations() {
        projectionMatrix = new Matrix4f();
        modelMatrix = new Matrix4f();
    }

    public final Matrix4f getOrthoMatrix(float width, float height, float zNear, float zFar) {
        projectionMatrix.identity();
        projectionMatrix.ortho(0.0f, width, 0.0f, height, zNear, zFar);
        return projectionMatrix;
    }

    public Matrix4f getModelMatrix(Vector3f offset, float angleZ, float scale) {
        modelMatrix.identity().translation(offset).rotate(angleZ, 0, 0, 0).scale(scale);
        return modelMatrix;
    }
}

Here's the test for collisions:

 public boolean isIn(Pos p) {
        return (p.getX() >= this.pos.getX() &&
                p.getX() <= this.pos.getX() + DIMENSION)
                && (p.getY() >= this.pos.getY() &&
                p.getY() <= this.pos.getY() + DIMENSION);
}

Also, there's a link to the github project: https://github.com/ShiroUsagi-san/opengl-engine.

I'm really new to OpenGL 3 so I could have done some really big mistakes.

I'm also running i3 as WM, I don't really know if this could lead to this kind of issues.


Solution

  • I fixes the issues after thinking about how openGL and VBO work: Indeed, I was setting a new reference for each entity, so I had to change the line

    Mesh fourmiMesh = MeshBuilder.buildRect(this.position.getX(), this.position.getY(), 10, 10);
    

    to

    Mesh fourmiMesh = MeshBuilder.buildRect(0, 0, 10, 10);
    

    It was a confusion that I made between the positions of the vertex in a VBO and the positions in my world. Hope that misunderstood helps people to understand.