- Home /
How to determine if rotation is clockwise or anticlockwise
I am trying to create a dial. It has a number in the center that I want to go up if the rotation is clockwise and go down if the rotation is anticlockwise. Here is my rotation code:
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.touches[0].position);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject == TurnDial)
{
switch (touch.phase)
{
case TouchPhase.Began:
var dir = Camera.main.WorldToScreenPoint(TurnPivot.transform.position);
dir = Input.mousePosition - dir;
baseAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(TurnPivot.transform.right.y, TurnPivot.transform.right.x) * Mathf.Rad2Deg;
break;
case TouchPhase.Moved:
var dir = Camera.main.WorldToScreenPoint(TurnPivot.transform.position);
dir = Input.mousePosition - dir;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - baseAngle;
AngleText.text = angle.ToString();
TurnPivot.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward)
//if rotation is clockwise text number goes up
//else it goes down
break;
}
}
}
}
How can I tell if the dial was rotated clockwise or anticlockwise?
Answer by Turlok · Jan 21, 2019 at 07:49 PM
One solution is to compare the starting angle to the ending angle. Decreasing angles result in clockwise rotation, increasing angles result in anticlockwise rotation. IE if (starting > ending) the rotation is clockwise, else the rotation is anticlockwise.
To avoid the pitfall of co-terminal angles, first reduce your angle's representation:
//restrict myAngle to 0-360 degree representation
myAngle %= 360;
if (myAngle < 0) myAngle += 360;
I have an object spinning on its axis how can I deter$$anonymous$$e that the spinning motion is "CW" or "CCW".
Answer by Out_Of_Names · Jan 21, 2019 at 08:30 PM
In OnActive() or Start() store your current rotation in a variable such as _lastRot. Then, after updating the rotation, compare _lastRot to your current rotation to decide if you have rotated clockwise or not. Then, update _lastRot to equal your current rotation so you can compare against it the next time it moves.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Trouble with Camera Rotation Math 2 Answers
Projectile motion arc 2d 1 Answer
Rotation using exponential functions 1 Answer
How to Reverse an equation.. (c#) 2 Answers