有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使项目符号向三维空间中的点移动

我目前正在用JavaLWJGL制作一个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) 个答案

  1. # 1 楼答案

    你要做的是获得所需的速度,使你的子弹更接近你的目标(这里是鼠标)endPoint

    首先,得到两个endPoint.sub(position);之间的向量

    然后你{}用它来获得方向

    scale()用你想要的速度来获得瞬间的速度

    然后你{}让它朝着目标移动