以一定的速度移动我的物体

时间:2012-04-10 09:22:27

标签: android opengl-es

考虑一个应该在道路上前进的汽车对象。我还没有车对象,但我稍后会添加这个形状。现在我可以用特定的速度向前移动它而不是汽车吗?

任何想法?

这是我的代码

public class GLqueue {
private float vertices[] = { 1, 1, -1, // topfront
        1, -1, -1, // bofrontright
        -1, -1, -1, // botfrontleft
        -1, 1, -1, 
        1, 1, 1, 
        1, -1, 1, 
        -1, -1, 1, 
        -1, 1, 1,

};

private FloatBuffer vertBuff;
private short[] pIndex = { 3, 4, 0,  0, 4, 1,  3, 0, 1, 
        3, 7, 4,  7, 6, 4,  7, 3, 6, 
        3, 1, 2,  1, 6, 2,  6, 3, 2, 
        1, 4, 5,  5, 6, 1,  6, 5, 4,

};
private ShortBuffer pBuff;

public GLqueue() {

    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);

    ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 4);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);

}

public void draw(GL10 gl) {
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length,
            GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
}

}

1 个答案:

答案 0 :(得分:1)

速度取决于两个因素 - 时间和距离: v = d/t “移动”一个物体通常是我改变相对于起始位置的位置。此距离根据上述公式计算: d = vt 这意味着为了在绘制时知道对象的位置,我们必须知道速度和时间。

速度可能由用户或程序以某种方式决定(I.E用户按下按钮以更快地行驶并且速度上升)。 可以通过调用System.currentTimeMillis()来检索当前时间。

以下是一个非常简单的实现示例:

//Variables:
long time1, time2, dt; 
float velocity; 
float direction;

//In game loop:

time1 = System.currentTimeMillis(); dt = time1-time2;

float ds = velocity*dt; //ds is the difference in position since last frame. car.updatePosition(ds, direction); //Some method that translates the car by ds in direction.

time2 = time1;