I need to start a sprite at a scale .6 (scale1) at a y position 40 (yPos1). Knowing the end y position 560 (yPos2) and the end scale 1 (scale2):

How do I calculate the scale at various y positions between yPos1 and yPos2?

  • \$\begingroup\$ Well, what type of interpolation do you want? Linear? Cubic? Square? Ease-in-out? Bounce? \$\endgroup\$ – Bálint Nov 22 '16 at 22:23

First get the difference of both scale and y pos

difY = y2-y1;
difS = s2-s1;

Then if you have a y position between the two given y positions, convert that to a normalised position 0-1

posY = ? 
normY = (posY- y1)/difY; // get the normalised distance between the points

Then you can use that normal to calculate the scale

scale = (difS * normY) + s1

If you want to easeInOut you can do that to the normal first

function easeInOut(n,p){  // n val 0-1. p > 1 the amount of ease p=1 none 2 a little ease, 4 a lot of ease
   nn = pow(n,p);  // pow is a raise n to the power of p
   return nn / (nn + pow(1 - n,p));

scale = (difS * easeInOut(normY,2)) + s1;

There are many functions that you can apply to the normalised position to modify the movement between two points.

If you have more than two points and you want the easing to be smooth along the whole path consider using beziers for the easing functions as they provide a simple way to get parabolas to fit between a given set of points (namely the 0 and 1 of the normalised y positions);

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.