[Navigation 2D] Fix sign of cross product

Regression from splitting the servers. Also replaces the method for
getting the triangle area.
This commit is contained in:
A Thousand Ships 2025-09-23 13:52:31 +02:00
parent 3d91a48298
commit f8d72ba783
No known key found for this signature in database
GPG key ID: DEFC5A5B1306947D
4 changed files with 77 additions and 6 deletions

View file

@ -40,7 +40,7 @@
using namespace Nav2D;
#define THREE_POINTS_CROSS_PRODUCT(m_a, m_b, m_c) (((m_c) - (m_a)).cross((m_b) - (m_a)))
#define THREE_POINTS_CROSS_PRODUCT(m_a, m_b, m_c) (-((m_c) - (m_a)).cross((m_b) - (m_a)))
bool NavMeshQueries2D::emit_callback(const Callable &p_callback) {
ERR_FAIL_COND_V(!p_callback.is_valid(), false);
@ -897,7 +897,7 @@ ClosestPointQueryResult NavMeshQueries2D::map_iteration_get_closest_point_info(c
const LocalVector<Ref<NavRegionIteration2D>> &regions = p_map_iteration.region_iterations;
for (const Ref<NavRegionIteration2D> &region : regions) {
for (const Polygon &polygon : region->get_navmesh_polygons()) {
real_t cross = (polygon.vertices[1] - polygon.vertices[0]).cross(polygon.vertices[2] - polygon.vertices[0]);
real_t cross = -(polygon.vertices[1] - polygon.vertices[0]).cross(polygon.vertices[2] - polygon.vertices[0]);
Vector2 closest_on_polygon;
real_t closest = FLT_MAX;
bool inside = true;
@ -905,7 +905,7 @@ ClosestPointQueryResult NavMeshQueries2D::map_iteration_get_closest_point_info(c
for (uint32_t point_id = 0; point_id < polygon.vertices.size(); ++point_id) {
Vector2 edge = polygon.vertices[point_id] - previous;
Vector2 to_point = p_point - previous;
real_t edge_to_point_cross = edge.cross(to_point);
real_t edge_to_point_cross = -edge.cross(to_point);
bool clockwise = (edge_to_point_cross * cross) > 0;
// If we are not clockwise, the point will never be inside the polygon and so the closest point will be on an edge.
if (!clockwise) {
@ -1029,7 +1029,7 @@ ClosestPointQueryResult NavMeshQueries2D::polygons_get_closest_point_info(const
// TODO: Check for further 2D improvements.
for (const Polygon &polygon : p_polygons) {
real_t cross = (polygon.vertices[1] - polygon.vertices[0]).cross(polygon.vertices[2] - polygon.vertices[0]);
real_t cross = -(polygon.vertices[1] - polygon.vertices[0]).cross(polygon.vertices[2] - polygon.vertices[0]);
Vector2 closest_on_polygon;
real_t closest = FLT_MAX;
bool inside = true;
@ -1037,7 +1037,7 @@ ClosestPointQueryResult NavMeshQueries2D::polygons_get_closest_point_info(const
for (uint32_t point_id = 0; point_id < polygon.vertices.size(); ++point_id) {
Vector2 edge = polygon.vertices[point_id] - previous;
Vector2 to_point = p_point - previous;
real_t edge_to_point_cross = edge.cross(to_point);
real_t edge_to_point_cross = -edge.cross(to_point);
bool clockwise = (edge_to_point_cross * cross) > 0;
// If we are not clockwise, the point will never be inside the polygon and so the closest point will be on an edge.
if (!clockwise) {

View file

@ -36,7 +36,7 @@ struct Triangle2 {
Vector2 vertex[3];
real_t get_area() const {
return Math::sqrt((vertex[0] - vertex[1]).cross(vertex[0] - vertex[2])) * 0.5f;
return Math::abs((vertex[0] - vertex[1]).cross(vertex[0] - vertex[2])) * 0.5f;
}
Vector2 get_random_point_inside() const;