Some maths help please?

Monkey Forums/Monkey Programming/Some maths help please?

CopperCircle(Posted 2012) [#1]
Hi, I need to create a function to allow an object to be rotated with two fingers. I guess I would need to workout the angle between the fingers and convert to degrees but my math is not great, any help would be appreciated.

Thanks.


Gerry Quinn(Posted 2012) [#2]
We can't answer until we understand the interface more exactly. Can you explain exactly where the player is supposed to touch/drag to rotate an object?

If you want the angle A-O-B where A and B are the fingers and O is the centre of the object, the way to do it is to calculate the angles O-A and O-B which are:

local angleA:float = ATan2( yA - yO, xA - xO )
...and similarly for angleB

Now you want to rotate by the difference between angleA and angleB, but to decide how to do that we need to know which is the startpoint and which is the endpoint.

Or maybe you have some other control system in mind.


Fred(Posted 2012) [#3]
There is a MultiTouch module with sources (if I remember well) for PC and BlitzMax that demonstrates this kind of picture/object handling:

Multitouch module
Topic: http://www.blitzforum.de/forum/viewtopic.php?p=365769#365769
Download: http://www.flowersoft.smokecover.com/flowersoft/developement/lobby%20modules.rar


CopperCircle(Posted 2012) [#4]
Thanks guys, Gerry I want the player to be able to do a two finger pinch anywhere on the screen and then rotate my object from its current angle in the same direction as there fingers.

ATan2 does the trick for the angles but I'm still having trouble with a complete 360 rotation due to the negative values returned...


Jesse(Posted 2012) [#5]
you can convert that to positive like this:
(ATan2(y,x) + 360.0) Mod 360.0



Gerry Quinn(Posted 2012) [#6]
Here's what I did recently when objects should slowly turn to face the correct direction before moving:

Local turn:Float = ( endAngle + 360.0 - startAngle ) Mod 360.0
If turn > 180.0
	turn = 360.0 - turn
	antiClock = False
Else
	antiClock = True
End


antiClock is a bool which says whether it should turn clockwise or anticlockwise, and turn is the total number of degrees it should turn.


CopperCircle(Posted 2012) [#7]
Thanks, Mod 360.0 was what I needed.