Predictive Tower Defense Aiming Code?

I’ve searched for some non-physics predictive Tower Defense aiming code for movable objects and stationary weopons / towers. There was a sample on the code share site some years ago but now it is gone.

Does anyone have the code for such a task maybe? This would help me lot here! :slight_smile:

Thx!

is this a “top view, no gravity, imperfect information” situation?

what does the turret fairly “know” about the target?

it’s position (by sight)? it’s velocity (by derivative of multiple sightings)?? perhaps even its acceleration (by second derivative)???

there are iterative approximations that converge quickly, depending on what the shooter can “know” (without cheating)

Yes, top view, no gravity. I just need a tower bullet with a straight shooting way and constant speed. But it has to hit the enemy, so I need a point to shoot at because of the enemy speed. The speed of the enemy is constant too.

The turret spots the target when it enters a view circles radius. So the target position is known and it should be possible to calculate the speed depending on how far the transition already was done. For the path the target is moving on actually we can also get the angle he is traveling.

Cheating is okay here :slight_smile: … I just want to know the turret where to shoot at, so it hits the target in sight.

those conditions make it pretty easy :slight_smile:

you have two initial positions (let’s say Pb is bullet, Pt is target)

and two velocities (let’s say Vb is bullet, Vt is target)

you want to know the “time” (T) when the two vectors intersect

(or come within some bounding hit radius, whatever)

begin iterating with estimated T = 0, then:

calc “future position” of target:  Pt’ = Pt + T * Vt (equal to initial position at this point)

* calc distance between future target position Pt’ and bullet initial position Pb:  D = |Pt`-Pb|

calc a new time estimate by dividing that distance by magnitude of bullet velocity:  T = D / |Vb|

calc “future position” of target:  Pt’ = Pt + T * Vt

calc “future position” of bullet:  Pb’ = Pb + T * Vb

calc the distance between the two future positions and see if you’re “close enough”, if not iterate again * with new estimate T

how it works:  on the first iteration you shoot “at” the target’s initiial position, and see “how long” it takes to get there.  of course by then target will have moved too, so you recalc and say how long would it take to get THERE?  and of course (again) by the time you get THERE, target has moved a teensy bit more.  so you estimate THAT time, repeat.

about three iterations ought to do it, depending on your tolerances for hit testing.

and of course once you know “where” this intersection will occur you can “aim” for it (set the angle)

[edited to better indicate “where” * the iteration jumps back to in the pseudocode]

Wow! Thank you very much for the detailed answer!

one other thing perhaps not obvious/explicit by the way i worded it - the “aiming” referred to needs occur during every iteration, get it?  you always need to have bullet’s velocity actually “pointing at” the target’s predicted position before you calc bullet’s next predicted position. (just take the delta from Pb to Pt’ as a vector, normalize it, scale it by bullet velocity magnitude, done)  sooner or later (after n iterations) it’ll be pointing “close enough” (you must test for that exit condition) then you’re ready to fire with that final bullet velocity as is (it’ll already be “aimed” from most recent iteration)

is this a “top view, no gravity, imperfect information” situation?

what does the turret fairly “know” about the target?

it’s position (by sight)? it’s velocity (by derivative of multiple sightings)?? perhaps even its acceleration (by second derivative)???

there are iterative approximations that converge quickly, depending on what the shooter can “know” (without cheating)

Yes, top view, no gravity. I just need a tower bullet with a straight shooting way and constant speed. But it has to hit the enemy, so I need a point to shoot at because of the enemy speed. The speed of the enemy is constant too.

The turret spots the target when it enters a view circles radius. So the target position is known and it should be possible to calculate the speed depending on how far the transition already was done. For the path the target is moving on actually we can also get the angle he is traveling.

Cheating is okay here :slight_smile: … I just want to know the turret where to shoot at, so it hits the target in sight.

those conditions make it pretty easy :slight_smile:

you have two initial positions (let’s say Pb is bullet, Pt is target)

and two velocities (let’s say Vb is bullet, Vt is target)

you want to know the “time” (T) when the two vectors intersect

(or come within some bounding hit radius, whatever)

begin iterating with estimated T = 0, then:

calc “future position” of target:  Pt’ = Pt + T * Vt (equal to initial position at this point)

* calc distance between future target position Pt’ and bullet initial position Pb:  D = |Pt`-Pb|

calc a new time estimate by dividing that distance by magnitude of bullet velocity:  T = D / |Vb|

calc “future position” of target:  Pt’ = Pt + T * Vt

calc “future position” of bullet:  Pb’ = Pb + T * Vb

calc the distance between the two future positions and see if you’re “close enough”, if not iterate again * with new estimate T

how it works:  on the first iteration you shoot “at” the target’s initiial position, and see “how long” it takes to get there.  of course by then target will have moved too, so you recalc and say how long would it take to get THERE?  and of course (again) by the time you get THERE, target has moved a teensy bit more.  so you estimate THAT time, repeat.

about three iterations ought to do it, depending on your tolerances for hit testing.

and of course once you know “where” this intersection will occur you can “aim” for it (set the angle)

[edited to better indicate “where” * the iteration jumps back to in the pseudocode]

Wow! Thank you very much for the detailed answer!

one other thing perhaps not obvious/explicit by the way i worded it - the “aiming” referred to needs occur during every iteration, get it?  you always need to have bullet’s velocity actually “pointing at” the target’s predicted position before you calc bullet’s next predicted position. (just take the delta from Pb to Pt’ as a vector, normalize it, scale it by bullet velocity magnitude, done)  sooner or later (after n iterations) it’ll be pointing “close enough” (you must test for that exit condition) then you’re ready to fire with that final bullet velocity as is (it’ll already be “aimed” from most recent iteration)