我目前正在製作一個3D第一人稱射擊遊戲,使用java LWJGL。我想轉向子彈向世界的特定點移動。我設法讓子彈打開Y軸,但不打開X和Z.我怎樣才能讓子彈打開Z軸和X軸,然後朝着點移動?如何讓子彈向三維空間中的某個點移動
這裏是我的子彈類:
package entities;
import org.lwjgl.util.vector.Vector3f;
import models.TexturedModel;
import renderEngine.DisplayManager;
import toolbox.MousePicker;
public class Bullet extends Entity{
private static Vector3f currentRay = new Vector3f();
private static final float RAY_RANGE = 600;
public static boolean reset = true;
public Bullet(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale) {
super(model, position, rotX, rotY, rotZ, scale);
}
public void move(Bullet b){
float distance = 2 * DisplayManager.getFrameTimeSeconds();
currentRay = MousePicker.calculateMouseRay();
Vector3f endPoint = MousePicker.getPointOnRay(currentRay, 10000);
//I want my Bullet to move towards the Vector3f endPoint
float zDistance = endPoint.z - this.getPosition().z;
float xDistance = endPoint.x - this.getPosition().x;
double angleToTurn = Math.toDegrees(Math.atan2(xDistance, zDistance));
this.setRotY((float)angleToTurn);
float dx = (float) (distance * Math.sin(Math.toRadians(super.getRotY())));
float dz = (float) (distance * Math.cos(Math.toRadians(super.getRotY())));
super.increasePosition(dx, 0, dz);
}
}
你確定你想要得到的3個角度旋轉移動? 也許你可以考慮即時速度設置爲: targetPosition.sub(bulletPossition).normalize()scalarMult(bullet.maxSpeed) ,然後加入這個瞬間提速的位置上的每個更新循環 (很抱歉,如果該方法是不完全相同的Vector3f提供,這是我的一段時間:))。 – Pignic
什麼是targetPostion? – Disser
對不起,這是你稱爲endPoint – Pignic