Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Shoot a bullet towards cursor - top down 2d

I'm creating a 2D top down shooter, and I want to get the bullets to go in the direction of the mouse. Not sure of the logic that goes into it.

share|improve this question

marked as duplicate by Byte56, doppelgreener, Josh Petrie, Noctrine Oct 30 '12 at 14:42

This question was marked as an exact duplicate of an existing question.

This is a very common question. I can't imagine you searched the site first. Please do so in the future before asking a question. – Byte56 Oct 20 '12 at 15:14

You could use trigonometry like here: 2D Magnet-like repelling behavior

But the very simplest approach is:


//every frame:
dx = mouse.x - bullet.x;
dy = mouse.y - bullet.y;

bullet.vx = dx * CONSTANT_FACTOR;
bullet.vy = dy * CONSTANT_FACTOR;

bullet.x += bullet.vx;
bullet.y += bullet.vy;
share|improve this answer
@Byte56 Thanks for a tip, will use it. – Markus von Broady Oct 20 '12 at 17:10

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