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

Re: [pygame] trajectory



There is a pygame object to create a 2d vector?

2011/7/20 Joe Ranalli <jranalli@xxxxxxxxx>
Yes a vector is probably appropriate.


On Wed, Jul 20, 2011 at 10:20 AM, Nathan BIAGINI <nathan.open@xxxxxxxxx> wrote:
Ok. But all the calcul of how long the bullet will take to reach the target etc... will be made byn using vector? I mean, using a vector still viable?


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 :-)