1

次のコードを使用して、ポイントA(右上)からポイントB(左下)までの増分を計算しています。しかし、ポイントBに近づくにつれて、私の増分は予想されるパスからどんどん外れていきます。写真の緑色の線は、白い点の予想される経路です。

不一致

    public function get target():Point { return _target; }
    public function set target(p:Point):void
    {
        _target = p;
        var dist:Number = distanceTwoPoints(x, _target.x, y, _target.y); //find the linear distance
        //double the steps to get more accurate calculations. 2 steps are calculated each frame
        var _stepT:Number = 2 * (dist * _speed); //_speed is in frames/pixel (something like 0.2)

        if (_stepT < 1) //Make sure there's at least 1 step
            _stepT = 1;
        _stepTotal = int(_stepT); //ultimately, we cannot have half a step
        xInc = (_target.x - x) / _stepT; //calculate the xIncrement based on the number of steps (distance / time)
        yInc = (_target.y - y) / _stepT;
    }

    private function distanceTwoPoints(x1:Number, x2:Number, y1:Number, y2:Number):Number 
    {
        var dx:Number = x1-x2;
        var dy:Number = y1-y2;
        return Math.sqrt(dx * dx + dy * dy);
    }

基本的に、私はアイデアがありません。白い点が緑の線に正確に従うように見える唯一のことは、次のようにターゲットの位置を調整することです。

distanceTwoPoints(x, _target.x + 2, y, _target.y + 1);
//...
xInc = (_target.x + 2 - x) / _stepT;
yInc = (_target.y + 1 - y) / _stepT;

ただし、これにより、ポイントA(右上)に入るなど、ポイント間に角度がないシミュレーションの他の部分が破棄されます。これにより、2点間の距離を実際よりも短く計算する必要があると思います。何か案は?

4

1 に答える 1

9

Flashには、これに非常に便利な優れた機能があります。Point.interpolate(pointA, pointB, number)ポイントAとポイントBの間のポイントを返します。3番目の入力(数値)は、ポイントAまたはポイントBにどれだけ近いか、0から1までのポイントです。その値を計算する必要があります。

内挿は基本的に2つの入力ポイントの加重平均であり、数値は1つのポイントに対する重みです。数値が0.5の場合、2つの入力ポイントの中間にポイントがあります。1はPointAを返し、0はPointBを返します。

詳細については、 flash.geom.Point.interpolate()を参照してください。

他の言語、または一般的な数学の場合、この方法で行うことができます。トリガーは必要ありません:point1、原点、およびpoint2終点。とpoint3の間のポイントです。からの比率であり、どのくらい先に進むかです。に向かっての道の4分の1になります。および。これは、線上の点など、0-1以外の値でも機能しますが、それらの間では機能しません。point1point2locpoint1point2loc = .25point1point2point3.x = point1.x * (1 - loc) + point2.x * locpoint3.y = point1.y * (1 - loc) + point2.y * locpoint1point2

于 2012-04-25T14:20:33.303 に答える