Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do you explain Clockwise and Counter Clockwise?

Discussion in 'Scripting' started by YoungRichi, Mar 12, 2020.

  1. YoungRichi

    YoungRichi

    Joined:
    Apr 19, 2018
    Posts:
    14
    We all understand the different between Clockwise and Counter Clockwise.

    How do we define that condition in a scripting language?

    For example, a user is plotting n coordinates in the world in order to draw a polygon.

    The user can choose plot the coordinates in a CW manner or CCW manner. How can you tell if the person is plotting something CW or CCW?

    Ultimately, what is the PATTERN for something that is going clockwise vs counter-clockwise?

    If we look an actual clock, then we can say that the general pattern of clock wise direction at 12:00 is : Right to left / Top to Bottom.

    But that's not always the case. If you start watching the clock at 6:00. The pattern becomes Left to Right / Bottom to Top.

    Imagine if a person has never seen a clock before. How can we explain clock wise and counter clock wise to that person?

    I hope my questions make sense. Any idea is appreciated!

    Thank you.
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You can use cross products for this. https://docs.unity3d.com/ScriptReference/Vector3.Cross.html
    Take two vectors that are in the plane of the "clock face", the first from the center of the face to the player's previously selected position, the second from the center of the face to the player's currently selected position. If the cross product points up (in the clock face's local space), then they are moving clockwise. If the cross product points down, they are moving counter clockwise. You might need to check that I didn't get those backwards, but that's the basic idea.
     
    neoshaman, lordofduct and YoungRichi like this.
  3. YoungRichi

    YoungRichi

    Joined:
    Apr 19, 2018
    Posts:
    14
    Thank you so much. That definitely pointed me to the right direction!
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    https://demonstrations.wolfram.com/SignedAreaOfAPolygon/

    the universal description of the pattern that eludes you, is by convention called a right hand grip rule, and is used in physics and mathematics, whenever there is a need to define some rotating frame.

    https://upload.wikimedia.org/wikipedia/commons/3/34/Right-hand_grip_rule.svg

    the general thing with the hands is also called handedness, and will flip when a system of coordinates flips, like in Unity, in which the actual system is a left handed one. however, Unity adopts the conventional angular notation for its system of Euler angles, and thus, the angles in fact go in reverse (in relative terms).

    you can see this in my recent Quaternion.Euler recreation (full code/demo here)

    Code (csharp):
    1. public Quaternion myEuler(Vector3 euler, EulerOrder order = EulerOrder.ZXY) {
    2.   euler *= Mathf.Deg2Rad;
    3.  
    4.   var qx = QuaternionEx.FromToRotation(Vector3.forward, VectorEx.Polar(-euler.x).ToVector3_ZY(), new Quaternion(1f, 0f, 0f, 0f));
    5.   var qy = QuaternionEx.FromToRotation(Vector3.right,   VectorEx.Polar(-euler.y).ToVector3_XZ(), new Quaternion(0f, 1f, 0f, 0f));
    6.   var qz = QuaternionEx.FromToRotation(Vector3.right,   VectorEx.Polar(+euler.z).ToVector3_XY(), new Quaternion(0f, 0f, 1f, 0f));
    7.  
    8.   // ... (visit the link)
    9.  
    10.   return q;
    11. }
    consider that the Z axis is always counter-handed

    btw you can see the handedness literally depicted in the docs for Vector3.Cross
     
    Last edited: Mar 12, 2020
    YoungRichi likes this.
  5. YoungRichi

    YoungRichi

    Joined:
    Apr 19, 2018
    Posts:
    14
    There
    Thank you for explaining.

    So i was doing some more research online. And i found Signed Angle. https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html

    It says it will always return a positive number in a clockwise direction.

    In my case, does it make more sense to simply use the SignedAngle over CrossProduct?

    If i am understanding cross product correctly. Given 2 vectors, the cross product will always return a third vector. And the sign of that cross product (3rd vector) will determine the direction (CW or CCW).

    So whats difference here between signed angle and cross product?
     
  6. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    The cross product might be very slightly faster, since I think signed angle is doing a similar thing underneath, but I doubt that there is much difference. I didn't realize Unity had SignedAngle and always did it myself :D
     
    YoungRichi likes this.
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    SignedAngle does this:
    Code (csharp):
    1.  
    2.         public static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis)
    3.         {
    4.             float unsignedAngle = Angle(from, to);
    5.  
    6.             float cross_x = from.y * to.z - from.z * to.y;
    7.             float cross_y = from.z * to.x - from.x * to.z;
    8.             float cross_z = from.x * to.y - from.y * to.x;
    9.             float sign = Mathf.Sign(axis.x * cross_x + axis.y * cross_y + axis.z * cross_z);
    10.             return unsignedAngle * sign;
    11.         }
    12.        
    13.         public static float Angle(Vector3 from, Vector3 to)
    14.         {
    15.             // sqrt(a) * sqrt(b) = sqrt(a * b) -- valid for real numbers
    16.             float denominator = (float)Math.Sqrt(from.sqrMagnitude * to.sqrMagnitude);
    17.             if (denominator < kEpsilonNormalSqrt)
    18.                 return 0F;
    19.  
    20.             float dot = Mathf.Clamp(Dot(from, to) / denominator, -1F, 1F);
    21.             return ((float)Math.Acos(dot)) * Mathf.Rad2Deg;
    22.         }
    23.  
    https://github.com/Unity-Technologi...ob/master/Runtime/Export/Math/Vector3.cs#L318

    So yeah, it performs a cross product (inlined as cross_x,y,z). But it also calculates the angle which requires a sqrt and some trig. This added complexity is going to be ever so slightly slower.

    But if you wanted the angle anyways... well now you also have the angle and the direction.
     
    YoungRichi likes this.
  8. YoungRichi

    YoungRichi

    Joined:
    Apr 19, 2018
    Posts:
    14
    You guys are the best. Thank you for answering!