# Using the mouse to aim...

## Recommended Posts

Hello all, My current game is a top down 2D shooter in which the player is a tank and they have to complete several missions, etc. Anyways, I want to use the mouse to aim the turret on the tank. I am already using the keyboard to rotate and move the tank body, and it is much more intuitive to use the mouse to aim the turret than any other input setup I have tried. so here is my problem, the player's tank turret can point at any one of 16 predefined angles. I want it to be so that wherever the mouse pointer is, is where the turret will aim. I have tried to implement this as a big nested if statement that tried to determine the correct angle based on the mouses offset from the player, but it didn't work out too well. I was wondering if there was another technique that I could use to do this. My game window resolution is 640x480, and the player's position never changes in terms of screen coordinates. it's center is always at 320x240 unless the tank approaches a boundary, in which it can move to the edge since the window will not scroll any further. I can also retrieve absolute and relative mouse coordinates, which ever will help the most. I think I covered everything, please ask if I was unclear on anything. Thanks in advance, Svenjamin

##### Share on other sites
Moved to For Beginners.

##### Share on other sites
Trigonometry will be your best option here.

First, get the screen position of your tank (which is mostly 320x240 unless it goes near a wall). Then calculate the distance of the mouse from the tank, using this formula:

sqrt((posOfTankX - posOfMouseX)^2 + (posOfTankY - posOfMouseY)^2)

Make a triangle from those two coordinates, and calculate the the angle of one of the hypotenuse angles and use that angle to know how much to turn your tank. Am I clear on that part? I might have to draw a picture to make this part understandable.

EDIT: Wait, you don't actually need the distance formula here. Ignore that part.

##### Share on other sites
Get the distance from the tank's position to the mouse - this will be an (x,y) pair where each value is possibly negative. The x comes from (x of mouse - x of tank); similarly for y. Feed those two values to (assuming C or C++) the library function atan2(); that will give you the angle, in radians, of the line from tank to the mouse. A value of 0 means to the right; increasing values move the angle counter-clockwise, with a maximum of 2 * pi (so a return of pi means the angle is to the left; pi/2 means up; 3pi/2 means down). The last step is to translate that into a direction, from 0 (right) to 15 (right by down-right). We'll do that by:

- scaling the value from [0..2*pi) up to a [0..16) value (multiply by 8/pi);
- rounding the value to the nearest integer (add 0.5 and then convert to int - which discards the remainder);
- if the result is 16, change it to 0 (we had an angle that was just slightly below straight-right).

##### Share on other sites
Thanks for the replies,
The trig makes a lot of sense, it seems so obvious when someone else tells you about it. [grin]
I will try this stuff out as soon as I get home.
Thanks again!
Svenjamin

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982121

• 22
• 9
• 9
• 13
• 11