我猜因为这是一个游戏而不是一个精确的海上模拟,所以你只是在寻找一种方法来创建一个轨迹图。
这最好使用简单的迭代/参数化方法来处理,假设时间步长足够小,它会形成一条不错的曲线。请记住,曲线没有简单的函数形式,它必须由点数组表示。
伪代码(Matlab / Octave 样式语法)
Given: StartX, StartY, StartHeading, EndX, EndY, MaxSpeed, MaxRotationRate
-----------------------------------------------------------------------------
MaxDisplacement = Max Speed * deltaT
MaxRotation = MaxRotationRate * deltaT
CurrentX = StartX
CurrentY = StartY
CurrentHeading = StartHeading
Trajectory = []
While [CurrentX, CurrentY] != [EndX, EndY]
% Store the Current Position by appending to results
Trajectory = [Trajectory; [CurrentX, CurrentY, CurrentHeading]]
% Get the vector form of the current heading and the straight-line path
HeadingVector = [cos(CurrentHeading),sin(CurrentHeading)]
DirectVector = [EndX - CurrentX, EndY - CurrentY]
% Some simple vector math here using dot products and cross products
RequiredRotation = arccos(dotP(HeadingVector,DirectVector)/abs((HeadingVector)*abs(DirectVector))
RotationDirection = sign(crossP(HeadingVector,DirectVector))
% Clip the rotation rate based on the maximum allowed rotation
if RequiredRotation > MaxRotation
RequiredRotation = MaxRotation
% Update the position based on the heading information
CurrentX = CurrentX + cos(RequiredRotation) * MaxDisplacement
CurrentY = CurrentY + sin(RequiredRotation) * MaxDisplacement
CurrentHeading = CurrentHeading + RequiredRotation * RotationDirection
Loop
Return Trajectory
此代码在到达端点时存在一些问题,我将由您决定如何最好地处理它。两个明显的问题:船将超出所写的端点,因为船总是以最大速度移动;如果终点太近而无法驶入,船可能会卡在“轨道”点上。对此有多种解决方法,这取决于您希望您的游戏如何处理此问题。
几何解
另一种方法是做一些更硬核的几何计算(精确解)。
首先,您需要计算转弯半径而不是最大转弯率。从那里,给定船的当前位置和航向,可以确定船可以采取的两个“转弯圈”。选择正确的中心点C,然后在圆上计算正确的切点T。最终路径将是一条弧线(起点、终点、中心),然后是一条线段。