2017-02-09 16 views
2

我目前正在製作一個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); 


} 
    } 
+0

你確定你想要得到的3個角度旋轉移動? 也許你可以考慮即時速度設置爲: targetPosition.sub(bulletPossition).normalize()scalarMult(bullet.maxSpeed) ,然後加入這個瞬間提速的位置上的每個更新循環 (很抱歉,如果該方法是不完全相同的Vector3f提供,這是我的一段時間:))。 – Pignic

+0

什麼是targetPostion? – Disser

+0

對不起,這是你稱爲endPoint – Pignic

回答

0

你想要做什麼,以獲得所需的速度,使你的子彈更接近你的目標是(這裏的鼠標)endPoint

因此,首先,你會得到兩個endPoint.sub(position);

之間的矢量然後你normalize()它來獲得方向。

scale()它以您想要的速度獲得即時速度。

,你super.increasePosition(speed.x, speed.y, speed.z);,使其朝着目標

+1

非常感謝!經過兩個月的挫折,它終於奏效了! – Disser

+0

完美,你能通過驗證我的答案來關閉帖子嗎? ;) – Pignic

相關問題