好的,这个问题会有点抽象。
我有一个图标沿着一条线移动,该线由存储在向量中的一系列坐标表示,我正在遍历它们。坐标之间的距离是可变的。所以有时图标会缓慢而平稳地移动,而有时它会一次跳跃数百个像素。
我很难想出一种算法来将它必须在其之间移动的每组坐标拆分为一组相对坐标,其中数字基于大小,因此无论单个坐标上有多少个坐标,过渡都是平滑的线。
任何想法将不胜感激。谢谢 :)
好的,这个问题会有点抽象。
我有一个图标沿着一条线移动,该线由存储在向量中的一系列坐标表示,我正在遍历它们。坐标之间的距离是可变的。所以有时图标会缓慢而平稳地移动,而有时它会一次跳跃数百个像素。
我很难想出一种算法来将它必须在其之间移动的每组坐标拆分为一组相对坐标,其中数字基于大小,因此无论单个坐标上有多少个坐标,过渡都是平滑的线。
任何想法将不胜感激。谢谢 :)
Take a look at this discussion of the Main Game Loop.
And here's a quote from that page:
At this step, updates to all the objects in the game world are calculated and performed. Usually, a time step value is passed to all of the update methods indicating how much time has passed since the last update ...
You need to know 3 things:
From these, you can calculate the current position of the object.
如果您希望对象以恒定速度移动,我建议使用基于时间的模型,其中您的对象实际上以一定速度(像素/秒)移动。如果您沿着曲线(例如catmull-rom曲线)进行样条曲线,您仍然可以让它击中每个点(ish)。
所以你想沿着一条线从一个初始点 (x0/y0) 移动到一个终点 (x1/y1) 的步数不定,但每一步的最大距离是多少?
这可以通过以下方式完成:
int stepdist = 10; // max pixels per step
double xdiff = x1 - x0;
double ydiff = y1 - y0;
double dist = sqrt( xdiff * xdiff + ydiff * ydiff );
int steps = (int) ( ( dist - 1 ) / stepdist );
if( steps > 0 )
{
xdiff /= steps;
ydiff /= steps;
while( --steps >= 0 )
{
x0 += xdiff;
y0 += ydiff;
moveTo( (int) x0, (int) y0 );
}
}
moveTo( (int) x1, (int) y1 );