- Home /
Circular Rotation Input
Hello,
I have this top down view of a table that I want to be able to rotate around on its Y axis via a finger swipe (mouse swipe), but I want it to be a circular gesture. What I mean by that is currently, if you swipe anywhere on the screen, the table will rotate anticlockwise. I want it so your finger can actually rotate the table itself, around its y axis.
Example:
I swipe my finger from left to right on the lower portion of the screen, the table will rotate anticlockwise, as if its following my finger.
I swipe my finger from left to right on the upper portion of the screen, the table will roate clockwise, as if its following my finger.
I swipe my finger from bottom to top, on the left portion of the screen, the table will rotate clockwise, etc.
So basically, if I move my finger in a circular motion around the table, the table itself will continually rotate with my finger.
How will I introduce a system like this?
Thanks
Answer by HappyMoo · Jan 24, 2014 at 11:26 AM
When you first put the finger down, you calculate the angle between the middle point of the screen and your finger. You can use Mathf.Atan2 for that.
Then while you move your finger, you calculate the angle every frame and the difference between the starting angle and your current is what you add to the starting angle(also needs to be saved on start) of the table.
Now you only have to handle multiple turns right, so you don't jump from 360 to 0 again. You can do this by manipulating the starting angle on every turn in the opposite direction. This can be tricky as the jump occures at the mathematical 0 angle point, which is not your starting angle, but you'll see when you get there.
Okay, so I've got this so far:
var initialFingerLocation : Vector2;
var currentFingerLocation : Vector2;
var currentAngle : float;
function Update () {
var middlePoint = Vector2(Screen.width/2, Screen.height/2);
if(Input.GetButtonDown("Fire1")){
initialFingerLocation = Vector2(Input.mousePosition.x - middlePoint.x, Input.mousePosition.y - middlePoint.y);
}
if(Input.GetButton("Fire1")){
if(initialFingerLocation != Vector2(0,0))
currentFingerLocation = Vector2(Input.mousePosition.x - middlePoint.x, Input.mousePosition.y - middlePoint.y);
}
if(Input.GetButtonUp("Fire1")){
initialFingerLocation = Vector2(0,0);
currentFingerLocation = Vector2(0,0);
}
var vec2 = currentFingerLocation - middlePoint;
currentAngle = $$anonymous$$athf.Atan2(vec2.y, vec2.x);
}
$$anonymous$$y current angle is only giving out readings between: -2.5 and -1.5, which doesn't sound right to me?
Atan2 gives you rad angles... multiply by $$anonymous$$athf.Rad2Deg if you want degrees
Okay - the value I'm getting is better, ranges from -180 to +180. I guess this is the bit you were referring to when the value 'jumps'?
yes, you need to save your last angle in a var and when you jump signs, but not close to 0, but close to 180, you need to count turns.
float turns = 0;
...
if (($$anonymous$$ath.Sign(currentAngle) != $$anonymous$$athf.Sign(lastCurrentAngle)) && $$anonymous$$ath.Abs(currentAngle)>90) {
// this may be the wrong direction. if so, use -360 ins$$anonymous$$d of 360
turns += 360 * $$anonymous$$ath.Sign(currentAngle);
}
...
tablerotation = turns + currentAngle - startAngle
Awesome, got it working now. Thanks! $$anonymous$$uch appreciated.