From 5b68a5c7ece49328aa9008eb29c1eb0c925955be Mon Sep 17 00:00:00 2001 From: Oscar Bigu Date: Fri, 26 Oct 2018 11:41:38 +0200 Subject: [PATCH 1/6] Fix #5388. Made Vector2 angle methods consistent --- CHANGES | 2 ++ gdx/src/com/badlogic/gdx/math/Vector2.java | 10 ++++++---- .../com/badlogic/gdx/math/Vector2Test.java | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 6c1bfab0e63..88dc0a552ea 100755 --- a/CHANGES +++ b/CHANGES @@ -27,6 +27,8 @@ - API Change: Batch and Decal setColor(float) renamed to setPackedColor for differentiation, since the conversion from float to Color is lossy. - API Change: PolygonSprite getVertexColor renamed to getPackedColor to match other classes. - API Change: FreeTypeFontGenerator only generates a missing glyph if \0 is in the characters. +- API Change: Vector2 angle(Vector2) returns value in the [0, 360) range. +- API Change: Vector2 angle(Vector2) and angleRad(Vector2) now correctly return counter-clockwise angles. [1.9.8] - Add iPhoneX images diff --git a/gdx/src/com/badlogic/gdx/math/Vector2.java b/gdx/src/com/badlogic/gdx/math/Vector2.java index 9a424ae81ca..ecaa45dd92c 100644 --- a/gdx/src/com/badlogic/gdx/math/Vector2.java +++ b/gdx/src/com/badlogic/gdx/math/Vector2.java @@ -317,7 +317,7 @@ 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 in the [0, 360) range. */ public float angle () { float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees; if (angle < 0) angle += 360; @@ -325,9 +325,11 @@ public float 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 */ + * (typically counter-clockwise.) in the [0, 360) range */ public float angle (Vector2 reference) { - return (float)Math.atan2(crs(reference), dot(reference)) * MathUtils.radiansToDegrees; + 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. @@ -339,7 +341,7 @@ 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). diff --git a/gdx/test/com/badlogic/gdx/math/Vector2Test.java b/gdx/test/com/badlogic/gdx/math/Vector2Test.java index a03ca82c6f4..fa861c8e8a1 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).angle(), MathUtils.FLOAT_ROUNDING_ERROR); + } + + @Test + public void testAngleRelative() { + assertEquals(270f, new Vector2(0, -1f).angle(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); + } } From c2917c235c356208dff6f4141f8a6a48bd5af1b5 Mon Sep 17 00:00:00 2001 From: obigu Date: Tue, 1 Sep 2020 15:22:03 +0200 Subject: [PATCH 2/6] Added Vector2 methods with "deg" suffix and deprecated non suffix ones --- gdx/src/com/badlogic/gdx/math/Vector2.java | 69 ++++++++++++++++--- .../com/badlogic/gdx/math/Vector2Test.java | 4 +- .../src/com/badlogic/gdx/tests/GroupTest.java | 2 +- .../com/badlogic/gdx/tests/Vector2dTest.java | 2 +- 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/gdx/src/com/badlogic/gdx/math/Vector2.java b/gdx/src/com/badlogic/gdx/math/Vector2.java index ecaa45dd92c..cf915e4956d 100644 --- a/gdx/src/com/badlogic/gdx/math/Vector2.java +++ b/gdx/src/com/badlogic/gdx/math/Vector2.java @@ -316,17 +316,36 @@ public float crs (float x, float y) { return this.x * y - this.y * x; } + /** @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. */ + * (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 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 angle (Vector2 reference) { + 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; @@ -345,10 +364,18 @@ public float angleRad (Vector2 reference) { } /** 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. */ @@ -360,16 +387,19 @@ 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 */ - public Vector2 rotateAround (Vector2 reference, float degrees) { - return this.sub(reference).rotate(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. @@ -387,6 +417,22 @@ 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 + * @deprecated use {@link #rotateAroundDeg(Vector2, float)} instead. */ + @Deprecated + public Vector2 rotateAround (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 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 */ @@ -394,7 +440,10 @@ public Vector2 rotateAroundRad (Vector2 reference, float radians) { return this.sub(reference).rotateRad(radians).add(reference); } - /** Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise. */ + /** Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise. + * @deprecated use {@link #rotateDeg(float)} instead. + * */ + @Deprecated public Vector2 rotate90 (int dir) { float x = this.x; if (dir >= 0) { diff --git a/gdx/test/com/badlogic/gdx/math/Vector2Test.java b/gdx/test/com/badlogic/gdx/math/Vector2Test.java index fa861c8e8a1..99ac705034e 100644 --- a/gdx/test/com/badlogic/gdx/math/Vector2Test.java +++ b/gdx/test/com/badlogic/gdx/math/Vector2Test.java @@ -18,12 +18,12 @@ public void testFromString () { @Test public void testAngle() { - assertEquals(270f, new Vector2(0, -1f).angle(), MathUtils.FLOAT_ROUNDING_ERROR); + assertEquals(270f, new Vector2(0, -1f).angleDeg(), MathUtils.FLOAT_ROUNDING_ERROR); } @Test public void testAngleRelative() { - assertEquals(270f, new Vector2(0, -1f).angle(Vector2.X), MathUtils.FLOAT_ROUNDING_ERROR); + assertEquals(270f, new Vector2(0, -1f).angleDeg(Vector2.X), MathUtils.FLOAT_ROUNDING_ERROR); } @Test 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)); From 2f733f1cd6ddf12464adecd3c1d843fe559de171 Mon Sep 17 00:00:00 2001 From: obigu Date: Tue, 1 Sep 2020 15:27:08 +0200 Subject: [PATCH 3/6] Formatting --- gdx/src/com/badlogic/gdx/math/Vector2.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/gdx/src/com/badlogic/gdx/math/Vector2.java b/gdx/src/com/badlogic/gdx/math/Vector2.java index cf915e4956d..8f33a40c86e 100644 --- a/gdx/src/com/badlogic/gdx/math/Vector2.java +++ b/gdx/src/com/badlogic/gdx/math/Vector2.java @@ -329,7 +329,7 @@ public float angle () { /** @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 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; @@ -388,16 +388,14 @@ 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 - * @deprecated use {@link #rotateDeg(float)} instead. - * */ + * @deprecated use {@link #rotateDeg(float)} instead. */ @Deprecated public Vector2 rotate (float degrees) { return rotateRad(degrees * MathUtils.degreesToRadians); } /** 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 */ public Vector2 rotateDeg (float degrees) { return rotateRad(degrees * MathUtils.degreesToRadians); } @@ -441,8 +439,7 @@ public Vector2 rotateAroundRad (Vector2 reference, float radians) { } /** Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise. - * @deprecated use {@link #rotateDeg(float)} instead. - * */ + * @deprecated use {@link #rotateDeg(float)} instead. */ @Deprecated public Vector2 rotate90 (int dir) { float x = this.x; From b4765b7be6e846c4f6e17dd7a20e051ff07f3a34 Mon Sep 17 00:00:00 2001 From: obigu Date: Tue, 1 Sep 2020 15:42:48 +0200 Subject: [PATCH 4/6] CHANGES --- CHANGES | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index fee33a8a2b8..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 @@ -138,8 +140,6 @@ - API Change: Batch and Decal setColor(float) renamed to setPackedColor for differentiation, since the conversion from float to Color is lossy. - API Change: PolygonSprite getVertexColor renamed to getPackedColor to match other classes. - API Change: FreeTypeFontGenerator only generates a missing glyph if \0 is in the characters. -- API Change: Vector2 angle(Vector2) returns value in the [0, 360) range. -- API Change: Vector2 angle(Vector2) and angleRad(Vector2) now correctly return counter-clockwise angles. - API Change: DragScrollListener no longer requires the touch/mouse cursor to be directly above/below the scroll pane. - API Change: List#toString(Object) changed from protected to public. Subclasses overriding this need to change to public. - API Change: List now handles more key presses. From 81e5d8032f71527f8d26e975341cba7d765980df Mon Sep 17 00:00:00 2001 From: obigu Date: Wed, 2 Sep 2020 02:14:54 +0200 Subject: [PATCH 5/6] Remove rotate90 deprecation --- gdx/src/com/badlogic/gdx/math/Vector2.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gdx/src/com/badlogic/gdx/math/Vector2.java b/gdx/src/com/badlogic/gdx/math/Vector2.java index 8f33a40c86e..0d9dae81f7f 100644 --- a/gdx/src/com/badlogic/gdx/math/Vector2.java +++ b/gdx/src/com/badlogic/gdx/math/Vector2.java @@ -438,9 +438,7 @@ public Vector2 rotateAroundRad (Vector2 reference, float radians) { return this.sub(reference).rotateRad(radians).add(reference); } - /** Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise. - * @deprecated use {@link #rotateDeg(float)} instead. */ - @Deprecated + /** Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise. */ public Vector2 rotate90 (int dir) { float x = this.x; if (dir >= 0) { From 01cded4077e6c7802f266dd8b2497ecc66b586d0 Mon Sep 17 00:00:00 2001 From: obigu Date: Wed, 2 Sep 2020 16:22:35 +0200 Subject: [PATCH 6/6] Formatting --- gdx/src/com/badlogic/gdx/math/Vector2.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/gdx/src/com/badlogic/gdx/math/Vector2.java b/gdx/src/com/badlogic/gdx/math/Vector2.java index 0d9dae81f7f..edef0ef7617 100644 --- a/gdx/src/com/badlogic/gdx/math/Vector2.java +++ b/gdx/src/com/badlogic/gdx/math/Vector2.java @@ -316,7 +316,6 @@ public float crs (float x, float y) { return this.x * y - this.y * x; } - /** @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. * @deprecated use {@link #angleDeg()} instead. */ @@ -394,6 +393,15 @@ 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 + * @deprecated use {@link #rotateAroundDeg(Vector2, float)} instead. */ + @Deprecated + public Vector2 rotateAround (Vector2 reference, float degrees) { + 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) { @@ -415,15 +423,6 @@ 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 - * @deprecated use {@link #rotateAroundDeg(Vector2, float)} instead. */ - @Deprecated - public Vector2 rotateAround (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 degrees the angle in degrees * @param reference center Vector2 */