Finding rotation based off mouse

BlitzMax Forums/BlitzMax Programming/Finding rotation based off mouse

Robert Cummings(Posted 2007) [#1]
Hi,

I have a box of verts. The box has an angle.

I need to rotate the box so the vert on the corner of the box is closest to the mouse position.

This is because like photoshops image rotation gadgets, you click and select a corner of the box, then you move the mouse and that corner strives to get to your mouse position, thereby rotating the box.

Its a good user interface thing for my app I am developing.

Just grab a corner and drag to rotate the box. Hows it done only knowing the position of the vert and the angle of the box?

I just want to return a sane angle...


Floyd(Posted 2007) [#2]
Consider two vectors starting at the center of the box. They point to the vertex and to the mouse.

ATan2( my-cy, mx-cx ) - ATan2( vy-cy, vx-cx )

That should be the angle to turn the box so that the vertex moves toward the mouse. m,c,v refer to mouse,center,vertex.

Not tested, but the idea looks right.


Robert Cummings(Posted 2007) [#3]
Thank you floyd, almost perfect. It tends to be off by 180 or 90 degrees sometimes depending which corner is selected.

I am not yet sure why.

[edit] It seems to vibrate or flip rapidly, I will test.


Robert Cummings(Posted 2007) [#4]
Bah I can't get predictable results. I must be doing something silly.


big10p(Posted 2007) [#5]
Hmm. Floyd's solution sounds right to me.

You are 'snapping' the image to the new angle, aren't you? If you gradually rotate the image to the angle, sometimes the calculation will result in the rotation going in the wrong direction, due to it trying to 'take the long way around'.

Post some code?


Robert Cummings(Posted 2007) [#6]
Local cx:Float = SelectedMob.x
Local cy:Float = SelectedMob.y
Local vx:Float = cx + SelectedVertex.x
Local vy:Float = cy + SelectedVertex.y
			
SelectedMob.Angle = ATan2( my-cy, mx-cx ) - ATan2( vy-cy, vx-cx )
Print SelectedMob.Angle


verts are local so I add them to the center pos of the "box" so their coords are global.

Thats it. There's nothing complex about it... tried printing vx,vy,cx,cy and they are all in the right places. There's something up with Atan2.


Robert Cummings(Posted 2007) [#7]
Here is the output from Print, in the above code to illustrate what is going on.

0.567266405
8.30943100e-006
0.567266405
8.30943100e-006
0.567266405
8.30943100e-006
0.567266405
8.30943100e-006
0.567266405
8.30943100e-006
0.567266405
8.30943100e-006
0.567266405
8.30943100e-006
0.567266405
8.30943100e-006
0.567266405
8.30943100e-006


As you can see it vibrates between 2 positions constantly.


Floyd(Posted 2007) [#8]
Make sure everybody is playing by the same rules. You may be mixing two different coordinate systems.

Typically the "box" would live in a world with the origin at the center of the screen and the y-axis pointing up.

The mouse is probably using the upper left corner of the screen as the origin, with the y-axis pointing down.


Robert Cummings(Posted 2007) [#9]
Gah I give up for the night. Its just one big nightmarish headache that I can't seem to fix.