I found this to work so well I did not need collision detection for the shot. Vector bulletPath = aimSpot - tower.position įloat timeToImpact = bulletPath.Length() / bullet.speed //speed must be in units per second Vector aimSpot = target.position + target.velocity * t Here is some pseudo code: Vector totarget = target.position - tower.position įloat a = Vector.Dot(target.velocity, target.velocity) - (bullet.velocity * bullet.velocity) įloat b = 2 * Vector.Dot(target.velocity, totarget) įloat c = Vector.Dot(totarget, totarget) įloat q = (float)Math.Sqrt((b * b) - 4 * a * c) / (2 * a) The way I solved it was by using a quadratic equation. I'm making a tower defense game that basically has the same need for a tower's bullet, a tower wants to fire a gun such that the bullet will intercept a moving target as long as it doesn't change speed/direction. If I understand your question, you do not want the ship to steer to the target, but rather to fly in a straight line that happens to intercept the target.
#Projectile intersection vector 2d plus
A plus would be if it even considers braking (battling at lightspeed is very energy inefficient in the given universe) It should work for every starting speed, not only from stand still. projectile hits the player and you also notice we have a teen both of these for how long its been so we dont know T which is our intersection time so. Preffered implemantation would be one that considers the acceleration capabilities of the friend, and knows when interception is impossible because of the speed. What I want is to set a direct and straight path to the position that the foe will (presumably) will have when firing distance will be reached, assuming that foe will not change course until then.Īs a first and "simple" implementation it would be enough if we assume the friend can speed up from 0 to max in no time. The friendly ship wants to get itself into firing range to battle the foe.Īctually I am setting just a direct vector to the actual position moving ship, and recalculate it every frame, resulting in some kind of "round" flightpath. Given are a twodimensional space, and 1 friendly spaceship standing still, one foe is moving NOT directly to the friendly ship with known actual position, speed and direction.