Page 1 of 1

Reputation:

# Dark GDK sprite rotation question

Posted 25 February 2010 - 03:51 AM

Right, I have to make a game for a programming project. I decided on something similar to smash tv where the player can move and fire in any direction simultaniously. The problem I'm having (at the moment) is how to get the player's avatar to track the mouse position.

What I want, is for the player's avatar to constantly rotate around its center to face the mouse cursor's position, and fire in that direction as well while being able to move freely about the screen. I have the avatar appearing and moving, but I can't get it to rotate and face the mouse.

Obviously I have to keep track of the mouse's position, and the player's position. That isn't hard. But I don't know how to find the propper angle to rotate the sprite to get it to face the mouse cursor.

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: Dark GDK sprite rotation question

### #2 lesPaul456

Reputation: 175
• Posts: 729
• Joined: 16-April 09

## Re: Dark GDK sprite rotation question

Posted 25 February 2010 - 08:28 AM

I haven't tested this out, so it may not work right, but perhaps something like this:

```float angle = dbAtanFull(x2 - x1, y2 - y1);
dbRotateSprite(spriteID, angle);

```

Where x2 and y2 are the position of the mouse and x1 and y1 are the position of the sprite.

Again I don't know if this will work. I'm pretty sure dbAtanFull returns the angle as degrees. If you find that it doesn't, and that its returning radians (which I'm pretty sure it doesn't) just multiply the value by 180 / PI.

Reputation:

## Re: Dark GDK sprite rotation question

Posted 25 February 2010 - 03:11 PM

Thank you, That function was the right one, but it required a little tweaking before it worked properly (Avatar was rotating clockwise as the mouse was moving counter clockwise when I first tried it).

This is how I got it to work...
```		angle = dbAtanFull(Pone.Ypos - dbMouseY(), Pone.Xpos - dbMouseX()) + 90;
dbRotateSprite(1, angle);
```

First, I'm subtracting the mouse's Y and X positions from the Player's and adding 90 to it because its default facing was down (if I didn't he would be facing 90 degrees from the mouse). For some reason doing it x then y results in the opposite rotation.