在lwjgl / java中为基于方向的移动增加速度

时间:2014-02-24 12:10:54

标签: java 3d controls lwjgl game-physics

好吧所以我刚刚开始进入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())));

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

1 个答案:

答案 0 :(得分:0)

你需要将你的方向乘以你的速度,而不是因为我猜你现在正在做的事情。试试这个:

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