interpolateBetween | Multi Theft Auto: Wiki Skip to content

interpolateBetween

Client-side
Server-side
Shared

This page is incomplete! Help wanted!

Please finish this page using the corresponding Old Wiki article.
Go to Contribution guidelines for more information.


Interpolates a 3D Vector between a source value and a target value using either linear interpolation or any other easing function. It can also be used to interpolate 2D vectors or scalars by only setting some of the x, y, z values and putting 0 to the others.

Syntax

interpolateBetween ( )

Code Examples

server

This clientside example uses interpolateBetween to create position and color interpolation(with effect) on a marker. The command to test it is "/marker". The position is interpolated with "OutBounce" as strEasingType to make the marker bounce off the ground and "Linear" interpolation for the color.

local g_Marker = nil
addCommandHandler("marker",
function ()
if g_Marker then return end
local x, y, z = getElementPosition(getLocalPlayer())
z = z - 1
g_Marker = {}
g_Marker.startPos = {x, y, z + 5}
g_Marker.startTime = getTickCount()
g_Marker.startColor = {255, 0, 0, 0}
g_Marker.endPos = {x, y, z}
g_Marker.endTime = g_Marker.startTime + 2000
g_Marker.endColor = {0, 0, 255, 255}
local x, y, z = unpack(g_Marker.startPos)
local r, g, b, a = unpack(g_Marker.startColor)
g_Marker.marker = createMarker(x, y, z, "cylinder", 1, 255, r, g, b, a)
addEventHandler("onClientRender", getRootElement(), popMarkerUp)
end)
function popMarkerUp()
local now = getTickCount()
local elapsedTime = now - g_Marker.startTime
local duration = g_Marker.endTime - g_Marker.startTime
local progress = elapsedTime / duration
local x1, y1, z1 = unpack(g_Marker.startPos)
local x2, y2, z2 = unpack(g_Marker.endPos)
local x, y, z = interpolateBetween (
x1, y1, z1,
x2, y2, z2,
progress, "OutBounce")
setElementPosition(g_Marker.marker, x, y, z)
local r1, g1, b1, a1 = unpack(g_Marker.startColor)
local r2, g2, b2, a2 = unpack(g_Marker.endColor)
local r, g, b = interpolateBetween (
r1, g1, b1,
r2, g2, b2,
progress, "Linear")
local a = interpolateBetween (
a1, 0, 0,
a2, 0, 0,
progress, "Linear")
setMarkerColor(g_Marker.marker , r, g, b, a)
if now >= g_Marker.endTime then
removeEventHandler("onClientRender", getRootElement(), popMarkerUp)
setTimer(
function ()
destroyElement(g_Marker.marker)
g_Marker = nil
end, 3000, 1)
end
end