Skip to content

Instantly share code, notes, and snippets.

@qassa
Last active May 2, 2017 07:41
Show Gist options
  • Save qassa/595edfcd2f3034169ccd to your computer and use it in GitHub Desktop.
Save qassa/595edfcd2f3034169ccd to your computer and use it in GitHub Desktop.
lerp realization in Java using interpolation by time. Ability to set determine speed of object when it's moving on the straight line (X,Y) on the scene.
public int lerp(float start, float target, float duration, float timeSinceStart){
float value = start;
if(timeSinceStart>=0.0f && timeSinceStart < duration){
float range = target - start;
float percent =timeSinceStart / duration;
value = start + range * percent;
}
else if(timeSinceStart>=duration)
value = target;
return Math.round(value);
}
while(true){
long start = System.currentTimeMillis();
long elapsedTime = System.currentTimeMillis() - start;
double b = Math.max(destinationX,x)-Math.min(destinationX,x);
double c = Math.max(destinationY,y)-Math.min(destinationY,y);
float a = (float)Math.sqrt(Math.pow(b,2)+Math.pow(c,2));
float speed = 0.1f;
int currentValue = lerp(startX,destinationX,a/speed,elapsedTime);
int currentValue = lerp(startY,destinationY,a/speed,elapsedTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment