1

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?

theateist
  • 107
  • 4
  • Please see this: http://www.developer.nokia.com/Community/Wiki/How_to_move_an_object_into_a_target_in_Java_ME It's J2ME, but you can easily convert the algorithm for other languages. It uses trigonometry (pythagorean theorem). –  Mar 21 '13 at 04:05

1 Answers1

8

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.

doppelgreener
  • 7,184
  • 7
  • 42
  • 68