This calculates to shoot in the path of the target.
Like how you throw a football, to lead the catcher. Instead of where they
are at that second.

For tower defense, some Bullets fly fast enough you don't have to lead.

Slower bullets fire, then every frame you:
Move the bullet location toward target. ( without leading )
Thats simple homing, but you can skip the steering part a homing missile
has. The trajectory will end up curving, depending on speeds used.

For a straight line you will have to lead. But leading will miss if target
chamges direction or speed.

On Saturday, July 23, 2011, Joe Ranalli <[email protected]> wrote:
> Yes.
>
> So to be more explicit:
>
> 1 Calculate the distance between the source and the target.
> 2 Calculate the time for the bullet to move that distance (i.e. by
dividing this distance by the bullet velocity)
> 3 Calculate newposition (multiply the target velocity by the time
calculated in the previous step, add to original target position)
> 1a Recalculate the distance between the source and newposition
> 2a Recalculate the time for the bullet to move from the source to
newposition (i.e. divide the new distance by the bullet velocity)
> 3a Recalculate newposition using this new time (multiply the target
velocity by the new time calculated in the previous step, add to original
target position)
>
>
>
> On Sat, Jul 23, 2011 at 12:55 PM, Nathan BIAGINI <[email protected]>
wrote:
>>
>> Ok so thanks all for all your replies i will work with all that. I don't
know already if i want to use homing missile style which seems to be easier
to code or to use an algorithm to fire straight to the right spot.
>> To be honest, i'm not native english speaker and it's not always easy to
figure out what you really mean but i'm doing my best.
>>
>> I just want to ask you Joe about your algorithm :
>>
>>> If you want to have the bullet go straight to the right spot, you need
to:
>>> 1) calculate how long the bullet will take to get to the enemy
>>> 2) calculate where the enemy will be at that time (newposition)
>>> 3) calculate how long it will take the bullet to get to newposition
>>> 4) recalculate newposition based on the new time
>>
>> Ok so i think the first step is pretty clear to me, the step 2 too but i
don't understand the step 3. My english isn't the best i repeat so... i have
to calculate how long the bullet will take to go to 'newposition'. So i will
get a new time value and then i need to recalculate newposition based on
this new time? And fire at 'newposition'? And also, to be sure,
'newposition' is the virtual position of the target after the time taken by
the bullet to hit it, right?
>>
>> Sorry if it seems to be straigh forward to you :-)
>>
>> Of course i will try others way like Lee suggested me.
>>
>> Thanks.
>>
>> 2011/7/20 Joe Ranalli <[email protected]>
>>>
>>> It depends what you're trying to do.
>>>
>>> If you draw the straight line between the tower and the enemy and use
that vector to translate a bullet each tick, the bullets might miss the
enemy.  Think about it this way, the bullet moves 2 steps toward the enemy,
then the enemy moves 1 step, then the bullet moves 2, etc.  Because the
enemy moves the bullet will have to change its direction through the
flight.  So you could calculate the direction vector between the bullet and
the enemy every tick and have the bullet move that direction.  That would
make the bullets kind of arc to the target.
>>>
>>> If you want to have the bullet go straight to the right spot, you need
to:
>>> 1) calculate how long the bullet will take to get to the enemy
>>> 2) calculate where the enemy will be at that time (newposition)
>>> 3) calculate how long it will take the bullet to get to newposition
>>> 4) recalculate newposition based on the new time
>>>
>>> Technically you could iterate that repeatedly until newposition
converges.  Practically, iterating once probably gets you close enough
unless the movement is extremely complicated.
>>>
>>> On Wed, Jul 20, 2011 at 9:14 AM, Nathan BIAGINI <[email protected]>
wrote:
>>>>
>>>> Hi everyone,
>>>>
>>>> i would like to know what are the common way to handle "trajectory" in
a 2d game. In fact, i think of writing a tower defense game and i wonder how
to handle the trajectory of the missile launch by the towers. I though of
getting the pos of the tower and the target and create a vector between this
two entitites to make a sprite translation of this vector but there is maybe
some tricky stuff to do it.
>>>>
>>>> Thanks in advance and happy game making all :-)
>>>
>>
>
>

-- 
Jake

Reply via email to