我为一个游戏编写了一个运动类,我在让一个物体沿着一条曲线接近一个目的地时遇到了一些麻烦。我已经阅读了多个其他stackoverflow的答案,但没有成功,所以我认为我打开了一个我自己的问题。
这是我的移动课程中的方法,我试图从当前位置Vector2接近目的地Vector2:
public Vector2 CurvedMove()
{
// Where DestinationPosition and CurrentPosition are Vector2's stored in
// the movement class.
float xDistance = DestinationPosition.X - CurrentPosition.X;
float yDistance = DestinationPosition.Y - CurrentPosition.Y;
// Set the center of the circle once (-1 is what _circleCenter is initialized to)
if (_circleCenter.X == -1)
{
// Gets the center point of our circle
_circleCenter.X = CurrentPosition.X + (xDistance / 2.0f);
_circleCenter.Y = CurrentPosition.Y + (yDistance / 2.0f);
}
float diameter = (float)Math.Sqrt((xDistance * xDistance) + (yDistance * yDistance));
float radius = diameter / 2.0f;
float circumference = (float)Math.PI * diameter;
// Speed represents the number of pixels an object can move (along a straight
// line) in a tick
float factor = ((float)Speed) / circumference;
_elapsedAngle += 2.0f * (float)Math.PI * factor;
// Math based off of this answer: http://stackoverflow.com/questions/14096138/find-the-point-on-a-circle-with-given-center-point-radius-and-degree
_currentPosition.X = (_circleCenter.X + (float)(radius * Math.Sin(_elapsedAngle)));
_currentPosition.Y = (_circleCenter.Y - (float)(radius * Math.Cos(_elapsedAngle)));
if (_currentPosition == _destinationPosition)
_reachedDestination = true;
return CurrentPosition;
}
这可能是简单的几何形状或对基本数学的完全误解,但我无法弄明白。有什么想法吗?