Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XNA MathHelper.SmoothStep? How does it work?

Tags:

c#

xna

smoothstep

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.

like image 771
Moulde Avatar asked Feb 26 '09 13:02

Moulde


2 Answers

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.

like image 133
Coincoin Avatar answered Oct 01 '22 22:10

Coincoin


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.

like image 34
shoosh Avatar answered Oct 02 '22 00:10

shoosh