我正在使用以下代码计算从 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(右上角)。这让我觉得两点之间的距离需要计算得比实际短。有任何想法吗?