Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm developing 2D strategy game. There are weapons that shoot at enemies. From what I've read in this, this, this and this post I think that I need linear algebra, but I don't really understand what algorithm I should use so the bullet will go to the target? Do I need a pathfinder, why?

Can you please suggest what algorithms and/or books I can use for bullet movement toward the enemy?

share|improve this question
    
Please see this: developer.nokia.com/Community/Wiki/… It's J2ME, but you can easily convert the algorithm for other languages. It uses trigonometry (pythagorean theorem). –  user27658 Mar 21 '13 at 4:05
add comment

1 Answer

I suggest you read Linear algebra for game developers part 1 / part 2 over on Wolfire's blog. They explain linear algebra quite easily - there aren't algorithms, just vector math. I know you linked that tutorial, but if you haven't read part 2 yet, definitely read part 2 because it explains how this works.

An overview of how it's done

The basic process is that you have a vector for the bullet's position, and a vector representing its movement in each frame, and you add this movement vector (also known as displacement vector) to its position on each update to move the bullet.

You determine the movement vector through vector math too: determine the vector from the bullet to the target (vector difference), which gives you the direction for the bullet to go in. To go at the right speed in that direction: normalize this vector (to give it a length/magnitude of 1), then multiply it by the speed the bullet should be going at. The resulting vector will be your movement vector.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.