2017-03-26 5 views
2

私のLibGdxゲームでは、私はwaypoins(ModelInstance)を持っており、それらの距離を知る必要があります。 Transform.getTranslation(new Vector3)を使用しようとしましたが、モデルの翻訳を返します。私はモデルの世界的な立場を得る方法を見つけていません。libgdxの距離オブジェクトを見つける方法

答えて

0

2つのベクトル間の距離は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)と回転を(保存するラッパーを作成することですクォータニオン)、このラッパーでレンダリングメソッドのモデル位置を次のように設定します。

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); 
    } 
} 
+0

おかげで、よさそうだが、それは本当に私を助けます。グローバルな空間でのモデルの位置づけはどうですか? – Mike

+0

ModelWrapperインスタンス.getPosition() – Hllink

1

ゲームは2Dまたは3D空間でですか? 2Dで、我々は単にピタゴラスの定理を使用し、当社独自の方法で書くことができた場合 は:

double distance(Vector2 object1, Vector2 object2){ 
    return Math.sqrt(Math.pow((object2.x - object1.x), 2) + Math.pow((object2.y - object1.y), 2)); 
} 
+0

ちょうど私が必要な、感謝! – ROSA

関連する問題