[英]translation done incorrectly in opengl
i am making a 2d game using opengl in android. 我正在使用Android中的opengl制作2D游戏。 I have a square textured sprite which i intend to use it as a bouncing ball.
我有一个方形纹理的精灵,打算将其用作弹跳球。 The problem i am facing is regarding the translation of the sprite.
我面临的问题是关于精灵的翻译。 I am using a single model matrix as a uniform of my vertex shader.
我使用单个模型矩阵作为顶点着色器的统一体。 i update that matrix before rendering each of the sprites.
我在渲染每个精灵之前更新该矩阵。 Is THIS the right method to do so??
这是这样做的正确方法吗?
I want to make the ball accelerate by using gravity effect but it only translates with constant speed. 我想通过重力效应使球加速,但它只能以恒定速度平移。
Here is the update function of the sprite class :- 这是精灵类的更新功能:
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()));
}
My Vertex shader :- 我的顶点着色器:-
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;
}
My OnDrawFrame Function :- 我的OnDrawFrame函数:-
public void onDrawFrame(GL10 gl) {
textureShaderProgram.useProgram();
ball.update(modelMatrix);
textureShaderProgram.setUniforms(projectionMatrix, modelMatrix);
ball.bindData(textureShaderProgram);
ball.render();
}
Your update function in wrong. 您的更新功能有误。 You translate your
modelMatrix
by speed instead of location. 您可以通过速度而不是位置来转换
modelMatrix
。
Matrix.translateM(modelMatrix, 0, speed.getI(), speed.getJ(), 0);
You probably want something like: 您可能想要类似:
Matrix.translateM(modelMatrix, 0, location.getI(), location.getJ(), 0);
Your speed basicly functions as location now, which is incremented each update: 现在,您的速度基本上就可以用作位置了,每次更新都会增加:
accel.setJ(1);
speed.addVector(accel);
accel.setI(-(0.3f * speed.getI()));
speed
is initially (0, 0)
and is incremented by accel
every update, which will always be (0, 1)
as -(0.3f * 0) = 0
. speed
最初是(0, 0)
,每次更新都会通过accel
递增,当-(0.3f * 0) = 0
(0, 1)
始终为(0, 1)
-(0.3f * 0) = 0
。
Therefore your object moves with a constant speed of (0, 1). 因此,您的对象以(0,1)的恒定速度移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.