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

时间:2013-09-11 05:05:20

标签: 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

1 个答案:

答案 0 :(得分:1)

根据我的说法,如果你有一个向量(x,y)并且想要围绕原点旋转角度'theta',则新向量(x1,y1)变为:

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

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

(以上可以使用极坐标导出)

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

Vx =速度* cos(phi)

Vy =速度*罪(phi)

编辑2:同样,在采用cos-inverse时,angleinradians可能有多种可能性。您可能必须检查两个向量所在的象限。您的最大转弯速度在任一方向都是50度。因此,该角度的余弦应始终为正。 (余弦仅为90至270度的负值。

编辑3:我认为要获得关于+转弯方向或转弯方向的信息,交叉产品是一个更好的主意。

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

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