Moving an object from vector A to B in 2d medium with a percentage step

I know the coordinates of the vectors A and B. How can we count the first point between these two vectors? The first vector X is equal to 1% of the distance between vectors A and B. Therefore, first I will move the object in the vector A 1% closer to the vector B. Therefore, I need to calculate the vector X, which is the new vector for the object, until it reaches vector B.

+6
source share
2 answers

Do you want lerp ing. For reference, the basic formula is:

x = A + t * (B - A)

Where t is between 0 and 1. (Anything outside of this range makes it extra ).

Make sure x = A when t = 0 and x = B when t = 1 .

Please note that my answer does not mention vectors or 2D.

+18
source

Including aib answer in code:

 function lerp(a, b, t) { var len = a.length; if(b.length != len) return; var x = []; for(var i = 0; i < len; i++) x.push(a[i] + t * (b[i] - a[i])); return x; } var A = [1,2,3]; var B = [2,5,6]; var X = lerp(A, B, 0.01); 
+14
source

Source: https://habr.com/ru/post/889516/


All Articles