次のコードを使用して、ポイント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点間の距離を実際よりも短く計算する必要があると私に思います。何か案は?
これは機能します!どうもありがとうございました!あなたは私の頭痛を緩和しました:) – chris