Skip to content

Commit

Permalink
Fix #5388. Made Vector2 angle methods consistent (#5428)
Browse files Browse the repository at this point in the history
Fix #5388. Made Vector2 angle methods consistent
  • Loading branch information
obigu committed Sep 2, 2020
1 parent 2b58d87 commit 1fc7cd4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -24,6 +24,8 @@
- API Addition: Added a Lwjgl3ApplicationConfiguration#foregroundFPS option.
- API Change: Utility classes are now final and have a private constructor to prevent instantiation.
- API Change: ScrollPane now supports all combinations of scrollBarsOnTop and fadeScrollBars.
- API Addition: Added new methods with a "deg" suffix in the method's name for all Vector2 degrees-based methods and deprecated the old ones.
- API Change: Vector2 angleRad(Vector2) now correctly return counter-clockwise angles.

[1.9.11]
- Update to MobiVM 2.3.8
Expand Down
61 changes: 53 additions & 8 deletions gdx/src/com/badlogic/gdx/math/Vector2.java
Expand Up @@ -317,18 +317,38 @@ public float crs (float x, float y) {
}

/** @return the angle in degrees of this vector (point) relative to the x-axis. Angles are towards the positive y-axis
* (typically counter-clockwise) and between 0 and 360. */
* (typically counter-clockwise) and between 0 and 360.
* @deprecated use {@link #angleDeg()} instead. */
@Deprecated
public float angle () {
float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees;
if (angle < 0) angle += 360;
return angle;
}

/** @return the angle in degrees of this vector (point) relative to the given vector. Angles are towards the positive y-axis
* (typically counter-clockwise.) between -180 and +180 */
/** @return the angle in degrees of this vector (point) relative to the given vector. Angles are towards the negative y-axis
* (typically clockwise) between -180 and +180
* @deprecated use {@link #angleDeg(Vector2)} instead. Be ware of the changes in returned angle to counter-clockwise and the range. */
@Deprecated
public float angle (Vector2 reference) {
return (float)Math.atan2(crs(reference), dot(reference)) * MathUtils.radiansToDegrees;
}

/** @return the angle in degrees of this vector (point) relative to the x-axis. Angles are towards the positive y-axis
* (typically counter-clockwise) and in the [0, 360) range. */
public float angleDeg () {
float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees;
if (angle < 0) angle += 360;
return angle;
}

/** @return the angle in degrees of this vector (point) relative to the given vector. Angles are towards the positive y-axis
* (typically counter-clockwise.) in the [0, 360) range */
public float angleDeg (Vector2 reference) {
float angle = (float)Math.atan2(reference.crs(this), reference.dot(this)) * MathUtils.radiansToDegrees;
if (angle < 0) angle += 360;
return angle;
}

/** @return the angle in radians of this vector (point) relative to the x-axis. Angles are towards the positive y-axis.
* (typically counter-clockwise) */
Expand All @@ -339,14 +359,22 @@ public float angleRad () {
/** @return the angle in radians of this vector (point) relative to the given vector. Angles are towards the positive y-axis.
* (typically counter-clockwise.) */
public float angleRad (Vector2 reference) {
return (float)Math.atan2(crs(reference), dot(reference));
return (float)Math.atan2(reference.crs(this), reference.dot(this));
}

/** Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
* @param degrees The angle in degrees to set. */
* @param degrees The angle in degrees to set.
* @deprecated use {@link #setAngleDeg(float)} instead. */
@Deprecated
public Vector2 setAngle (float degrees) {
return setAngleRad(degrees * MathUtils.degreesToRadians);
}

/** Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
* @param degrees The angle in degrees to set. */
public Vector2 setAngleDeg (float degrees) {
return setAngleRad(degrees * MathUtils.degreesToRadians);
}

/** Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
* @param radians The angle in radians to set. */
Expand All @@ -358,16 +386,26 @@ public Vector2 setAngleRad (float radians) {
}

/** Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees */
* @param degrees the angle in degrees
* @deprecated use {@link #rotateDeg(float)} instead. */
@Deprecated
public Vector2 rotate (float degrees) {
return rotateRad(degrees * MathUtils.degreesToRadians);
}

/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees
* @param reference center Vector2 */
* @param reference center Vector2
* @deprecated use {@link #rotateAroundDeg(Vector2, float)} instead. */
@Deprecated
public Vector2 rotateAround (Vector2 reference, float degrees) {
return this.sub(reference).rotate(degrees).add(reference);
return this.sub(reference).rotateDeg(degrees).add(reference);
}

/** Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees */
public Vector2 rotateDeg (float degrees) {
return rotateRad(degrees * MathUtils.degreesToRadians);
}

/** Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
Expand All @@ -385,6 +423,13 @@ public Vector2 rotateRad (float radians) {
return this;
}

/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees
* @param reference center Vector2 */
public Vector2 rotateAroundDeg (Vector2 reference, float degrees) {
return this.sub(reference).rotateDeg(degrees).add(reference);
}

/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
* @param radians the angle in radians
* @param reference center Vector2 */
Expand Down
20 changes: 20 additions & 0 deletions gdx/test/com/badlogic/gdx/math/Vector2Test.java
Expand Up @@ -15,4 +15,24 @@ public void testToString () {
public void testFromString () {
assertEquals(new Vector2(-5f, 42.00055f), new Vector2().fromString("(-5,42.00055)"));
}

@Test
public void testAngle() {
assertEquals(270f, new Vector2(0, -1f).angleDeg(), MathUtils.FLOAT_ROUNDING_ERROR);
}

@Test
public void testAngleRelative() {
assertEquals(270f, new Vector2(0, -1f).angleDeg(Vector2.X), MathUtils.FLOAT_ROUNDING_ERROR);
}

@Test
public void testAngleRad() {
assertEquals(- MathUtils.PI / 2f, new Vector2(0, -1f).angleRad(), MathUtils.FLOAT_ROUNDING_ERROR);
}

@Test
public void testAngleRadRelative() {
assertEquals(- MathUtils.PI / 2f, new Vector2(0, -1f).angleRad(Vector2.X), MathUtils.FLOAT_ROUNDING_ERROR);
}
}
2 changes: 1 addition & 1 deletion tests/gdx-tests/src/com/badlogic/gdx/tests/GroupTest.java
Expand Up @@ -206,7 +206,7 @@ public boolean mouseMoved (InputEvent event, float x, float y) {

public void draw (Batch batch, float parentAlpha) {
// Use Stage#toScreenCoordinates, which we know is correct.
toScreenCoordinates.set(testX, testY).sub(getOriginX(), getOriginY()).scl(getScaleX(), getScaleY()).rotate(getRotation())
toScreenCoordinates.set(testX, testY).sub(getOriginX(), getOriginY()).scl(getScaleX(), getScaleY()).rotateDeg(getRotation())
.add(getOriginX(), getOriginY()).add(getX(), getY());
getStage().toScreenCoordinates(toScreenCoordinates, batch.getTransformMatrix());

Expand Down
Expand Up @@ -87,7 +87,7 @@ public void render () {
renderer.setColor(Color.WHITE);

renderVectorAt(2, 2, rotating);
rotating.rotate(93 * changeRate);
rotating.rotateDeg(93 * changeRate);

renderVectorAt(2, -2, scalingX);
scalingX.set(0, MathUtils.sin((System.currentTimeMillis() - start) / 520.0f));
Expand Down

0 comments on commit 1fc7cd4

Please sign in to comment.