Tuesday, 10 September 2013

Why isn't my homing missile algorithm working?

Why isn't my homing missile algorithm working?

I've taken code that's heavily inspired by this answer but my projectile
is not homing in the way I expect. The initial projectile direction is
often perpendicular to the target. At which point, it does seem to home in
on his direction, but if it "passes" him, it seems to get stuck in place
like it's frozen at a point but then seems to follow the movements the
target makes without moving at its intended speed. I've commented a line
of code that I'm concerned about. He's using V3 and V4 in his algorithm
which I suspect is a typo on his part but I'm not sure. If anyone can help
me with what I'm doing wrong here, I'd be very grateful.
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

No comments:

Post a Comment