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

Given the sprites X and Y values, and the mouses X and Y values, how can I make the sprite face the mouse cursor?

I tried doing this in my move method which I found in another SO question:

    private function move(mousePosX:Number, mousePosY:Number) {
            this.vx -= this.speed;
            this.vx *= this.friction;
            this.vx += this.speed;
            this.vx *= this.friction;
            this.vy -= this.speed;
            this.vy *= this.friction;
            this.vy += this.speed;
            this.vy *= this.friction;

        this.x += vx;
        this.y += vy;

        // Make the player face the cursor
        this.rotation = Math.atan2(this.x - mousePosX, this.y - mousePosY);

And rotation does happen, but it's of one or two pixels. this.x and this.y are the sprite X and Y values.


EDIT: Got it. here's the final calculation:

this.rotation = -(Math.atan2(this.x - mousePosX, this.y - mousePosY) * 180 / Math.PI);
share|improve this question
possible duplicate of Orienting a model to face a target – Nicol Bolas Apr 4 '12 at 21:08
up vote 0 down vote accepted

Trying to speed answer your question,

I guess you are halfway there, you probably are getting the rotation in radians from Math.atan2() call, and you're graphics library deals with degrees!

degree_angle = rotation * 180 / 3.1415926 (PI);

Anyway, as a bonus, if you actually need movement in the mouse direction, the direction vector would be such as v(cos(rotation), sin(rotation)). Then multiply that for the desired movement "speed", and there it goes, following the mouse.

Please correct me if i'm wrong :)

share|improve this answer
That formula you posted worked, thanks :) Updated my question with the answer. – Joe Simpson Feb 22 '12 at 2:20
great! good programming !:) – Grimshaw Feb 22 '12 at 2:24

Your Answer


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.