java - 如何使子弹向 3 维空间中的一个点移动

标签 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);


}
    }

最佳答案

你想要做的是获得使子弹更接近目标(此处为鼠标)endPoint 所需的速度。

所以首先,你得到两个 endPoint.sub(position);

之间的 vector

然后你normalize()它得到方向。

您以所需的速度缩放()它以获得即时速度。

和你 super.increasePosition(speed.x, speed.y, speed.z); 让它向目标移动

关于java - 如何使子弹向 3 维空间中的一个点移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145649/

相关文章:

java - 了解 Java 的基础知识,学习 Groovy 的正确方法是什么?

c++ - 计算以恒定加速度对齐两个 3D vector 所需的扭矩?

c# - 将 Math.Pow 与 Microsoft Solver Foundation 结合使用

qt - 来自向量 x y z 分量的四元数 - 在 Qt 中

ios - 如何在 SceneKit 中的盒子中创建一个洞?

java - spring-ws:让 EchoEndpoint 工作,而不是返回 404

java - 查找具有最小成员变量的对象

java - 我如何表示这个通用类型

math - 改进了 WebGL 和 ThreeJS 中的区域照明

math - Z80 DAA指令