From 89433ddeb48aa3410ac316e2550798e625c113bb Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 4 Apr 2024 18:14:32 -0400 Subject: [PATCH] Trail length FPS independence --- core/src/mindustry/graphics/Trail.java | 34 ++++++++++++++++---------- gradle.properties | 2 +- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/core/src/mindustry/graphics/Trail.java b/core/src/mindustry/graphics/Trail.java index 7594257642..48e8ff900e 100644 --- a/core/src/mindustry/graphics/Trail.java +++ b/core/src/mindustry/graphics/Trail.java @@ -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; diff --git a/gradle.properties b/gradle.properties index 1733b891ba..5aca247a29 100644 --- a/gradle.properties +++ b/gradle.properties @@ -25,4 +25,4 @@ org.gradle.caching=true #used for slow jitpack builds; TODO see if this actually works org.gradle.internal.http.socketTimeout=100000 org.gradle.internal.http.connectionTimeout=100000 -archash=e312cc96f5 +archash=4ea3d3b7e8