#### Archived

This topic is now archived and is closed to further replies.

# Mouse and rotating square.

This topic is 5814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my action game the square which is the player rotates according to the mouse. Unfortunately, I have exceeded my knowledge in this and am out of answers, of any kind. As I move the mouse, I want the square to turn according to the mouse position on the screen. All the usable constants available are mouse.getX mouse.getY rotatesquare(theta t) (radians) Does anyone know what equation I should use here? Do I need other constants? I tried dividing pi, by the place of the mouse but it did not work. I''m sorry if this is confusing, but I have been working with this the whole night and I''m kind of out of focus because of the lack of the sleep.

##### Share on other sites
You mean, the square should face towards the mouse cursor?
First, get the relative position of the mouse to the center of the square: X = MouseX - SquareX; Y = MouseY - SquareY;

Now we can assume our X and Y coordinates are respectively proportional to the sine and cosine of the theta angle. Thus Y/X = sin / cos = tan theta

theta = arctan( y / X );

You''ll need an arc function somewhere, since you''re trying to convert a length to an angle. This is just one way to do it.

Extatica is coming soon!
Check it out on:
http://www.extatica.com02.com

##### Share on other sites
I don''t quite understand the arc function. Could someone please explain how it works or direct me to reliable source?

So basically I''m going to use trigonometry to determine the angle of the triangle, which forms between the center of the square and mouse. I assume the line between the pointer and center is the hypotenuse.

##### Share on other sites
Later on tonight I solved the problem using basic trigonometry.

The equation I used is:

(PI/2) - atan((pcx-mousex)/(pcy-mousey))

and thats it. PI/2 is there to get the angle so that it wont be reversed.

##### Share on other sites
And as the formula says, PI/2 - ATAN x = ATAN 1/x

So you''re better off using ATAN y/x instead of PI/2 - ATAN x/y

Still, there''s the zero divide problen that may loom...

Extatica is coming soon!
Check it out on:
http://www.extatica.com02.com
Nexeruza Studios:
http://nexeruza.ionichost.com/home.html

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632566
• Total Posts
3007107

×