我有一个海战游戏,我试图根据速度和旋转速度向我的用户展示他的船将采取的路线。即,我尝试实现类似于下图的效果。正如你所看到的,一艘能够走红色航线的船,其旋转速度比蓝色的快。现在,我不知何故需要找到一种方法来根据速度、旋转速度和所需方向计算最终的运动矢量,但我还不知道怎么做。任何想法表示赞赏!
2 回答
我猜想因为这是一个游戏而不是一个精确的海上模拟,你只是在寻找一种方法来创建一个轨迹图。
这最好用一个简单的迭代/参数方法来处理,假设一个足够小的时间步长它会产生一个不错的曲线。请记住,曲线没有简单的函数形式,它必须由点数组表示。
伪代码(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 。最终路径将是一条弧线(起点、终点、中心),后跟一条线段。
这可能不是你的确切答案,但我相信你想要的公式是这样的:
这里 U 是物体的四速,Γ 代表坐标系的 64 个连接系数或 Christoffel 符号。请注意,希腊下标有四个可能的值,即时间轴为 0,空间坐标轴为 1-3,并且重复索引用于表示该索引的所有值的总和。
这组四个方程的左侧(一个用于类时和三个类空间的索引 λ 值)是物体的适当加速度 3 向量与从参考的有利位置看到的零时间分量相结合或簿记员坐标系。右手边的第一项列出了在旅行钟上每单位时间 τ,物体四速 U 的类时间(能量/mc)和类空间(动量/m)分量的变化率。
如您所见,它不是 QUITE 线性代数。:)