From c84530857c65d95955b167f12fddcf55ec10b100 Mon Sep 17 00:00:00 2001 From: Aurelien Gateau Date: Tue, 25 Jul 2023 21:05:52 +0200 Subject: [PATCH 1/2] Snap angles --- .../src/com/agateau/utils/tests/AgcMathUtilsTests.java | 7 +++++++ .../com/agateau/pixelwheels/racer/VehicleRenderer.java | 4 ++++ core/src/com/agateau/utils/AgcMathUtils.java | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java b/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java index 08a2fb35b..9b1fbf122 100644 --- a/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java +++ b/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java @@ -137,4 +137,11 @@ public void testLineDoesNotCrossSegment() { assertThat(AgcMathUtils.lineCrossesSegment(l1, l2, s1, s2), is(false)); assertThat(AgcMathUtils.lineCrossesSegment(l1, l2, s2, s1), is(false)); } + + @Test + public void testSnapAngle() { + assertThat(AgcMathUtils.snapAngle(92f), is(90f)); + assertThat(AgcMathUtils.snapAngle(87f), is(90f)); + assertThat(AgcMathUtils.snapAngle(183f), is(180f)); + } } diff --git a/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java b/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java index aaf480a36..72fe66333 100644 --- a/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java +++ b/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java @@ -25,6 +25,7 @@ import com.agateau.pixelwheels.gameobject.CellFrameBufferManager; import com.agateau.pixelwheels.gameobject.CellFrameBufferUser; import com.agateau.pixelwheels.utils.BodyRegionDrawer; +import com.agateau.utils.AgcMathUtils; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.Animation; @@ -85,6 +86,9 @@ public void init(CellFrameBufferManager manager) { private void drawBodyToCell(Batch batch, Body body, TextureRegion region) { float angle = body.getAngle() * MathUtils.radiansToDegrees; + // Snap angles so that the vehicles do not slightly rotated when facing north, south, east + // or west. This is especially useful at startup. + angle = AgcMathUtils.snapAngle(angle); float xOffset = (body.getPosition().x - mVehicle.getPosition().x) / Constants.UNIT_FOR_PIXEL; float yOffset = diff --git a/core/src/com/agateau/utils/AgcMathUtils.java b/core/src/com/agateau/utils/AgcMathUtils.java index 25083a487..36da7a4b4 100644 --- a/core/src/com/agateau/utils/AgcMathUtils.java +++ b/core/src/com/agateau/utils/AgcMathUtils.java @@ -222,4 +222,13 @@ public static boolean rectangleContains(Rectangle rect, Vector2 position, float } return true; } + + public static float snapAngle(float value) { + value = normalizeAngle(value); + float snappedValue = MathUtils.round(value / 90f) * 90f; + if (Math.abs(snappedValue - value) < 2) { + return snappedValue; + } + return value; + } } From 545a40734650c309f8f045290fc0bb6259f0ce1a Mon Sep 17 00:00:00 2001 From: Aurelien Gateau Date: Fri, 11 Aug 2023 08:14:10 +0200 Subject: [PATCH 2/2] Do not draw slightly rotated vehicles on the start line --- .changes/unreleased/Changed-20230811-081227.yaml | 4 ++++ .../src/com/agateau/utils/tests/AgcMathUtilsTests.java | 9 +++++++-- .../com/agateau/pixelwheels/racer/VehicleRenderer.java | 4 ++-- core/src/com/agateau/utils/AgcMathUtils.java | 9 +++++++-- 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .changes/unreleased/Changed-20230811-081227.yaml diff --git a/.changes/unreleased/Changed-20230811-081227.yaml b/.changes/unreleased/Changed-20230811-081227.yaml new file mode 100644 index 000000000..d50ca3d84 --- /dev/null +++ b/.changes/unreleased/Changed-20230811-081227.yaml @@ -0,0 +1,4 @@ +kind: Changed +body: Vehicles are no longer drawn slightly rotated on the start line, making them + look better. +time: 2023-08-11T08:12:27.301161038+02:00 diff --git a/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java b/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java index 9b1fbf122..65db83763 100644 --- a/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java +++ b/core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java @@ -141,7 +141,12 @@ public void testLineDoesNotCrossSegment() { @Test public void testSnapAngle() { assertThat(AgcMathUtils.snapAngle(92f), is(90f)); - assertThat(AgcMathUtils.snapAngle(87f), is(90f)); - assertThat(AgcMathUtils.snapAngle(183f), is(180f)); + assertThat(AgcMathUtils.snapAngle(182f), is(180f)); + assertThat(AgcMathUtils.snapAngle(-89f), is(-90f)); + assertThat(AgcMathUtils.snapAngle(-92f), is(-90f)); + + // Untouched values + assertThat(AgcMathUtils.snapAngle(87f), is(87f)); + assertThat(AgcMathUtils.snapAngle(183f), is(183f)); } } diff --git a/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java b/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java index 72fe66333..c2b4c1fcb 100644 --- a/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java +++ b/core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java @@ -86,8 +86,8 @@ public void init(CellFrameBufferManager manager) { private void drawBodyToCell(Batch batch, Body body, TextureRegion region) { float angle = body.getAngle() * MathUtils.radiansToDegrees; - // Snap angles so that the vehicles do not slightly rotated when facing north, south, east - // or west. This is especially useful at startup. + // Snap angles so that the vehicle body textures are not drawn slightly rotated when facing + // north, south, east or west. This is especially useful at startup. angle = AgcMathUtils.snapAngle(angle); float xOffset = (body.getPosition().x - mVehicle.getPosition().x) / Constants.UNIT_FOR_PIXEL; diff --git a/core/src/com/agateau/utils/AgcMathUtils.java b/core/src/com/agateau/utils/AgcMathUtils.java index 36da7a4b4..cef30db59 100644 --- a/core/src/com/agateau/utils/AgcMathUtils.java +++ b/core/src/com/agateau/utils/AgcMathUtils.java @@ -28,6 +28,8 @@ public class AgcMathUtils { public static final float msToKmh = 3600 / 1000f; public static final float kmhToMs = 1 / msToKmh; + private static final float SNAP_ANGLE_THRESHOLD = 2; + /** Wrap angles if they are less than 0 or greater than 360 */ public static float normalizeAngle(float angle) { return modulo(angle, 360); @@ -223,10 +225,13 @@ public static boolean rectangleContains(Rectangle rect, Vector2 position, float return true; } + /** + * Snap angle when its value is close to a multiple of 90°. "Close" means a difference of less + * than SNAP_ANGLE_THRESHOLD. + */ public static float snapAngle(float value) { - value = normalizeAngle(value); float snappedValue = MathUtils.round(value / 90f) * 90f; - if (Math.abs(snappedValue - value) < 2) { + if (Math.abs(snappedValue - value) <= SNAP_ANGLE_THRESHOLD) { return snappedValue; } return value;