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.

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 has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
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

1 Answer 1

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

But the very simplest approach is:

//init:
CONSTANT_FACTOR = .001;


//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.