java - LWJGL 平滑动画

标签 java opengl lwjgl

我已经编写了一些从高度图渲染 3D 世界的代码,现在我也编写了允许行走的代码。问题是,当高度增加时,动画几乎“跳跃”,因为每个单位都相当大。有没有一种简单的方法可以使动画更流畅? (总的来说,我对 OpenGL 和 3D 渲染非常陌生,所以我不想涉足插值和更复杂的事情。)

最佳答案

您是否正在寻求有关如何顺利移动的帮助?就像加速一样?

如果是这样,你可以这样做:

你的玩家类应该有一个位置、速度和一个方向 vector 。不断地将速度添加到您的位置,就像这样

//mainloop...
{
position.add(velocity);
}

静止时,您的速度为<0,0,0>,因此没有位置变化,要移动播放,只需将您的方向的百分比添加到速度中,如下所示。

if(Key.W)
velocity.add(direction);

这基本上是加速。现在要停止,您应该将方向 vector 的百分比添加到速度中,但方向相反。

public class Camera
{
    public final static Camera VIEW = new Camera();
    //
    public Vector3 position; 
    public Vector3 directionalVelocity, angularVelocity;
    public Vector3 rotation;
    public Vector3 cameraDirectionVector; // vector pointing in direction of camera

    public Camera()
    {
        this.position = new Vector3(0,0,0);
        this.rotation = new Vector3(0,0,0);
        this.directionalVelocity = new Vector3(0, 0, 0);
        this.angularVelocity = new Vector3(0, 0, 0);
        this.cameraDirectionVector = new Vector3(0, 0, 0);
        Mouse.setGrabbed(true);
    }
    public void setPosition(float x, float y, float z)
    {
        this.position.set(x, y, z);
    }
    public void setRotation(float x, float y, float z)
    {
        this.rotation.set(x, y, z);
    }

    private float walkAcc = 0.2f;
    private float mouseAcc = 0.2f;

    public void update()
    {
        GL11.glLoadIdentity(); // reset matrix
        GL11.glRotatef(rotation.x, 1, 0, 0);
        GL11.glRotatef(rotation.y, 0, 1, 0);
        GL11.glRotatef(rotation.z, 0, 0, 1);
        GL11.glTranslatef(position.x, position.y, position.z);
        //
            // mouse Input , increment angularVelocity
        if(Mouse.isGrabbed())
        {
            float mouseX = Mouse.getDX() * mouseAcc;
            float mouseY = Mouse.getDY() * mouseAcc;
            if(mouseY > 0 && this.rotation.x > -90)
                this.angularVelocity.x -= mouseY;
            if(mouseY < 0 && this.rotation.x < 90)
                this.angularVelocity.x += -mouseY;
            if(mouseX > 0)
                this.angularVelocity.y += mouseX;
            if(mouseX < 0)
                this.angularVelocity.y -= -mouseX;
                    // slow down mouse velocity
            this.applyFriction(this.angularVelocity, 0.4f);  
        }
        //
            // Keyboard input , add direction to velocity
        if(Keyboard.isKeyDown(Keyboard.KEY_W))
            this.walkForward(this.walkAcc);
        if(Keyboard.isKeyDown(Keyboard.KEY_S))
            this.walkBackwards(this.walkAcc);
        if(Keyboard.isKeyDown(Keyboard.KEY_D))
            this.strafeRight(this.walkAcc);
        if(Keyboard.isKeyDown(Keyboard.KEY_A))
            this.strafeLeft(this.walkAcc);
        if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
            this.directionalVelocity.y -= walkAcc;
        if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
            this.directionalVelocity.y += walkAcc;
            //
        //slow down walk speed
        this.applyFriction(this.directionalVelocity, 0.09f);
        //
            // add velocity to position
        this.position.add(this.directionalVelocity);
        this.rotation.add(this.angularVelocity);
        //
        this.updateCameraDirectionVector();
    }

   // Calculates a vector pointing in the direction of your cross hair
    public Vector3 updateCameraDirectionVector() 
    {

        //x = rsin(angle1)cos(angle2)
        //y = rsin(angle1)sin(angle2)
        //z = rcos(angle1)

        this.cameraDirectionVector.x = (float) (Math.sin(Math.toRadians(rotation.y)) * Math.cos(Math.toRadians(rotation.x)));
        this.cameraDirectionVector.z = (float) (-Math.cos(Math.toRadians(rotation.y)) * Math.cos(Math.toRadians(rotation.x)));
        this.cameraDirectionVector.y = (float) -Math.sin(Math.toRadians(rotation.x)); // good
        return this.cameraDirectionVector;
    }
    public void applyFriction(Vector3 vector, float speed)
    {
        //add a percentage of opposite direction to current direction
        vector.x += -vector.x * speed;
        vector.y += -vector.y * speed;
        vector.z += -vector.z * speed;
    }
    public void walkForward(float distance)
    {
        this.directionalVelocity.x -= this.cameraDirectionVector.x * distance;
        this.directionalVelocity.y -= this.cameraDirectionVector.y * distance;
        this.directionalVelocity.z -= this.cameraDirectionVector.z * distance;

    }
    public void walkBackwards(float distance)
    {
        this.directionalVelocity.x += this.cameraDirectionVector.x * distance;
        this.directionalVelocity.y += this.cameraDirectionVector.y * distance;
        this.directionalVelocity.z += this.cameraDirectionVector.z * distance;

    }
    public void strafeLeft(float distance)
    {
        // cross product with (0,1,0) ... y unit vector..
        this.directionalVelocity.x += -this.cameraDirectionVector.z * distance;
        this.directionalVelocity.z += this.cameraDirectionVector.x * distance;

    }
    public void strafeRight(float distance)
    {
        // cross product with -(0,1,0) ... y unit vector..
        this.directionalVelocity.x -= -this.cameraDirectionVector.z * distance;
        this.directionalVelocity.z -= this.cameraDirectionVector.x * distance;

    }
    @Override
    public String toString()
    {
        return this.position.x + " " + this.position.y + " " + this.position.z;
    }
}

//这是一个基本的 vector 类,我对其进行了简化,以便于阅读。

public class Vector3
{
   public float x,y,z;
   public Vector3(float x,float y, float z)
   {
   this.x = x;
   this.y = y;
   this.z = z;
   }
   public void add(Vector3 v)
   {
    this.x += v.x;
    this.y += v.y;
    this.z += v.z;
   }
 public void set(float x, float y ,float z)
   {
    this.x = x;
    this.y = y;
    this.z = z;
   }

}

关于java - LWJGL 平滑动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15328187/

相关文章:

java - 在 java 中读取和使用文件中的嵌套列表

java - AJP 连接器和 Tomcat 8.5.54 之间的网关超时问题

使用 JavaFX 属性包装 JavaBean

java - MyBatis/Ibatis :- Help regarding query in Ibatis/Mybatis

opengl - 为什么 glGetUniformLocation 让我失望?

java - 过多的native调用并不影响速度

java - OpenGL:单个顶点缓冲区中的两个球体

java - lwjgl - 编辑器视口(viewport)

opengl - 片段着色器并为纹理着色

c++ - glm lookAt 和矩阵变换产生奇怪的行为