[英]adding speed to direction based movement in 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()))));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.