我上周写了这台相机,一切正常,接受我不如何使用外观位置作为 Z 位置的事实。
如果你尝试一下,你会亲眼看到的。
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.toRadians;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Vector3f;
public class Camera{
private Vector3f position = null; //X, Y, Z position of the PandaCam
private float yaw = 0.0f; //Yaw of the PandaCam
private float pitch = 0.0f; //Pitch of the PandaCam
private float dx = 0.0f, dy = 0.0f;
private float multi = 0.005f;
private float moveSpeed = 0.25f;
private float mouseSensitivity = 0.02f;
private float tDelta = getDelta();
private long lastFrame, time;
/**
* Create a new PandaCam
* default:
* x = 0
* y = 0
* z = 0
*/
public Camera(){
position = new Vector3f(0, 0, 0);
}
/**
* Create a new PandaCam
* default:
* z = 0
*
* @param x Set the Starting X position of the camera
* @param y Set the Starting Y position of the camera
*/
public Camera(float x, float y){
position = new Vector3f(x, y, 0);
}
/**
* Create a new PandaCam
*
* @param x Set the Starting X position of the camera
* @param y Set the Starting Y position of the camera
* @param z Set the Starting Z position of the camera
*/
public Camera(float x, float y, float z){
position = new Vector3f(x, y, z);
}
/**
* Call this function every frame to update the PandaCam
*/
public void update(){
glLoadIdentity();
mouseInput();
keyboardInput();
setPosition();
}
/**
* @return Returns the delta time.
*/
public int getDelta(){
time = (Sys.getTime() * 1000) / Sys.getTimerResolution();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
/**
* Updates the PandaCam yaw
* @param amount The amount the yaw moves. takes numbers below and above zero
*/
public void yaw(float amount){
yaw -= amount;
}
/**
* Updates the PandaCam pitch
* @param amount The amount the pitch moves. takes numbers below and above zero
*/
public void pitch(float amount){
pitch += amount;
}
/**
* Moves the PandaCam forward.
* @param distance The distance the camera move's forward
*/
public void walkForward(float distance){
calculatePosition(0, 0, multi * distance * 0.003f);
}
/**
* Moves the PandaCam backward.
* @param distance The distance the camera move's backward
*/
public void walkBackwards(float distance){
calculatePosition(0, 0, -multi * distance * 0.003f);
}
/**
* Moves the PandaCam left.
* @param distance The distance the camera move's left
*/
public void strafeLeft(float distance){
calculatePosition(multi * distance * 0.003f, 0, 0);
}
/**
* Moves the PandaCam right.
* @param distance The distance the camera move's right
*/
public void strafeRight(float distance){
calculatePosition(-multi * distance * 0.003f, 0, 0);
}
/**
* Sets the PandaCam position
* @param dx Is an amount that the PandaCam add's or subtracts from the current X
* @param dy Is an amount that the PandaCam add's or subtracts from the current Y
* @param dz Is an amount that the PandaCam add's or subtracts from the current Z
*/
private void calculatePosition(float dx, float dy, float dz){
position.x -= dx * (float) sin(toRadians(yaw - 90)) + dz * sin(toRadians(yaw));
position.y += dy * (float) sin(toRadians(pitch - 90)) + dz * sin(toRadians(pitch));
position.z += dx * (float) cos(toRadians(yaw - 90)) + dz * cos(toRadians(yaw));
}
/**
* Handles the keyboard input
*/
public void keyboardInput(){
//Move forward
if (Keyboard.isKeyDown(Keyboard.KEY_W) || Keyboard.isKeyDown(Keyboard.KEY_UP)){
walkForward(moveSpeed * (tDelta/10));
}
//Move backwards
if (Keyboard.isKeyDown(Keyboard.KEY_S) || Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
walkBackwards(moveSpeed * (tDelta/10));
}
//Strafe left
if (Keyboard.isKeyDown(Keyboard.KEY_A) || Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
strafeLeft(moveSpeed * (tDelta/10));
}
//Strafe right
if (Keyboard.isKeyDown(Keyboard.KEY_D) || Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
strafeRight(moveSpeed * (tDelta/10));
}
}
/**
* Handles mouse Input
*/
public void mouseInput(){
dx = Mouse.getDX();
dy = Mouse.getDY();
yaw(dx * mouseSensitivity);
pitch(dy * mouseSensitivity);
}
/**
* Sets the position of the PandaCam
*/
public void setPosition(){
//Rotate the pitch around the X axis
glRotatef(-pitch, 1.0f, 0.0f, 0.0f);
//Rotate the yaw around the Y axis
glRotatef(-yaw, 0.0f, 1.0f, 0.0f);
//translate to the position vector's location
glTranslatef(position.x, position.y, position.z);
}
public float getMX(){
return Mouse.getX();
}
public float getMY(){
return Mouse.getY();
}
/**
* @return The Pitch position of the PandaCam
*/
public float getPitch(){
return -pitch;
}
/**
* @return The Yaw position of the PandaCam
*/
public float getYaw(){
return -yaw;
}
/**
* @return The X position of the PandaCam
*/
public float getX(){
return position.x;
}
/**
* @return The Y position of the PandaCam
*/
public float getY(){
return position.y;
}
/**
* @return The Z position of the PandaCam
*/
public float getZ(){
return position.z;
}
}