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. Dismiss Notice

Help needed with solving non linear equation please...

Discussion in 'Scripting' started by Genkidevelopment, Feb 21, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Here are the values plotted roughly...

    Code (CSharp):
    1. Velocity  Modifier
    2.  
    3. 2    =      0.81
    4. 3     =      0.68
    5. 4     =      0.58
    6. 5     =      0.50
    7. 6      =    0.43
    8. 7     =      0.36
    9. 8     =      0.31
    10.  



    I have gotten as far as Modifier = 2 - (cubeRoot of Velocity)

    But I am lost now!!

    Any ideas anyone?
     
  2. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    http://en.wikipedia.org/wiki/Lagrange_polynomial#Examples

    ....

    http://www.solvemymath.com/online_math_calculator/interpolation.php
     
    Genkidevelopment likes this.
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    1.67f - 0.68f * Mathf.Pow(x, 1f/3f)
    Looks pretty close
     
    Genkidevelopment likes this.
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    for comparison, what the solvemymath calculator comes up with (wont work for values outside the example plot range)
     
    Genkidevelopment likes this.
  5. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks you so much for the help... I ended a 48 hour session yesterday, tediously testing the game and creating data for the smaller extremes and all '.5' inputs... Conceded to simply creating an 'if machine' that chose the closest value... Sloppy, but it got the idea into the game!!! Now I see a few bugs that I slept on, going to work on those today AFTER I visit your links and re-evaluate this area..

    Peace :D
     
  6. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    I've never learned the proper way to do interpolation over multiple plot points... Given that Excel, OO Calc, and Maxima will do these for me, I've never needed to learn to interpolate functions for graphs by hand...
     
    Genkidevelopment likes this.
  7. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186

    And.... That equation is going to make a processor burn itself into a fission reactor I'd imagine :D
     
    Mycroft likes this.
  8. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Not significntly, you can cache the x and increment it's power every step
     
  9. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
  10. tmcsweeneyML

    tmcsweeneyML

    Joined:
    Aug 13, 2013
    Posts:
    7
    My go to solution for remapping values like this is to use Unity's built-in AnimationCurve.

    Add a curve to your monobehavior like this:
    Code (CSharp):
    1. public AnimationCurve VelocityToModifier;
    Then use the editor to set up your curve with your input values (velocity) across the horizontal axis, and your output values (modifier) up the vertical. You can create a curve control point for each of your data values to construct a curve that fits.



    Then at runtime you can sample the curve at any point using the Evaluate() method like such:

    Code (CSharp):
    1. float modifier = VelocityToModifier.Evaluate( velocity );
     
  11. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
  12. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks for all the effort in helping all. I have been busy in the real world since the weekend... so haven't yet had a chance to implement any of this... As a matter of interest, which of the methods above would be least expensive on the cpu? The reason I ask is I am creating a sport simulation and thus have a lots of things being calculated, I have no performance issues yet, but I'm only 15% in ;)
     
  13. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Don't concern yourself with this until the game is done and working, then (and only then) profile performance to check for bottlenecks.
    Such a small difference is unlikely to be the ultimate cause of bad performance.
    If you are truly worried about it at this early stage, it's a simple matter to set up a small test program to make a million runs of a function and time the result

    ps. I took this opportunity to update to my (free) grapher extension, and used your values in the screenshot
     
  14. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Having some serious issues today with calculating my initial projectile velocity. I am measuring an objects position for reference and then measuring it again 0.1 seconds later... It was stable all weekend, but today each test yields a different result, like the 0.1seconds is becoming 0.1s +- 15%!!!! What time gap would have you used in the past anyone?

    Thanks
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Do you really have no way to get the instantaneous velocity of the target? No access to rigidbody.velocity, or velocity from whatever script is making the target move?
     
  16. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Hmmm! Possibly... rigidbody.velocity sounds interesting... I am using 'add force (Impulse)' to manipulate the projectile, so yes... I don't know the in's and out's of how this would normally be approached!
     
  17. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes, try looking at the rigidbody.velocity of the target rather than deriving it from a couple of position measurements.

    And for launching your projectile, consider setting the rigidbody.velocity directly, rather than adding a force. I know the docs warn against setting the velocity directly, but it's OK if you're only doing it once. This is unless you want something like a rocket, that actually accelerates under its own power; in that case you do need to call AddForce for an extended period of time.
     
    Genkidevelopment likes this.
  18. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks, yes I understand the difference between scripting a constant force/acceleration and that of scripting an 'impulse'... It is entirely possible for me to manipulate the velocity in the way you describe... Thanks again for the heads up...