相机根据沿 y 轴的旋转运动
Camera movement according to rotation along the y axis
位置沿 x 轴和 z 轴更新得很好。但是当我转动时它停止工作;
我试过重新编写结构但没有成功。
private static final float RUN_SPEED = 20;
private static final float TURN_SPEED = 20;
private float currentSpeed = 0;
private float currentSidewaysSpeed = 0;
private float currentTurnSpeed = 0;
public void checkInputs(){
if(Keyboard.isKeyDown(Keyboard.KEY_W)) {
this.currentSpeed = -(RUN_SPEED);
}else if(Keyboard.isKeyDown(Keyboard.KEY_S)) {
this.currentSpeed = RUN_SPEED/2;
}else{
this.currentSpeed = 0;
}
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
this.currentTurnSpeed = -TURN_SPEED;
}else if(Keyboard.isKeyDown(Keyboard.KEY_A)){
this.currentTurnSpeed = TURN_SPEED;
}else{
this.currentTurnSpeed = 0;
}
}
public void move(){
checkInputs();
float xDistance = currentSpeed * MainGameHandler.getFrameTimeSeconds();
float zDistance = currentTurnSpeed * MainGameHandler.getFrameTimeSeconds();
float distance = xDistance + zDistance;
float dx = (float) (xDistance * Math.sin(Math.toRadians(-getRotY())));
float dz = (float) (zDistance * Math.sin(Math.toRadians(-getRotY())));
System.out.println(dx + " " + dz);
increasePosition(dx, 0, dz);
}
supost 可以在任何方向工作。我 99.9% 确定我遗漏了一两行代码。预先感谢您的帮助
好吧,我使用旧运动中的一些代码解决了这个问题,这里是最终版本:
private static final float RUN_SPEED = 20;
private static final float TURN_SPEED = 20;
private float currentSpeed = 0;
private float currentSidewaysSpeed = 0;
private float currentTurnSpeed = 0;
public void checkInputs(){
if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
this.currentSpeed = -(RUN_SPEED);
}else if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
this.currentSpeed = RUN_SPEED;
}else{
this.currentSpeed = 0;
}
if(Keyboard.isKeyDown(Keyboard.KEY_W)){
this.currentTurnSpeed = -TURN_SPEED;
}else if(Keyboard.isKeyDown(Keyboard.KEY_S)){
this.currentTurnSpeed = TURN_SPEED/2;
}else{
this.currentTurnSpeed = 0;
}
}
public Vector3f move(){
Vector4f movement = new Vector4f();
Vector3f position = new Vector3f();
checkInputs();
float xDistance = currentSpeed * MainGameHandler.getFrameTimeSeconds();
float zDistance = currentTurnSpeed * MainGameHandler.getFrameTimeSeconds();
float distance = xDistance + zDistance;
float dx = xDistance;
float dz = zDistance;
System.out.println(dx + " " + dz);
movement.x += dx;
movement.z += dz;
Matrix4f invertedViewMatrix = new Matrix4f();
Matrix4f.invert(viewMatrix,invertedViewMatrix);
Matrix4f.transform(invertedViewMatrix,movement,movement);
position.x+=movement.x;
position.z+=movement.z;
return position;
}
只是为同样正在启动 opengl lwjgl 并需要帮助的所有人发帖 =)
位置沿 x 轴和 z 轴更新得很好。但是当我转动时它停止工作;
我试过重新编写结构但没有成功。
private static final float RUN_SPEED = 20;
private static final float TURN_SPEED = 20;
private float currentSpeed = 0;
private float currentSidewaysSpeed = 0;
private float currentTurnSpeed = 0;
public void checkInputs(){
if(Keyboard.isKeyDown(Keyboard.KEY_W)) {
this.currentSpeed = -(RUN_SPEED);
}else if(Keyboard.isKeyDown(Keyboard.KEY_S)) {
this.currentSpeed = RUN_SPEED/2;
}else{
this.currentSpeed = 0;
}
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
this.currentTurnSpeed = -TURN_SPEED;
}else if(Keyboard.isKeyDown(Keyboard.KEY_A)){
this.currentTurnSpeed = TURN_SPEED;
}else{
this.currentTurnSpeed = 0;
}
}
public void move(){
checkInputs();
float xDistance = currentSpeed * MainGameHandler.getFrameTimeSeconds();
float zDistance = currentTurnSpeed * MainGameHandler.getFrameTimeSeconds();
float distance = xDistance + zDistance;
float dx = (float) (xDistance * Math.sin(Math.toRadians(-getRotY())));
float dz = (float) (zDistance * Math.sin(Math.toRadians(-getRotY())));
System.out.println(dx + " " + dz);
increasePosition(dx, 0, dz);
}
supost 可以在任何方向工作。我 99.9% 确定我遗漏了一两行代码。预先感谢您的帮助
好吧,我使用旧运动中的一些代码解决了这个问题,这里是最终版本:
private static final float RUN_SPEED = 20;
private static final float TURN_SPEED = 20;
private float currentSpeed = 0;
private float currentSidewaysSpeed = 0;
private float currentTurnSpeed = 0;
public void checkInputs(){
if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
this.currentSpeed = -(RUN_SPEED);
}else if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
this.currentSpeed = RUN_SPEED;
}else{
this.currentSpeed = 0;
}
if(Keyboard.isKeyDown(Keyboard.KEY_W)){
this.currentTurnSpeed = -TURN_SPEED;
}else if(Keyboard.isKeyDown(Keyboard.KEY_S)){
this.currentTurnSpeed = TURN_SPEED/2;
}else{
this.currentTurnSpeed = 0;
}
}
public Vector3f move(){
Vector4f movement = new Vector4f();
Vector3f position = new Vector3f();
checkInputs();
float xDistance = currentSpeed * MainGameHandler.getFrameTimeSeconds();
float zDistance = currentTurnSpeed * MainGameHandler.getFrameTimeSeconds();
float distance = xDistance + zDistance;
float dx = xDistance;
float dz = zDistance;
System.out.println(dx + " " + dz);
movement.x += dx;
movement.z += dz;
Matrix4f invertedViewMatrix = new Matrix4f();
Matrix4f.invert(viewMatrix,invertedViewMatrix);
Matrix4f.transform(invertedViewMatrix,movement,movement);
position.x+=movement.x;
position.z+=movement.z;
return position;
}
只是为同样正在启动 opengl lwjgl 并需要帮助的所有人发帖 =)