Direction of the bullet - how to have something else than left, right, top, bottom

Posted by Florian Margaine on Game Development See other posts from Game Development or by Florian Margaine
Published on 2012-08-31T12:05:21Z Indexed on 2012/08/31 15:54 UTC
Read the original article Hit count: 394

Filed under:
|
|

I'm making a simple shooter game using canvas and javascript. The current code can be seen here.

To know which way I want the bullet to be shot, I simply have a direction property that can have 4 values (left, right, bottom, top), and I can then calculate the next position of the bullet easily.

However, I'd like to move the bullet to the mouse position, but I don't really see how to do this. How do I calculate the next position? I'm guessing there is some formula to calculate the line between two positions (the player's and the mouse's), but I don't have much idea yet.

So there is no obstacle, but I don't see how to calculate this, and be able have the next position of the bullet at each frame.

© Game Development or respective owner

Related posts about JavaScript

Related posts about path-finding