所以我最近开始使用LWJGL编程,我有近2年的Java经验。 我正在努力让相机正常移动(相机处于第一人称视角)。我希望能够使用箭头键移动并根据鼠标位置旋转相机。有没有人知道像这样的相机类的好教程?我找不到任何接近我需要的东西。也许还有一些书可以教一些3D概念?
答案 0 :(得分:2)
我在这里制作的,应该是相当自我解释的
public class Camera {
public static float moveSpeed = 0.5f;
private static float maxLook = 85;
private static float mouseSensitivity = 0.05f;
private static Vector3f pos;
private static Vector3f rotation;
public static void create() {
pos = new Vector3f(0, 0, 0);
rotation = new Vector3f(0, 0, 0);
}
public static void apply() {
if(rotation.y / 360 > 1) {
rotation.y -= 360;
} else if(rotation.y / 360 < -1) {
rotation.y += 360;
}
glLoadIdentity();
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
glTranslatef(-pos.x, -pos.y, -pos.z);
}
public static void acceptInput(float delta) {
acceptInputRotate(delta);
acceptInputGrab();
acceptInputMove(delta);
}
public static void acceptInputRotate(float delta) {
if(Mouse.isGrabbed()) {
float mouseDX = Mouse.getDX();
float mouseDY = -Mouse.getDY();
rotation.y += mouseDX * mouseSensitivity * delta;
rotation.x += mouseDY * mouseSensitivity * delta;
rotation.x = Math.max(-maxLook, Math.min(maxLook, rotation.x));
}
}
public static void acceptInputGrab() {
if(Mouse.isInsideWindow() && Mouse.isButtonDown(0)) {
Mouse.setGrabbed(true);
}
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
Mouse.setGrabbed(false);
}
}
public static void acceptInputMove(float delta) {
boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_W);
boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_S);
boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_D);
boolean keyLeft = Keyboard.isKeyDown(Keyboard.KEY_A);
boolean keyFast = Keyboard.isKeyDown(Keyboard.KEY_Q);
boolean keySlow = Keyboard.isKeyDown(Keyboard.KEY_E);
boolean keyFlyUp = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
boolean keyFlyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
float speed;
if(keyFast) {
speed = moveSpeed * 5;
}
else if(keySlow) {
speed = moveSpeed / 2;
}
else {
speed = moveSpeed;
}
speed *= delta;
if(keyFlyUp) {
pos.y += speed;
}
if(keyFlyDown) {
pos.y -= speed;
}
if(keyDown) {
pos.x -= Math.sin(Math.toRadians(rotation.y)) * speed;
pos.z += Math.cos(Math.toRadians(rotation.y)) * speed;
}
if(keyUp) {
pos.x += Math.sin(Math.toRadians(rotation.y)) * speed;
pos.z -= Math.cos(Math.toRadians(rotation.y)) * speed;
}
if(keyLeft) {
pos.x += Math.sin(Math.toRadians(rotation.y - 90)) * speed;
pos.z -= Math.cos(Math.toRadians(rotation.y - 90)) * speed;
}
if(keyRight) {
pos.x += Math.sin(Math.toRadians(rotation.y + 90)) * speed;
pos.z -= Math.cos(Math.toRadians(rotation.y + 90)) * speed;
}
}
public static void setSpeed(float speed) {
moveSpeed = speed;
}
public static void setPos(Vector3f pos) {
Camera.pos = pos;
}
public static Vector3f getPos() {
return pos;
}
public static void setX(float x) {
pos.x = x;
}
public static float getX() {
return pos.x;
}
public static void addToX(float x) {
pos.x += x;
}
public static void setY(float y) {
pos.y = y;
}
public static float getY() {
return pos.y;
}
public static void addToY(float y) {
pos.y += y;
}
public static void setZ(float z) {
pos.z = z;
}
public static float getZ() {
return pos.z;
}
public static void addToZ(float z) {
pos.z += z;
}
public static void setRotation(Vector3f rotation) {
Camera.rotation = rotation;
}
public static Vector3f getRotation() {
return rotation;
}
public static void setRotationX(float x) {
rotation.x = x;
}
public static float getRotationX() {
return rotation.x;
}
public static void addToRotationX(float x) {
rotation.x += x;
}
public static void setRotationY(float y) {
rotation.y = y;
}
public static float getRotationY() {
return rotation.y;
}
public static void addToRotationY(float y) {
rotation.y += y;
}
public static void setRotationZ(float z) {
rotation.z = z;
}
public static float getRotationZ() {
return rotation.z;
}
public static void addToRotationZ(float z) {
rotation.z += z;
}
public static void setMaxLook(float maxLook) {
Camera.maxLook = maxLook;
}
public static float getMaxLook() {
return maxLook;
}
public static void setMouseSensitivity(float mouseSensitivity) {
Camera.mouseSensitivity = mouseSensitivity;
}
public static float getMouseSensitivity() {
return mouseSensitivity;
}
}
答案 1 :(得分:1)
我最近写了其中一个!以下是一些最重要的部分:
机芯:
public static void updatePosition(int delta) {
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
posX -= (float)(Math.sin(-rotZ*Math.PI/180)*speed);
posY -= (float)(Math.cos(-rotZ*Math.PI/180)*speed);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
posX += (float)(Math.sin(-rotZ*Math.PI/180)*speed);
posY += (float)(Math.cos(-rotZ*Math.PI/180)*speed);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
posX += (float)(Math.sin((-rotZ-90)*Math.PI/180)*speed);
posY += (float)(Math.cos((-rotZ-90)*Math.PI/180)*speed);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
posX += (float)(Math.sin((-rotZ+90)*Math.PI/180)*speed);
posY += (float)(Math.cos((-rotZ+90)*Math.PI/180)*speed);;
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
posZ += speed;
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
posZ -= speed;
}
请注意,posX
,posY
,posZ
,rotX
,rotY
和rotZ
是双倍的。
现在鼠标移动:
public static void updateRotation(int delta) {
if (Mouse.isGrabbed()) {
float mouseDX = Mouse.getDX()*0.128f;
float mouseDY = Mouse.getDY()*0.128f;
if (rotZ+mouseDX>=360)
rotZ += mouseDX-360;
else if (rotZ+mouseDX<0)
rotZ += mouseDX+360);
else
rotX += mouseDX);
if (rotX-mouseDY>=-89&&rotX-mouseDY<=89)
rotation.setX(rotX-mouseDY);
else if (rotX-mouseDY<-89)
rotation.setX(-89);
else if (rotX-mouseDY>89)
rotation.setX(89);
}
}
不要忘记在课程开始时Mouse.setGrabbed(true)
。