Game Development Community

Interpolate

by Christian M Weber · in Torque 2D Beginner · 12/07/2014 (11:31 am) · 0 replies

I didn't find a interpolate function for T2D so here is one in script without needing to rebuild C++ code.


function Vector2Interpolate(%startX, %startY, %endX, %endY, %factor) {

%inverse = 1.0 - %factor;
%resultX = %startX * %inverse + %endX * %factor;
%resultY = %startY * %inverse + %endY * %factor;

return %resultX SPC %resultY;
}


This is a handy function to find points along a line within 2d space.

examples:

find mid point between two points:
Vector2Interpolate(0, 0, 10, 10, 0.5) returns "5 5"

or find point 75% of the way
Vector2Interpolate(0, 0, 10, 10, 0.75) returns "7.5 7.5"

%factor is not limited, you can input "2" and it'll return "20 20". Input "-1" and it'll return "-10 -10"