diff --git a/CHANGES b/CHANGES index d95c8d75e5e..c9d7e3fa4ae 100755 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/gdx/src/com/badlogic/gdx/math/Vector2.java b/gdx/src/com/badlogic/gdx/math/Vector2.java index 9a424ae81ca..edef0ef7617 100644 --- a/gdx/src/com/badlogic/gdx/math/Vector2.java +++ b/gdx/src/com/badlogic/gdx/math/Vector2.java @@ -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) */ @@ -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. */ @@ -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. @@ -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 */ diff --git a/gdx/test/com/badlogic/gdx/math/Vector2Test.java b/gdx/test/com/badlogic/gdx/math/Vector2Test.java index a03ca82c6f4..99ac705034e 100644 --- a/gdx/test/com/badlogic/gdx/math/Vector2Test.java +++ b/gdx/test/com/badlogic/gdx/math/Vector2Test.java @@ -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); + } } diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/GroupTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/GroupTest.java index dc31d053d1f..ba8b2aab84a 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/GroupTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/GroupTest.java @@ -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()); diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/Vector2dTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/Vector2dTest.java index 593931d8dfe..6aaa112dfc7 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/Vector2dTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/Vector2dTest.java @@ -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));