Search Unity

algebra question

Discussion in 'Scripting' started by eteeski, Feb 23, 2010.

  1. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Hey, i have a question kind of relating back to the good old days of high school algebra. Im looking for an equation to give the ship in my game smoother acceleration.

    The way i need it to work is that X and Y are always between -1 and 1.
    I came up with one equation, but it's not quite as smooth as i want it.
    The equation is: Y= (((x * 2Pi) + Pi + sin((x * 2Pi) + Pi) - Pi) / 2Pi

    And that comes out looking like this:


    but i want it to look more like this:
     
  2. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    I just finished doing something like this. Here is what I am using to smooth my acceleration.

    Code (csharp):
    1.  
    2.     if ( throttle >= trueSpeed)
    3.     {
    4.         trueSpeed = Mathf.Lerp (trueSpeed, throttle, accelerateConst * Time.deltaTime);
    5.     }
    6.     if (throttle < trueSpeed)
    7.     {
    8.         trueSpeed = Mathf.Lerp (trueSpeed, throttle, decelerateConst * Time.deltaTime);
    9.     }
    throttle is the value of the direct user input, in my case it is an axis value between -1 and 1. TrueSpeed is the endall value that the ship is going, this gets pluged into your addForce or whatever. AccelerateConst is the constant that smooths acceleration, change this to alter the "feel" of your acceleration.
     
  3. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    ok, i see. i think your script is good for like an accelerating car? that's really close to what im trying to do. what im really looking for is for my ship to speed up smoothly, but also reach its top speed smoothly too.

    the way my script is going so far is:
    - based on the user input, axis -1..0..1, var ratio is smoothly changed between -1 and 1 using Mathf.SmoothStep

    Code (csharp):
    1.  
    2. var axis = Input.GetAxis("Vertical");
    3. var ratio = Mathf.SmoothStep(ratio, axis, Time.deltaTime * acceleration);
    4.  
    -then make sure ratio isn't more than 1 or less than -1

    Code (csharp):
    1.  
    2. ratio = Mathf.Clamp(ratio, -1, 1);
    3.  
    - then i apply the equation from one of the graphs above, where y = newRatio and x = ratio.

    - then i multiply newRatio by the max speed and set that to the current speed

    - and position += currentSpeed, blah blah blah

    all this would hopefully make it so that once the top speed is reached, it doesn't stop accelerating all in one frame. it's like when you're driving a real car; you wouldn't just take your foot off the gas as soon as you reach the speed limit.

    i bet we can both use this equation in our games if we can find it, mirage.
     
  4. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    To the best of my understanding Lerp does exactialy what you have in your image. It slowly increases to the halfway point, then slows down to the end value, creating an "S"-shaped curve. I am using it in a zero gravity spaceship now and it works well enough. It is not "realistic" by any account, but it does its job well. Remember to mess around with the accel and decel constants though, those are what realy matter.
     
  5. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    you know, maybe spherical linear interpolation is what we're looking for. i've looked that the wikipedia page, but i dont really understand it. does anyone know if spherical linear interpolation, or Slerp, can be translated to a simple x and y graph like the ones above?
     
  6. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    ah, i see, maybe Lerp is what im looking for. i guess the word linear made me assume it was a straight line.

    any idea if the graph that function makes is like the blue graph or the red graph?
     
  7. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    ok, i applied Lerp and it is looking pretty good, but i still want to see if i can find the xy equation that would make the red graph if that is at all possible, and compare them to see which one i like better.
     
  8. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    It is very similar to the blue graph. Try it and see what you think, I use it any time I need to smooth a value. Make sure you have a smooth constant in there though, it adds much more control over what you want it to do. Possibly add a gui element to change the smooth const in game.
     
  9. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    alrighty, i've applied the Lerp and made some adjustments, and it's looking pretty good. And now that i understand Lerp, i can use it in some other parts of my game :D. thank you mirage for your help.