2017-11-30 13 views
-1

私はベクトルポイントのリストを持っています。ベクトルポイントのリストは、オブジェクトが従う直線セグメントのパスを定義します。現在、私はこのようなパスに沿って運動をアニメーション化するために、線形補間を行います。一定速度で直線セグメントのパスに沿って移動する

public class Demo 
{ 
    public float speed = 1; 
    private List<Vector3> points; 

    private float t; // [0..1] 

    private Vector3 Evaluate(float t) 
    { 
     // Find out in between which points we currently are 
     int lastPointIndex = GetLast(t); 
     int nextPointIndex = GetNext(t); 

     // Obviously, I need to somehow transform parameter t 
     // to adjust for the individual length of each segment. 

     float segmentLength = GetLength(lastPointIndex, nextPointIndex); 

     // But how would I do this? 
     return Vector3.Lerp(points[lastPointIndex], points[nextPointIndex], t); 
    } 

    public void Update() 
    { 
     // Curve parameter t moves between 0 and 1 at constant speed. 
     t = Mathf.PingPong(Time.time * speed, 1); 

     // Then just get the evaluated position for the curve time, but 
     // this gives variant speed if points are not evenly spaced. 
     Vector3 position = Evaluate(t); 
     SetObjectPosition(position); 
    } 
} 

私は一定の速度を達成することを実現、私は、各セグメントの長さを考慮してパラメータtを再スケールする必要がありますが、私は思えますどのように正確に見つけることができないようにする。

私も知っている、私は私の希望のスピードで次のポイントに向かって移動し、方向を変えるだけで、近くにいたり、次のセグメントですが、これは実際に各セグメントの正確な長さを知っており、正確にこれを補間できるはずです。

答えて

0

これは実際には静かです。まず、オブジェクトに必要な速度を定義します。たとえば、1秒あたり6台です。つまり、線分の長さが6単位の場合、オブジェクトの開始点から終了点までは1秒かかります。これはまた、その長さの半分(つまり3単位)の線分を持つ場合、オブジェクトを0.5秒かけて交差させることを意味します。だから、あなたがしなければならないことは、あなたのすべての線分の長さを計算し、それを行く速度で割ります。(3/6 = 0.5 = scaleFactor)。 0と1の間の補間の代わりに、0と1*scaleFactorの間を補間します。コードは次のようになります。

public class Demo 
{ 
public float speed = 1; 
private List<Vector3> points; 

private float t; // [0..1] 

private Vector3 Evaluate(float t) 
{ 
    // Find out in between which points we currently are 
    int lastPointIndex = GetLast(t); 
    int nextPointIndex = GetNext(t); 

    float segmentLength = GetLength(lastPointIndex, nextPointIndex); 
    float scaleFactor = segmentLength/speed; 

    // note that I divided t by scaleFactor instead of multiplication. 
    // That's because Lerp always takes an interval of [0..1]. So, we 
    // adjust the curve parameter instead. 
    return Vector3.Lerp(points[lastPointIndex], points[nextPointIndex], t/scaleFactor); 
} 

public void Update() 
{ 
    // Curve parameter t moves between 0 and 1 at constant speed. 
    t = Mathf.PingPong(Time.time * speed, 1); 

    // Then just get the evaluated position for the curve time, but 
    // this gives variant speed if points are not evenly spaced. 
    Vector3 position = Evaluate(t); 
    SetObjectPosition(position); 
} 
} 
関連する問題