[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] trajectory



I'm not totally clear on what you're asking.  If you're saying that the targets might change direction after the bullet is fired, but you still want the bullet to hit the target, then my second algorithm wouldn't work.  That just doesn't make any sense.  The algorithm I suggested aims at the target, then shoots at it.  If the target changes direction, then it will no longer go to the spot you were aiming.

If you're having this much trouble figuring this out I would definitely suggest you take some time to sit down and think about what it is you're trying to do and maybe read about some simple physics.  This part of the application really shouldn't be that difficult to figure out.

On Fri, Jul 22, 2011 at 9:08 AM, Nathan BIAGINI <nathan.open@xxxxxxxxx> wrote:
Hi. Thanks all for you replies i have one more question before trying to write something on top of that, your algorithme Joe, does it works if the way has some bifurcations, i mean, a fully linear way but that contain bifurcations.

Thanks.

2011/7/20 Joe Ranalli <jranalli@xxxxxxxxx>
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 <nathan.open@xxxxxxxxx> 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 :-)