Sign in to follow this  
Waterfox

Rotating stuff with the mouse

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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this