Math Lerp Question

no idea to have negative value for minimum in
MathHelper.Lerp(min,max,float)

All the lerp function does is look at the value parameter and the closer it is to 0, the closer the resulting number is to minimum. The closer the value is to 1, the closer the resulting number is to maximum.

So…for example… if minimum is 0, maximum is 50, and value is 0.5, the resulting number is 25.

The minimum and maximum values can be literally anything you want, including negative numbers. If you’re wondering how to pass a negative number, just subtract a positive number from 0. For example float negative = 0 - 4.15F will give you -4.15F.

2 Likes

thank you very much:slight_smile:

You can specify negative literals, there’s no need to subtract from zero. E.g. float negative = -4.15f;.

It’s interesting to note that Lerp does not clamp the interpolation value to [0, 1], so it’s fine to specify a number outside that range as well (e.g. for elastic tweening that pulls back/overshoots).

Oh my god I completely forgot negative literals existed. Derp.

Anyway, that explains a lot. I use lerps extensively in my game to handle things like fades and slide animations in cutscenes and basically the code looks like this:

//Class member for storing the slide progress.
private float _fadeProgress = 0;

//In Update():

_fadeProgress += (float)gameTime.ElapsedTime.TotalSeconds;
if(_fadeProgress >= 1)
{
  //do something to stop the fade progress from increasing even more.
}
//In Draw:

int startX = -100;
int endX = (ScreenWidth - 100) / 2; //centre of the screen, horizontally.

int x = (int)MathHelper.Lerp(startX, endX, _fadeProgress);
//render something at that position

It may be very miniscule (a few pixels at most if running at a decent framerate) but if you were to simulate low framerates, sub-1FPS, I could see the rendered object getting rendered at an unintended location.

Good thing MathHelper.Clamp() exists :stuck_out_tongue:

This might help in explaining - very good site:

https://www.redblobgames.com/grids/line-drawing.html

I wrote a blog post on Lerp here with an example Lerp class.

I am no maths expert - in fact I’m pretty hopeless at it. Writing these posts helps me get things clear in my head and also gives me something I can go back to when I’ve forgotten how I did something!

Here’s some lerp madness.

I wrote this to find a new custom vertice between two existing vertices i.e. to find typically a lerped point from two points that generates new points on a spherical curve.

/// <summary>
/// This method lerps between two custom vertices by the specified percentage. 
/// The position is found by the normal * the distance from the origin specified.
/// </summary>
public VertexPositionNormalColorUv LerpSubDivisionVertices
    (
    VertexPositionNormalColorUv from,
    VertexPositionNormalColorUv to,
    float lerpPercentage,
    float distance,
    Vector3 origin
    )
{
    VertexPositionNormalColorUv B = new VertexPositionNormalColorUv();
    B.Color = new Color(
        (byte)(from.Color.R + (to.Color.R - from.Color.R) * lerpPercentage),
        (byte)(from.Color.G + (to.Color.G - from.Color.G) * lerpPercentage),
        (byte)(from.Color.B + (to.Color.B - from.Color.B) * lerpPercentage),
        (byte)(from.Color.A + (to.Color.A - from.Color.A) * lerpPercentage)
        );
    B.TextureCoordinateA = from.TextureCoordinateA + ((to.TextureCoordinateA - from.TextureCoordinateA) * lerpPercentage);
    Vector3 d = (from.Normal + ((to.Normal - from.Normal) * lerpPercentage));
    B.Normal = Vector3.Normalize(d);
    B.Position = origin + B.Normal * distance;
    return B;
}