在 LWJGL/Java 中为基于方向的移动增加速度



好的,所以我刚刚开始进入 3D 游戏编程,我有一个基本的世界,我有自己的物理和基本控制,但是每当我考虑移动速度时,它就会抛弃我的方向计算,我不知道如何解决这个问题

这是我的相机课

package org.phin.platformer3d.game;
 import org.lwjgl.input.Keyboard;
 import org.lwjgl.opengl.GL11;
 import org.lwjgl.util.glu.GLU;
 import org.phin.platformer3d.lib.Strings;
 import org.phin.platformer3d.object.GameObject;
 public class Camera extends GameObject {
/**
 * the field of view angle 
 */
private float fovAngle;
/**
 * the ratio between points on a object
 */
private float aspectRatio;
/**
 *  nearest clipping point 
 *  (the near render distance so objects are not rendered into the camera
 */
private float near;
/** 
 * farthest clipping point (render distance)
 */
private float far;
private float amtJumped;
private boolean blockKeyUp = false;
private boolean blockKeyDown = false;
/**
 * initializes the <code>field of view angle, aspect ratio, near view clipping and 
 * far clipping (render distance) </code>
 * 
 * @param fovAngle
 * @param ar
 * @param near
 * @param far
 *
 */
public Camera(float fovAngle, float ar, float near, float far) {
    this.fovAngle = fovAngle;
    this.aspectRatio = ar;
    this.near = near;
    this.far = far;
    this.initGL();
}
/**
 * Initializes openGL
 */
private void initGL() {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GLU.gluPerspective(this.fovAngle, this.aspectRatio, this.near, this.far);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
}
/**
 * creates and updates the camera view (called every frame)
 */
public void updateView() {
    GL11.glRotatef(super.getRotX(), super.getX(), 0, 0);
    GL11.glRotatef(super.getRotY(), 0, super.getY(), 0);
    GL11.glRotatef(super.getRotZ(), 0, 0, super.getZ());
    GL11.glTranslatef(super.getX(), super.getY(), super.getZ());    
}
public void setAmtJumped(float amt) {
    this.amtJumped = amt;
}
public void setKeyBlocked(String key, boolean b) {
    if (key.equals("up")) {
        this.blockKeyUp = b;
    } else if (key.equals("down")) {
        this.blockKeyDown = b;
    } 
}
/**
 * translates the camera based on the user key input
 */
private void keyEvent() {
    if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
        if (!this.blockKeyUp) {
            this.blockKeyDown = false;
            super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY() + 90)));
            super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY() + 90)));
        }
    } else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {    
        if (!this.blockKeyDown) {
            this.blockKeyUp = false;
            super.setX(super.getX() + (float) Math.cos(Math.toRadians(super.getRotY() + 90)));
            super.setZ(super.getZ() - (float) Math.sin(Math.toRadians(super.getRotY() + 90)));
        }
    }
    // rotate left or right
    if (Keyboard.isKeyDown(Keyboard.KEY_D)) {   
        this.blockKeyUp = false;
        this.blockKeyDown = false;
        super.setRotY(super.getRotY() - Strings.SENSITIVITY);
    } else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
        this.blockKeyDown = false;
        this.blockKeyUp = false;
        super.setRotY(super.getRotY() + Strings.SENSITIVITY);
    }
    // Strife left or right
    if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
        super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY())));
        super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY())));
    } else if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
        super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY() + 180)));
        super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY() + 180)));
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
        this.blockKeyDown = false;
        this.blockKeyUp = false;
        if (this.amtJumped < Strings.JUMP_HEIGHT - super.getHeight()) {
            super.setY(super.getY() - 2.5F); // 2.5
            this.amtJumped += 2.5F;
        } else {
            this.amtJumped = 10000;
        }
    }
}
public void update() {
    this.keyEvent();
    // camera gravity
    super.setY(super.getY() + Strings.GRAVITY);
    this.updateView();
}
// no need to render the camera
public void render() {}

}

的发生

super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY())));
super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY())));

是我需要添加速度的地方,但是当我这样做时会抛出计算。如何在不甩掉速度的情况下增加速度?

你需要将你的方向乘以你的速度,而不是像我猜你现在所做的那样相加。 试试这个:

super.setX(super.getX() - (float) (speed*Math.cos(Math.toRadians(super.getRotY()))));
super.setZ(super.getZ() + (float) (speed*Math.sin(Math.toRadians(super.getRotY()))));

最新更新