Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Unity.Mathematics available on github

Discussion in 'Entity Component System and C# Job system' started by xoofx, Apr 10, 2018.

  1. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    74
    Is there an equivalent to Mathf.SmoothDamp in the mathematics library that can be used in jobs?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    3,185
    I haven't seen so I am a bit skeptical. But maybe is somewhere else?
     
  3. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    74
    The closest I have found is math.smoothstep. However, this one behaves differently from Mathf.SmoothStep.

    https://docs.unity3d.com/ScriptReference/Mathf.SmoothStep.html

    ^ To replicate the same behavior given in that example with math.smoothstep I had to do the following:

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Mathematics;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.     // Minimum and maximum values for the transition.
    7.     float minimum = 10.0f;
    8.     float maximum = 20.0f;
    9.  
    10.     // Time taken for the transition.
    11.     float duration = 5.0f;
    12.  
    13.     float startTime;
    14.  
    15.     void Start()
    16.     {
    17.         // Make a note of the time the script started.
    18.         startTime = Time.time;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         // Calculate the fraction of the total duration that has passed.
    24.  
    25.         var elapsedTime = (Time.time - startTime);
    26.         // smoothstep returns a value between 0 and 1
    27.         var elapsedTimeRange = math.smoothstep(0, duration, elapsedTime);
    28.         // use elapsedTimeRange to get value between start and end
    29.         // to get our final result
    30.         var current = math.lerp(start, end, elapsedTimeRange);
    31.     }
    32. }
     
    Last edited: Jan 5, 2019
    Antypodish likes this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    3,185
    Seams like a neat solution.
    Just wonder, what if time wraps?
    I mean game would have to be running for quite long time.
    But that would introduce some unexpected results.
     
  5. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    74
    We could use Time.deltaTime instead (the logic will be different from the code above). And that would be better since we won't have to keep track of "startTime", we only need to keep track of the "current" value which you'll most likely be keeping track anyways. It would also make it easier to use with ECS. I'll try out the Time.deltaTime approach with ECS (jobified) and post my results here.
     
    Antypodish likes this.
  6. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    74
    So I haven't been able to derive the elapsed time from the "current" value. I believe it's still possible, just takes a lot of math since you have the reverse engineer the math.smoothstep to get the value, so not sure how much value there is in that. But from the example above, you can still use Time.deltaTime instead of Time.time, and that would look something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Mathematics;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.     // Minimum and maximum values for the transition.
    7.     float minimum = 10.0f;
    8.     float maximum = 20.0f;
    9.     // Time taken for the transition.
    10.     float duration = 5.0f;
    11.     float elapsedTime;
    12.  
    13.     void Update()
    14.     {
    15.         // Calculate the fraction of the total duration that has passed.
    16.         elapsedTime += Time.deltaTime;
    17.         // smoothstep returns a value between 0 and 1
    18.         var elapsedTimeRange = math.smoothstep(0, duration, elapsedTime);
    19.         // use elapsedTimeRange to get value between start and end
    20.         // to get our final result
    21.         var current = math.lerp(start, end, elapsedTimeRange);
    22.     }
    23. }
    For my use case though (which is to smoothly damp the movement speed of the player) I decided not to go with math.smoothStep. Instead I just went with using math.lerp in a way that works similarly to Mathf.SmoothDamp. Like for example:

    Code (CSharp):
    1. currentSpeed = math.lerp(currentSpeed, desiredSpeed, 0.1);
    It's a little more performant and allows me to more smoothly damp the speed when the desiredSpeed changes then I would be able to do with smoothstep.
     
  7. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    489
    Some curves would be very useful to have in this library. At least 2 point curves.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,530
    The universe is apparently about 10^10 years old. Time.time's max value in seconds is ~10^31 years.
     
    Antypodish likes this.
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    3,185
    Do you try to say, we got plenty of time to run, to develop our game :)
    Time.time is float. So technically, if we don't want loose precision, that is a near 70 years.
    Still, looks like no need to worry, at least until next century/millennia bomb.
     
    Qbit86 likes this.
  10. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,363
    530000f seconds + .016f seconds = 530000f seconds
    So, after the first six days the time just stops. And we'll get zero intervals, divisions by zero, infinities and NaNs.
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    3,185
    I think I am talking rubbish anyway :p

    There is discussion
    https://gamedev.stackexchange.com/q...ppens-when-time-time-gets-very-large-in-unity
    But I am not convinced 100% for the answer.
    However, it referencing.
    https://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/

    And this table.

    Float Value Time Value Float Precision Time Precision
    1 1 second 1.19E-07 119 nanoseconds
    10 10 seconds 9.54E-07 .954 microsecond
    100 ~1.5 minutes 7.63E-06 7.63 microseconds
    1,000 ~16 minutes 6.10E-05 61.0 microseconds
    10,000 ~3 hours 0.000977 .976 milliseconds
    100,000 ~1 day 0.00781 7.81 milliseconds
    1,000,000 ~11 days 0.0625 62.5 milliseconds
    10,000,000 ~4 months 1 1 second
    100,000,000 ~3 years 8 8 seconds
    1,000,000,000 ~32 years 64 64 seconds

    Considering float got 9 digits precision, that is near 10 billions, seconds, without milliseconds.
    Hence over 300 years. But surely there is somewhere bottom line, far before reaching 9 digits.

    Someone here to confirm actual Time.time max?
     
  12. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,363
    According to the table, 10,000,001 can't be more precise than that. It's 8 digits. It's 10 million seconds, 115 days.
     
    Antypodish likes this.
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    3,185
    Oh boi, yeah you completely right.
     
  14. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    107
    I often need something like

    Code (CSharp):
    1. var distDiff = position.Value - positions[j].Value;
    2. var distSquared = math.dot(distDiff, distDiff);
    It would be nice to have math.dot with a single argument signature so i can use:

    Code (CSharp):
    1. var distSquared = math.dot(position.Value - positions[j].Value);
     
  15. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    308
    math.lengthsq
    exists
     
    daschatten and Antypodish like this.
  16. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    107
    Thanks :)