XNA MathHelper.SmoothStep? How does it work?
Asked Answered
P

3

5

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.

Primatology answered 26/2, 2009 at 13:9 Comment(0)
A
13

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.

Amathiste answered 26/2, 2009 at 16:6 Comment(0)
S
6

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.

Simonne answered 26/2, 2009 at 13:19 Comment(0)
P
0

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

Primatology answered 26/2, 2009 at 17:11 Comment(3)
When you need to interpolate discreet numbers into real numbers and you need the key points slope to be zero. lets say you wanted your car to start slowly at a precise moment and then slowly stop to a very precise moment, then SmootStep would help you for that.Amathiste
It could also be very usefull for all sort of animation transitions, such as menus.Amathiste
if you have examples, please post :)Primatology

© 2022 - 2024 — McMap. All rights reserved.