• 15
• 15
• 11
• 9
• 10

# Rotating stuff with the mouse

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

## Recommended Posts

So now that I know how to move things with the mouse, I want to know how to rotate thing with the mouse. I'm doing this in Direct2D but an example in GDI(+) will do, or even just an explanation on how to do it. Well I used my head and I've decided the best way is probably a rotate transformation. So far I have
angle = (float)atan((Ypos-200.0)/(Xpos-250.0))* 57.29577f;
for getting the angle. Xpos and Ypos are my mouse positions and 200 and 250 are the position where it's rotating btw. It works well as long as the mouse is on the left side of the line, when the mouse is on the right hand side the line rotates the opposite way of the mouse instead. OF course trigonometry is for triangles, and triangle only go up to 180 degrees, but I need 360 degrees. How would you guys do what I'm trying to do? Am I completely off track or do I just need to make some slight modifications?

##### Share on other sites
You can solve it using atan2 instead of atan. atan cannot determine the quandrant in which your point is only by the value of y/x. A single provided value would require two different values as results, and due to this, it returns a value in [-pi/2,pi/2], whether atan2 returns a value in the [-pi,pi] range, covering the 360 degrees you need.

##### Share on other sites
Thank you, it's working perfectly now!

Also, while it works fine, is this the best way to be doing this? Do programs use other ways for rotating stuff with the mouse, or should I just stick to the way I'm doing it?