lerp()
Purpose
Linear interpolation
Description
Returns an interpolation between two inputs (v0, v1) for a parameter (t) in the closed unit interval [0, 1]. This lerp function is commonly used for alpha blending (the parameter "t" is the "alpha value"), and the formula may be extended to blend multiple components of a vector (such as spatial x, y, z axes or r, g, b colour components) in parallel.
Syntax
result = lerp( v0, v1, t )
Arguments
result linear interpolation
v0 first value
v1 second value
t t parameter from 0 to 1
Example
image = loadImage( "Untied Games/Knight", false )
sprite = createSprite()
setSpriteImage( sprite, image )
setSpriteAnimation( sprite, 8, 11, 10 )
setSpriteScale( sprite, 5, 5 )
sprite.y = 100
start_x = 0
end_x = gWidth()
t = 0
duration = 5
reverse = false
loop
clear()
t += deltaTime() // duration
if t >= 1 then
t = 0
//reverse direction by swapping start and end
temp = start_x
start_x = end_x
end_x = temp
//reverse the character graphics direction
sprite.xscale *= -1
endIf
sprite.x = lerp( start_x, end_x, t )
updateSprites()
drawSprites()
update()
repeat
Associated Commands