Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snap angles #379

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changes/unreleased/Changed-20230811-081227.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions core-tests/src/com/agateau/utils/tests/AgcMathUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ 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(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));
}
}
4 changes: 4 additions & 0 deletions core/src/com/agateau/pixelwheels/racer/VehicleRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 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;
float yOffset =
Expand Down
14 changes: 14 additions & 0 deletions core/src/com/agateau/utils/AgcMathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -222,4 +224,16 @@ 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) {
float snappedValue = MathUtils.round(value / 90f) * 90f;
if (Math.abs(snappedValue - value) <= SNAP_ANGLE_THRESHOLD) {
return snappedValue;
}
return value;
}
}
Loading