[英]translation done incorrectly in opengl
我正在使用Android中的opengl制作2D游戏。 我有一个方形纹理的精灵,打算将其用作弹跳球。 我面临的问题是关于精灵的翻译。 我使用单个模型矩阵作为顶点着色器的统一体。 我在渲染每个精灵之前更新该矩阵。 这是这样做的正确方法吗?
我想通过重力效应使球加速,但它只能以恒定速度平移。
这是精灵类的更新功能:
public Ball(int textureID) {
texture = textureID;
//Stores location of the center of the ball
location = new Vector(300,350);
//The velocity of ball
speed = new Vector(0, 0);
//gravity acceleration
accel = new Vector(0, 2);
//Geometry of ball
rect = new Geometry.Rectangle(new Geometry.Point(location.getI() - RADIUS,location.getJ() - RADIUS, 0), 2*RADIUS, 2*RADIUS);
//Builder class to create vertex coordinates
builder = new ObjectBuilder(ObjectBuilder.RECTANGLE2D, true);
builder.generateData(rect, 0);
//Vertex Array holds the coordinates
vertexArray = new VertexArray(builder.vertexData);
}
public void update(float[] modelMatrix) {
Matrix.setIdentityM(modelMatrix, 0);
location.addVector(speed);
Matrix.translateM(modelMatrix, 0, speed.getI(), speed.getJ(), 0);
accel.setJ(1);
speed.addVector(accel);
accel.setI(-(0.3f * speed.getI()));
}
我的顶点着色器:-
uniform mat4 u_Matrix;
uniform mat4 u_ModelMatrix;
attribute vec4 a_Position;
attribute vec2 a_TextureCoordinates;
varying vec2 v_TextureCoordinates;
void main() {
v_TextureCoordinates = a_TextureCoordinates;
gl_Position = u_Matrix * u_ModelMatrix * a_Position;
}
我的OnDrawFrame函数:-
public void onDrawFrame(GL10 gl) {
textureShaderProgram.useProgram();
ball.update(modelMatrix);
textureShaderProgram.setUniforms(projectionMatrix, modelMatrix);
ball.bindData(textureShaderProgram);
ball.render();
}
您的更新功能有误。 您可以通过速度而不是位置来转换modelMatrix
。
Matrix.translateM(modelMatrix, 0, speed.getI(), speed.getJ(), 0);
您可能想要类似:
Matrix.translateM(modelMatrix, 0, location.getI(), location.getJ(), 0);
现在,您的速度基本上就可以用作位置了,每次更新都会增加:
accel.setJ(1);
speed.addVector(accel);
accel.setI(-(0.3f * speed.getI()));
speed
最初是(0, 0)
,每次更新都会通过accel
递增,当-(0.3f * 0) = 0
(0, 1)
始终为(0, 1)
-(0.3f * 0) = 0
。
因此,您的对象以(0,1)的恒定速度移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.