java - 如何找到 libgdx 中两个对象之间的距离

标签 java android 3d libgdx

在我的 LibGdx 游戏中,我有路标 (ModelInstance),我需要知道它们之间的距离。 我尝试使用 Transform.getTranslation(new Vector3) 但它返回模型的翻译。我还没有找到获取模型全局位置的方法。

最佳答案

要获取两个 vector 之间的距离,请使用 dst 方法:

public float dst(Vector3 vector)
Specified by:
dst in interface Vector<Vector3>
Parameters:
vector - The other vector
Returns:
the distance between this and the other vector

Vector3 reference

跟踪模型的最佳方法是创建一个包装器来存储对象位置 (Vector3) 和旋转 (Quaternion),并在该包装器中设置渲染方法上的模型位置,如下所示:

public abstract class ModelWrapper{

    private ModelInstance model;
    private Vector3 position;
    private Quaternion rotation;

    public ModelWrapper(ModelInstance model,Vector3 position,Quaternion rotation) {
        this.model = model;
        this.position = position;
        this.rotation = rotation;
    }

    public ModelInstance getModel() {
        return model;
    }

    public void setModel(ModelInstance model) {
        this.model = model;
    }

    public Vector3 getPosition() {
        return position;
    }

    public void setPosition(Vector3 position) {
        this.position = position;
    }

    public Quaternion getRotation() {
        return rotation;
    }

    public void setRotation(Quaternion rotation) {
        this.rotation = rotation;
    }

    public void render(ModelBatch modelBatch, Environment  environment) {
        this.model.transform.set(this.position,this.rotation);
        modelBatch.render(model, environment);
    }
}

关于java - 如何找到 libgdx 中两个对象之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026407/

相关文章:

json - Three.js JSONLoader blender 模型错误 : property 'length' undefined

delphi - TPlane 与 TImage3D?

java - 强制 Java 虚拟机运行垃圾收集器

Java:无法转换为自定义类

java - Android-错误膨胀类 fragment

android - youtube Android 应用程序是否使用 "DIAL"协议(protocol)?

java - 如何配置和打包简单的 Java 应用程序以使用 JPA

java - 为什么 Object 成员变量在 Java 中不能既是 final 又是 volatile?

android - 无法使用 kotlin 将 Realm 集成到 android 库模块中

javascript - Three.js - 正交相机