Search code examples
c#xnasmoothstep

XNA MathHelper.SmoothStep? How does it work?


I have a car and when accelerating i want the speed to increse "slowly"..

After looking at a few sites i came to the conclusion that the SmoothStep method could be used to do that?

I pretty much know how to move textures and stuff, so an example where smoothstep is used to increase value in a float or something like that, would be extremely helpful!

Thanks in advance :)

I think it is sad there isnt examples for all the methods in the MSDN library.


Solution

  • SmoothStep won't help you here. SmoothStep is a two value interpolation function. It does something similar to a sinus interpolation. It will accelerate slowly have a sharp speed at around x=0.5 and then slow down to the arrival (x=1.0).

    Like the following:

    smoothstep_approx

    This is approximate, the real function don't have these exact numbers.

    Yes you could use the x=0..0.5 to achieve the effect you want, but with very little control over the acceleration curve.

    If you want to really accelerate a car or any other object, your best bet would be to keep track of acceleration and velocity by yourself.

    class Car : GameComponent
    {
        public override void Update(GameTime time)
        {
             velocity += acceleration * time.ElapsedGameTime.TotalSeconds;
             position += velocity * time.ElapsedGameTime.TotalSeconds;
        }
    
        Vector3 position;
        Vector3 velocity;
        Vector3 acceleration;
    }
    

    position, velocity and acceleration being Vector2 or Vector3 depending on how many dimension your game state is using. Also, please note this form of integration is prone to slight math errors.