Search Unity

How does unity.physics detect axis specific angular position without using Euler angles?

Discussion in 'Physics for ECS' started by HeliosJack, Aug 4, 2021.

  1. HeliosJack

    HeliosJack

    Joined:
    Aug 15, 2013
    Posts:
    41
    What I wouldn't give for a guided tour of the unity.physics code base!

    Presumably, somewhere in there, there is code that implements twist joints - that is, constraints where once a joined body has twisted outside a certain range along a certain axis, angular force is applied counter to that position to return it to within the acceptable range.

    But the thing is, in order to do that, one would have to know the rotation of a body along a particular axis. That is what has been driving me crazy, because I don't understand how that is done with rotations represented only in quaternion values.

    Is anyone either a) familiar enough with unity.physics to point me to the exact spot or spots in the code where this is accomplished or b) familiar enough with quaternions to explain to me how angular joints are capable of finding angular position along a single axis?
     
  2. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    Hi,

    Twist limits are implemented in Unity.Physics/Dynamics/Jacobians/AngularLimit1DJacobian.cs. The twist about the joint is calculated in CalculateError(). First it gets the relative orientation of the pivots, orientationB^-1 * orientationA. Then it gets the twist angle from the swing-twist decomposition of the relative orientation. Here is a nice article about swing-twist decomposition: http://allenchou.net/2018/05/game-math-swing-twist-interpolation-sterp/. Basically it breaks the relative orientation into a product of two quaternions, one of them about the twist axis, and the angle of the twist quaternion tells us the relative orientation of the joint about the twist axis. CalculateTwistAngle() in Unity.Physics/Base/Math/Math.cs calculates that angle. Hope that's helpful.
     
    Occuros and HeliosJack like this.
  3. HeliosJack

    HeliosJack

    Joined:
    Aug 15, 2013
    Posts:
    41
    Gonna take a look now - at first glance this looks like exactly the kind of clue I was hoping to get in order to dig into this process intelligently. Thank you so much for taking the time to offer this.
     
  4. HeliosJack

    HeliosJack

    Joined:
    Aug 15, 2013
    Posts:
    41
    @MaxAbernethy wow, what a great response to my post. Not only did you point out a precise position in the code for me to see how unity.physics implements it, you gave me an article which implements it independently and explains the derivation. Not only that, the article you linked actually addresses the singularity problem which is something explained in the comments of the unity.physics implementation but is left unaddressed. Truly 5 star lol