XNA MathHelper.SmoothStep? How does it work?

7.3k Views Asked by At

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.

3

There are 3 best solutions below

0
Coincoin On BEST ANSWER

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.

0
shoosh On

From this documentation it looks like SmoothStep takes 3 arguments - the two values you want to move between and the amount between them which probably needs to be between 0 and 1.
So say you have a float f that increments linearly from 0 to the destination speed over a period of time. instead of using f directly as the speed, using SmoothStep would look like this:

float speed = MathHelper.SmoothStep(0, destSpeed, f/destSpeed);

It really is amazing how bad this documentation is.

3
Moulde On

Very useful site i found in the meantime. http://johnnygizmo.blogspot.com/2008/10/xna-series-basic-ai.html