Search Unity

Feedback: The lack of regular Transform methods in math

Discussion in 'Entity Component System' started by 8bitgoose, Jul 4, 2019.

  1. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    I've used the Transform extensions for years with Unity. And suddenly I've had to understand way more math in order to get proper local to world and world back to local. Or do things like transform a direction from world to a matrix's local direction.

    The most frustrating part is that the code for doing these simple transforms don't seem to be easily findable on the internet. It feels like a step back that I've had to write my own mathf function to simulate what Mathf and the extensions from transform used to do.

    I understand that math is supposed to be super fast for SMID stuff, but maybe include all the other functions that we know and love from UnityEngine.Transform and mark them as slightly slower.

    Or am I crazy and just bad at math?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    I don't think you are crazy. Matrices and quaternions don't have obvious visual intuitive correspondences like vectors, points, and geometry do. But rather than have more functions, I would first like to see a Unity.Mathematics cookbook. This way users can quickly find the formulas they want, but at the same time be able to make aggressive optimizations if they choose to do so.
     
    Orimay, thelebaron and 8bitgoose like this.
  3. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    Speaking of which,

    why do these two functions return different rotations?
    Code (CSharp):
    1.       Quaternion finalRot = Quaternion.AngleAxis(AngularVelocity.magnitude * fixedDeltaTime, AngularVelocity.normalized) * transform.rotation;
    2.  
    3.       Quaternion finalRot = quaternion.AxisAngle(AngularVelocity.normalized, AngularVelocity.magnitude * fixedDeltaTime) * transform.rotation;
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    Degress (mathf) vs radians (math)
     
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    One of the benefits of libraries is so we don't duplicate code/reinvent the wheel over and over. How complex the problem is or whether we know how to implement it isn't the point at all really. That's completely orthogonal.

    Given that Unity's own team is now duplicating this kind of thing out in various repo's and packages, I think it's worth solving for sooner then later.
     
  6. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    I completely agree @snacktime and am wondering why they've left out so many functions from math. I would prefer to not reinvent the wheel.

    @DreamingImLatios I am not sure what you are referring to.
     
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You asked why the functions return different values.

    Quaternion.AngleAxis expects degrees
    quaternion.AxisAngle expects radians
     
  8. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    Oh okay. See I clearly have no idea how angular velocity works because I didn't even know a vector3 could come in radians or degrees...
     
  9. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    It's not the vector that is in degrees.

    Code (CSharp):
    1. // Creates a rotation which rotates angle degrees around axis.
    2. public static Quaternion AngleAxis(float angle, Vector3 axis);
    Code (CSharp):
    1. /// <summary>
    2. /// Returns a quaternion representing a rotation around a unit axis by an angle in radians.
    3. /// The rotation direction is clockwise when looking along the rotation axis towards the origin.
    4. /// </summary>
    5. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    6. public static quaternion AxisAngle(float3 axis, float angle)
    The new math library is well documented.
     
  10. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    @tertle The magnitude of the angular velocity is in radians and not degrees. I had no idea that the magnitude of the angular velocity was the rotation in degrees. I still don't understand a lot of the physics and how they work.

    Speaking of which, how do you get the summaries into VS. None of the summaries show up when I call math. functions in the IDE.
     
  11. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    Radians and degrees are units expressing the same measurement, much like centimeters vs meters. The different units need to be accounted for when calculating the sine or cosine of an angle. Mathf often assumes units are in degrees, as they are easier for beginners. math likes radians for performance reasons.