target leading without using physics

I am not sure whether I should post it here because it’s not related to corona, it’s more of a general game programming question but I’m out of ideas after trying with several friends for 3 days now. Here it goes:

Problem statement:

Target leading algorithm for tower defense.

Suppose there is a cannon that can shoot at enemies when it comes into its range. example + explanation of algorithm:http://paul.sc/shooting-moving-target/ (you can find the algorithm too in there but somehow is not working for me)

The problem:

  1. The cannon shouldn’t target the enemy directly, should shoot where the enemy will be
  2. We move objects around using transition and supply the x,y and time to get there.
  3. The enemy moves in segments ex: from (0,0) -> (10, 20) -> (50, 30) each segment is a straight line. So targeting at (10, 19) would miss if we don’t take into account the next segment.

Known:

  1. Enemy’s x,y position at all times, time to get to a position
  2. Bullet’s x,y initial position and specified travel time of the bullet (time T) for transition.

Find:
Bullet’s ending position to hit the target with the given time T to be used with the transition function.

We could make the bullets homing but then that would be more costly in processing because:

  1. It has to be in the enterframe function
  2. recalculate the angle + position during the enterframe.

I am out of ideas, it should be simple but I can’t seem to figure it out. [import]uid: 11334 topic_id: 4477 reply_id: 304477[/import]

bump? anyone? [import]uid: 11334 topic_id: 4477 reply_id: 14147[/import]

I am going to assume one dimension ok?

Cannon x = 0
Enemy x = 30

velocity = distance/time so
distance = velocity*time, velocity is in px/frame, time is the transition time(better if multiple of framerate)

so say you are moving your enemy 10 px every frame and your time is 1 second and you are at 30 FPS then
your enemy moves 300 px

say you are moving your bullet at 11 px per frame for 1 second that’s 330px. But the bullet started at 0 (see Cannon x) so they collide at 1 second.

Although, I think you are going about the problem backwards. You usually aim where you want to hit not when you want to hit. So I suggest using enterframe events instead of transitions to move. just say

bullet.x = bullet.x + 10 (this moves the bullet 10 px every frame)

If you don’t want to use either.

I would personally use the physics engine. Turn all objects to sensors and kinematic. That way i wouldn’t have to worry about any animations, just let the physics engine handle it. You could even do coller things like drag, boosts, wind, etc. Much, much easier to do. [import]uid: 8192 topic_id: 4477 reply_id: 14203[/import]

Thank you amigoni, let me try that in
the reason i want to avoid physics is that this is the only part that needs physics, it is an overkill to me to include the whole library to accomplish a simple geometry, which unfortunately I cant’ do :frowning: [import]uid: 11334 topic_id: 4477 reply_id: 14217[/import]