次のコードを使用して、ポイント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点間の距離を実際よりも短く計算する必要があると思います。何か案は?