javascript - 为什么我的寻的导弹算法不起作用?

标签 javascript math 2d game-physics vector-graphics

我采用了深受 this answer 启发的代码但我的射弹没有按照我预期的方式寻的。初始抛射方向通常垂直于目标。在这一点上,它似乎确实朝着他的方向移动,但如果它“经过”了他,它似乎会卡在原地,就像卡住在某个点上一样,但随后似乎会跟随目标所做的运动,而不是按照其预期的方向移动速度。我对我关心的一行代码进行了注释。他在他的算法中使用了 V3 和 V4,我怀疑这是他的拼写错误,但我不确定。如果有人能帮助我解决我在这里做错的事情,我将非常感激。

normalizedDirectionToTarget = root.vector.normalize(target.pos.x - attack.x, target.pos.y - attack.y) #V4
V3 = root.vector.normalize(attack.velocity.x, attack.velocity.y)
normalizedVelocity = root.vector.normalize(attack.velocity.x, attack.velocity.y)

angleInRadians = Math.acos(normalizedDirectionToTarget.x * V3.x + normalizedDirectionToTarget.y * V3.y)
maximumTurnRate = 50 #in degrees
maximumTurnRateRadians = maximumTurnRate * (Math.PI / 180)
signOfAngle = if angleInRadians >= 0 then 1 else (-1)
angleInRadians = signOfAngle * _.min([Math.abs(angleInRadians), maximumTurnRateRadians])
speed = 3
attack.velocity = root.vector.normalize(normalizedDirectionToTarget.x + Math.sin(angleInRadians), normalizedDirectionToTarget.y + Math.cos(angleInRadians)) #I'm very concerned this is the source of my bug
attack.velocity.x = attack.velocity.x * speed
attack.velocity.y = attack.velocity.y * speed
attack.x = attack.x + attack.velocity.x
attack.y = attack.y + attack.velocity.y
<小时/>

编辑:有效的代码

normalizedDirectionToTarget = root.vector.normalize(target.pos.x - attack.x, target.pos.y - attack.y) #V4
normalizedVelocity = root.vector.normalize(attack.velocity.x, attack.velocity.y)
angleInRadians = Math.acos(normalizedDirectionToTarget.x * normalizedVelocity.x + normalizedDirectionToTarget.y * normalizedVelocity.y)
maximumTurnRate = .3 #in degrees
maximumTurnRateRadians = maximumTurnRate * (Math.PI / 180)
crossProduct = normalizedDirectionToTarget.x * normalizedVelocity.y - normalizedDirectionToTarget.y * normalizedVelocity.x
signOfAngle = if crossProduct >= 0 then -1 else 1
angleInRadians = signOfAngle * _.min([angleInRadians, maximumTurnRateRadians])
speed = 1.5
xPrime = attack.velocity.x * Math.cos(angleInRadians) - attack.velocity.y * Math.sin(angleInRadians)
yPrime = attack.velocity.x * Math.sin(angleInRadians) + attack.velocity.y * Math.cos(angleInRadians)
attack.velocity = root.vector.normalize(xPrime, yPrime)
attack.velocity.x *= speed
attack.velocity.y *= speed
attack.x = attack.x + attack.velocity.x
attack.y = attack.y + attack.velocity.y

最佳答案

根据我的说法,如果您有一个向量 (x,y) 并且想要将其绕原点旋转 Angular “theta”,则新向量 (x1,y1) 变为:

x1 = x*cos(theta) - y*sin(theta)

y1 = y*cos(theta) + x*sin(theta)

(以上可以用极坐标推导)

编辑:我不确定我是否理解正确,但如果你知道最终 Angular 速度和绝对值(比如 phi),那么为什么你不能简单地这样做:

Vx = 速度*cos( phi )

Vy = 速度*sin( phi )

编辑2:此外,在取余弦反函数时,弧度 Angular 可以有多种可能性。您可能需要检查两个向量所在的象限。您的最大转向速率为任一方向 50 度。因此,该 Angular 余弦应始终为正。 (余弦仅在 90 到 270 度范围内为负。

编辑3:我认为要获取有关+ve转动方向或-ve转动方向的信息,叉积是一个更好的主意。

编辑 4:如果执行以下操作,Vx/Vy 应该可以工作:

initialAngleInRadians = Math.atan(normalizedVelocity.y / normalizedVelocity.x)
finalAngleInRadians = initialAngleInRadians + angleInRadians
Vx = speed*cos(finalAngleInRadians)
Vy = speed*sin(finalAngleInRadians)

关于javascript - 为什么我的寻的导弹算法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733036/

相关文章:

wpf - WPF中的2D CAD应用程序

java - 按最大数对数组进行排序

javascript - 如何在延迟时间后执行此 JavaScript 调用?

javascript - React循环组件除了一个元素

比较两个整数并在两个数字之间插入比较符号 <,>=

python - IndexError : list assignment index out of range. 如何解决这个问题?

Java问题解决。这个对吗?

javascript - 在 Javascript 中创建玩家边界

javascript - 在 JavaScript 中,如何确保 float 保持在 32 位以下?

不带括号的 JavaScript 交替