Search Unity

Simple Sphere Mesh Rolling Calculations Are Off

Discussion in 'Entity Component System' started by BigRookGames, Feb 21, 2022.

  1. BigRookGames

    BigRookGames

    Joined:
    Nov 24, 2014
    Posts:
    330
    I am trying to get a sphere mesh to roll visually on the ground, and feel that I am missing something conceptually due to incorrect results. The same calculations work in non DOTS, which makes me think it is a transform calculation I am using incorrectly or a world/local discrepancy.

    Here is the code:

    Code (CSharp):
    1. Dependency = Entities.WithName("AddBallRotation")
    2.                 .ForEach((Entity e, int entityInQueryIndex
    3.                     , ref ROTATE_BASED_ON_POSITION_CHANGE ballRotate
    4.                     , ref Rotation rotation
    5.                     , in LocalToWorld l2w
    6.                 ) =>
    7.                 {
    8.                     var movement = l2w.Position - ballRotate.LastPosition;
    9.                     float distance = math.length(movement);
    10.                     if (distance > 0.001f)
    11.                     {
    12.                         float angle = distance * (180f / math.PI) / ballRotate.Radius;
    13.                         //this should be the normal of the ground instead of the float3.Y but just testing on flat ground
    14.                         float3 rotationAxis = math.normalize(math.cross(new float3(0,1,0), movement));
    15.  
    16.                         var eulerAngle= quaternion.EulerXYZ(rotationAxis * angle);
    17.                      
    18.                         rotation.Value = math.mul(eulerAngle, rotation.Value);
    19.                        
    20.                     }
    21.                     ballRotate.LastPosition = l2w.Position;
    22.  
    23.                 }).Schedule(Dependency);
    It seems to spin in a somewhat correct direction, but definitely not matching up correctly, something is out of sorts.
     
  2. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    Old Quaternion and new mathematics quaternion are different , the default rotation order is different.
     
    Baggers_ and BigRookGames like this.
  3. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    New math library is radian based, call math.radians(angle) before working with the quaternion

    Also default quaternion order isn't XYZ, I can't recall which one it was but you can check the default parameter from quaternion.Euler, if you're trying to match Mathf.Quaternion.Euler you need to use the default one
     
    BigRookGames likes this.
  4. BigRookGames

    BigRookGames

    Joined:
    Nov 24, 2014
    Posts:
    330
    Ah, that would explain it. Thank you.

    Okay cool, I'll try doing that and switching up the default orientation. Thanks for the input.