Math.lerp

From GiderosMobile
Revision as of 08:22, 31 January 2026 by MoKaLux (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Available since: Gideros 2026.1
Class: math

Description

Performs linear interpolation between two numbers using a factor.

(number) = math.lerp(a,b,t)

Generally returning the result of a + (b - a) * t.

When t is exactly 1, the value of b will be returned instead to ensure that when t is on the interval [0, 1], the result of lerp will be on the interval [a, b].

Parameters

a: (number) number value a
b: (number) number value b
t: (number) interpolation factor

Return values

Returns (number) linearly interpolated value

Example

application:setBackgroundColor(0x4e4e4e)

local pix = Pixel.new(0x00ffff, 1, 32, 32)
local current_position : number = 0
local target_position : number = 5*64
local speed : number = 0.1 -- determines how quickly it approaches the target

stage:addChild(pix)
pix:setPosition(current_position, 2*64)

local pix2 = pix:clone()
pix2:setColor(0xff5500)
stage:addChild(pix2)
pix2:setPosition(target_position, 2*64)

stage:addEventListener(Event.ENTER_FRAME, function(e)
	if current_position > (target_position-0.001) then return end
	current_position = math.lerp(current_position, target_position, speed * e.deltaTime * 30)
	pix:setPosition(current_position, 2*64)
end)

See also

Optimizations#New_math.lerp_luau_function_.28it.27s_bad.21.29 🤷‍♂️
Ftf_snippets#LERP_.40xxx