Trail length FPS independence

This commit is contained in:
Anuken
2024-04-04 18:14:32 -04:00
parent 89206a734c
commit 89433ddeb4
2 changed files with 22 additions and 14 deletions

View File

@@ -97,12 +97,11 @@ public class Trail{
/** Removes the last point from the trail at intervals. */
public void shorten(){
if((counter += Time.delta) >= 1f){
if(points.size >= 3){
points.removeRange(0, 2);
}
int count = (int)(counter += Time.delta);
counter -= count;
counter %= 1f;
if(points.size + ((count - 1) * 3) > length * 3){
points.removeRange(0, Math.min(3 * count - 1, points.size - 1));
}
}
@@ -113,18 +112,27 @@ public class Trail{
/** Adds a new point to the trail at intervals. */
public void update(float x, float y, float width){
//TODO fix longer trails at low FPS
if((counter += Time.delta) >= 1f){
if(points.size > length*3){
points.removeRange(0, 2);
int count = (int)(counter += Time.delta);
counter -= count;
if(count > 0){
int toRemove = points.size + (count - 1 - length) * 3;
if(toRemove > 0){
points.removeRange(0, Math.min(toRemove - 1, points.size - 1));
}
points.add(x, y, width);
counter %= 1f;
//if lastX is -1, this trail has never updated, so only add one point - there is nothing to interpolate with
if(count == 1 || lastX == -1f){
points.add(x, y, width);
}else{
for(int i = 0; i < count; i++){
float f = (i + 1f) / count;
points.add(Mathf.lerp(lastX, x, f), Mathf.lerp(lastY, y, f), Mathf.lerp(lastW, width, f));
}
}
}
//update last position regardless, so it joins
//update last position regardless, so it joins at the origin
lastAngle = -Angles.angleRad(x, y, lastX, lastY);
lastX = x;
lastY = y;