Rotating stuff with the mouse

Started by
2 comments, last by fcoelho 14 years, 3 months ago
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?
Advertisement
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.
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?
It is a perfectly fine solution. It all boils down to some sort of measure based on the current mouse position and your solution is as simple as it can get.

This topic is closed to new replies.

Advertisement