如何让子弹向三维空间中的一个点移动

时间:2017-02-09 19:53:04

标签: java math 3d lwjgl

我目前正在使用java LWJGL制作3D第一人称射击游戏。我想转动并将子弹移向世界上的指定点。我设法让子弹在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);


}
    }

1 个答案:

答案 0 :(得分:3)

您要做的是获得让子弹靠近目标所需的速度(此处为鼠标)endPoint.sub(position);

首先,你得到两个normalize()

之间的向量

然后你scale()来获取方向。

super.increasePosition(speed.x, speed.y, speed.z);以你想要的速度来获得即时速度。

FOR /?让它向目标移动