The_Osprey
The_Osprey

Reputation: 3

How does MathHelper's Smoothstep() fuction work, and how could I use it to animate things?

I am trying to animate something for a mod I'm making in c#(Disclaimer: I don't think that knowing what game it is will be important). I would like to know how MathHelper's Smoothstep() function works (I believe that it is from Microsoft.Xna), and how I would animate a sword swinging downwards and then upwards, for example.

I haven't really tried anything because I am SUPER confused and overwhelmed by an example program that has something different in the code than what I want to know.

Upvotes: 0

Views: 67

Answers (1)

Jeremy Glazebrook
Jeremy Glazebrook

Reputation: 96

So depending on what you need you may find what you're looking for by using Vector2.Lerp(Vector2 From, Vector2 To, Single Amount)

This is type of thing is referred to as Linear Interpolation.

It will find a point between the two Vector2 values that is proportional to the Amount parameter as supplied as a value between 0.0 and 1.0.

You can use this to animate the sword in a way that you're describing by changing the value of the Amount parameter by a fixed amount over multiple frames.

TimeSpan swordJabTime = TimeSpan.Zero;
public void Update(GameTime gameTime)
{
    //Set Positions
    Vector2 swordFromPosition = new(0,0);
    Vector2 swordToPosition = new(5,0);
    //Set Timing for current frame
    const float swordJabTotalTime = 1.0f;
    swordJabTime += gameTime.ElapsedGameTime;
    float swordJabPercentage = swordJabTime.TotalSeconds() / swordJabTotalTime;
    //Repeat if necessary
    if (swordJabPercentage > 1) {
        swordJabTime = TimeSpan.Zero; 
        swordJabPercentage = 0.0f;
    }
    Vector2 swordPosition = Vector2.Lerp(swordFromPosition, swordToPosition, swordJabPercentage);
}

This example makes use of using the amount of time passed between frames so that FrameRate doesn't affect the speed of the animation. The above example will jab the sword horizontally between the From and To positions over the course of 1 second before repeating.

Read up more on it here

Upvotes: 0

Related Questions