简体   繁体   English

lwjgl的实体坐标出现问题

[英]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. 我在OpenGL上下文中移动实体时遇到麻烦:当我尝试放置一个实体时,位置似乎正确,但是当该实体开始移动时,一切都出错了,并且碰撞不起作用。 I'm new to OpenGL, and I suspect my world matrix or model matrix to be wrong. 我是OpenGL的新手,我怀疑我的世界矩阵或模型矩阵是错误的。

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: 这是可计算大多数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 . 另外,还有指向github项目的链接: https : //github.com/ShiroUsagi-san/opengl-engine

I'm really new to OpenGL 3 so I could have done some really big mistakes. 我真的是OpenGL 3新手,所以我可能犯了一些非常大的错误。

I'm also running i3 as WM, I don't really know if this could lead to this kind of issues. 我也将i3作为WM运行,我真的不知道这是否会导致此类问题。

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 在考虑了openGL和VBO的工作原理后,我解决了这些问题:确实,我为每个实体设置了新的参考,所以我不得不改变这条线

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. 我在VBO中的顶点位置和我的世界中的位置之间感到困惑。 Hope that misunderstood helps people to understand. 希望被误解有助于人们理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM