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

Add handedness enum for is_polygon_clockwise #89635

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

exodrifter
Copy link

@exodrifter exodrifter commented Mar 18, 2024

When this was originally implemented in 16022da, the implementation treated the vertices as being in a space where the y-axis is positive in the up direction. This causes it to return the incorrect value for 2D games, since Godot treats the y-axis as being positive in the down direction.

This change maintains compatibility with existing uses of is_polygon_clockwise by adding an optional parameter which allows the user to select which handedness their coordinate system uses. The documentation for is_polygon_clockwise has also been updated to describe the difference.

This PR replaces #80035, from which it differs in the following ways:

  • Compatibility with existing calls is preserved.
  • Instead of always flipping the return result, the return result is only flipped when the left-handed version is asked for.

Fixes #49716.

@AThousandShips
Copy link
Member

For compatibility you need to add compatibility bindings, will explain how to add those, when I'm back at my computer

@exodrifter exodrifter force-pushed the exodrifter/polygon-handedness branch 2 times, most recently from 90d65d8 to 3d2f405 Compare March 18, 2024 10:04
@AThousandShips
Copy link
Member

AThousandShips commented Mar 18, 2024

This needs to be added:

diff --git a/core/core_bind.compat.inc b/core/core_bind.compat.inc
new file mode 100644
index 0000000000..aa4399bb76
--- /dev/null
+++ b/core/core_bind.compat.inc
@@ -0,0 +1,47 @@
+/**************************************************************************/
+/*  core_bind.compat.inc                                                  */
+/**************************************************************************/
+/*                         This file is part of:                          */
+/*                             GODOT ENGINE                               */
+/*                        https://godotengine.org                         */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
+/*                                                                        */
+/* Permission is hereby granted, free of charge, to any person obtaining  */
+/* a copy of this software and associated documentation files (the        */
+/* "Software"), to deal in the Software without restriction, including    */
+/* without limitation the rights to use, copy, modify, merge, publish,    */
+/* distribute, sublicense, and/or sell copies of the Software, and to     */
+/* permit persons to whom the Software is furnished to do so, subject to  */
+/* the following conditions:                                              */
+/*                                                                        */
+/* The above copyright notice and this permission notice shall be         */
+/* included in all copies or substantial portions of the Software.        */
+/*                                                                        */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
+/**************************************************************************/
+
+#ifndef DISABLE_DEPRECATED
+
+namespace core_bind {
+
+////// Geometry2D //////
+
+bool Geometry2D::_is_polygon_clockwise_bind_compat_89635(const Vector<Vector2> &p_polygon) {
+       return is_polygon_clockwise(p_polygon, RIGHT_HAND);
+}
+
+void Geometry2D::_bind_compatibility_methods() {
+       ClassDB::bind_compatibility_method(D_METHOD("is_polygon_clockwise", "polygon"), &Geometry2D::_is_polygon_clockwise_bind_compat_89635);
+}
+
+} // namespace core_bind
+
+#endif // DISABLE_DEPRECATED
diff --git a/core/core_bind.cpp b/core/core_bind.cpp
index 6927db002b..2185775dc0 100644
--- a/core/core_bind.cpp
+++ b/core/core_bind.cpp
@@ -29,6 +29,7 @@
 /**************************************************************************/
 
 #include "core_bind.h"
+#include "core_bind.compat.inc"
 
 #include "core/config/project_settings.h"
 #include "core/crypto/crypto_core.h"
diff --git a/core/core_bind.h b/core/core_bind.h
index 305f616cef..e27bfa26ef 100644
--- a/core/core_bind.h
+++ b/core/core_bind.h
@@ -257,6 +257,11 @@ class Geometry2D : public Object {
 protected:
 	static void _bind_methods();
 
+#ifndef DISABLE_DEPRECATED
+	bool _is_polygon_clockwise_bind_compat_89635(const Vector<Vector2> &p_polygon);
+	static void _bind_compatibility_methods();
+#endif // DISABLE_DEPRECATED
+
 public:
 	static Geometry2D *get_singleton();
 	Variant segment_intersects_segment(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b);
diff --git a/misc/extension_api_validation/4.2-stable.expected b/misc/extension_api_validation/4.2-stable.expected
index 8623e8eb25..cf4cb1590f 100644
--- a/misc/extension_api_validation/4.2-stable.expected
+++ b/misc/extension_api_validation/4.2-stable.expected
@@ -248,3 +248,10 @@ Validate extension JSON: Error: Field 'classes/AcceptDialog/methods/register_tex
 Validate extension JSON: Error: Field 'classes/AcceptDialog/methods/remove_button/arguments/0': type changed value in new API, from "Control" to "Button".
 
 Changed argument type to the more specific one actually expected by the method. Compatibility method registered.
+
+
+GH-89635
+--------
+Validate extension JSON: Error: Field 'classes/Geometry2D/methods/is_polygon_clockwise/arguments': size changed value in new API, from 1 to 2.
+
+Added handedness argument. Compatibility method registered.

@exodrifter exodrifter force-pushed the exodrifter/polygon-handedness branch from 3d2f405 to 5d606fe Compare March 19, 2024 03:27
@exodrifter exodrifter requested review from a team as code owners March 19, 2024 03:27
@exodrifter
Copy link
Author

exodrifter commented Mar 19, 2024

@AThousandShips Added in 5d606feff84f4c129ead0ae73600172af0f690b4. I'm not terribly familiar with how this works but I'll try to keep it in mind for future PRs.

@AThousandShips
Copy link
Member

When I have the energy and time I'm going to write a doc page on this, but it's a bit of a large task so haven't been able to focus on it

doc/classes/Geometry2D.xml Outdated Show resolved Hide resolved
@exodrifter exodrifter force-pushed the exodrifter/polygon-handedness branch from 5d606fe to afd68d5 Compare March 20, 2024 10:06
core/core_bind.h Outdated Show resolved Hide resolved
core/core_bind.h Outdated Show resolved Hide resolved
core/math/geometry_2d.h Outdated Show resolved Hide resolved
core/math/geometry_2d.h Outdated Show resolved Hide resolved
@exodrifter exodrifter force-pushed the exodrifter/polygon-handedness branch 2 times, most recently from dadedf2 to 239a2aa Compare March 20, 2024 10:21
Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me but the need and approach would need a look by the core team :)

@lawnjelly
Copy link
Member

Agree that more eyes on this would be good. Imo it would benefit from some more "user friendliness" opinions:

  • First, just to re-iterate, I'm not absolutely sure whether right handed and left handed coordinate system is the correct term for this in 2D, we need a mathematician to confirm / or change to the correct term. I used this because it seemed "similar" to the cases in 3D.
  • I suspect the documentation needs to make more clear when and why this flip might be needed, and why. At the moment it seems very unclear to users (it's not a simple concept, evidently).

Also, we should always consider the alternative - to keep the function the same, and simply mention this problem in the classref and documentation, and leave it to the user to flip where necessary (after all it's just a single operator). I would slightly lean towards making it explicit as in this PR, but opinions may differ on this.

This probably needs some more bikeshedding on how to best handle before proceeding, as it isn't clear cut.

When this was originally implemented in
16022da, the implementation treated the
vertices as being in a space where the y-axis is positive in the up
direction. This causes it to return the incorrect value for 2D games,
since Godot treats the y-axis as being positive in the down direction.

This change maintains compatibility with existing uses of
`is_polygon_clockwise` by adding an optional parameter which allows the
user to select which handedness their coordinate system uses. The
documentation for `is_polygon_clockwise` has also been updated to
describe the difference.

Fixes godotengine#49716.
@akien-mga akien-mga added enhancement and removed bug labels Jun 12, 2024
@akien-mga akien-mga modified the milestones: 4.3, 4.x Jun 12, 2024
@kinami-imai
Copy link
Contributor

Handedness seems to be the correct term of art for 2D Cartesian coordinates: https://en.wikipedia.org/wiki/Cartesian_coordinate_system#Orientation_and_handedness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Geometry.is_polygon_clockwise seems to return the opposite of expected results
6 participants