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; + } }