From 77b8e0fcb7cfcdabed7e7217caedf69c049c7a3e Mon Sep 17 00:00:00 2001 From: kleonc <9283098+kleonc@users.noreply.github.com> Date: Fri, 19 May 2023 13:28:50 +0200 Subject: [PATCH] Allow setting negative WorldBoundaryShape2D.distance through the editor --- core/math/plane.cpp | 4 --- doc/classes/Plane.xml | 6 ++--- doc/classes/WorldBoundaryShape2D.xml | 5 ++-- .../collision_shape_2d_editor_plugin.cpp | 3 ++- scene/resources/world_boundary_shape_2d.cpp | 26 ++++++++++++------- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/core/math/plane.cpp b/core/math/plane.cpp index 99694d78712..6b9bcea0813 100644 --- a/core/math/plane.cpp +++ b/core/math/plane.cpp @@ -98,13 +98,11 @@ bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 Vector3 segment = p_dir; real_t den = normal.dot(segment); - //printf("den is %i\n",den); if (Math::is_zero_approx(den)) { return false; } real_t dist = (normal.dot(p_from) - d) / den; - //printf("dist is %i\n",dist); if (dist > (real_t)CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist @@ -121,13 +119,11 @@ bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vec Vector3 segment = p_begin - p_end; real_t den = normal.dot(segment); - //printf("den is %i\n",den); if (Math::is_zero_approx(den)) { return false; } real_t dist = (normal.dot(p_begin) - d) / den; - //printf("dist is %i\n",dist); if (dist < (real_t)-CMP_EPSILON || dist > (1.0f + (real_t)CMP_EPSILON)) { return false; diff --git a/doc/classes/Plane.xml b/doc/classes/Plane.xml index fa29244c2ae..0b147934d41 100644 --- a/doc/classes/Plane.xml +++ b/doc/classes/Plane.xml @@ -138,7 +138,7 @@ - Returns a copy of the plane, normalized. + Returns a copy of the plane, with normalized [member normal] (so it's a unit vector). Returns [code]Plane(0, 0, 0, 0)[/code] if [member normal] can't be normalized (it has zero length). @@ -151,11 +151,11 @@ - The distance from the origin to the plane, in the direction of [member normal]. This value is typically non-negative. + The distance from the origin to the plane, expressed in terms of [member normal] (according to its direction and magnitude). Actual absolute distance from the origin to the plane can be calculated as [code]abs(d) / normal.length()[/code] (if [member normal] has zero length then this [Plane] does not represent a valid plane). In the scalar equation of the plane [code]ax + by + cz = d[/code], this is [code]d[/code], while the [code](a, b, c)[/code] coordinates are represented by the [member normal] property. - The normal of the plane, which must be a unit vector. + The normal of the plane, typically a unit vector. Shouldn't be a zero vector as [Plane] with such [member normal] does not represent a valid plane. In the scalar equation of the plane [code]ax + by + cz = d[/code], this is the vector [code](a, b, c)[/code], where [code]d[/code] is the [member d] property. diff --git a/doc/classes/WorldBoundaryShape2D.xml b/doc/classes/WorldBoundaryShape2D.xml index 4ee4b2551c7..9de10a531f8 100644 --- a/doc/classes/WorldBoundaryShape2D.xml +++ b/doc/classes/WorldBoundaryShape2D.xml @@ -10,10 +10,11 @@ - The line's distance from the origin. + The distance from the origin to the line, expressed in terms of [member normal] (according to its direction and magnitude). Actual absolute distance from the origin to the line can be calculated as [code]abs(distance) / normal.length()[/code]. + In the scalar equation of the line [code]ax + by = d[/code], this is [code]d[/code], while the [code](a, b)[/code] coordinates are represented by the [member normal] property. - The line's normal. Defaults to [code]Vector2.UP[/code]. + The line's normal, typically a unit vector. Its direction indicates the non-colliding half-plane. Can be of any length but zero. Defaults to [code]Vector2.UP[/code]. diff --git a/editor/plugins/collision_shape_2d_editor_plugin.cpp b/editor/plugins/collision_shape_2d_editor_plugin.cpp index 22ce3e2d6ec..65e7ee0a3d0 100644 --- a/editor/plugins/collision_shape_2d_editor_plugin.cpp +++ b/editor/plugins/collision_shape_2d_editor_plugin.cpp @@ -155,7 +155,8 @@ void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) { Ref world_boundary = node->get_shape(); if (idx == 0) { - world_boundary->set_distance(p_point.length()); + Vector2 normal = world_boundary->get_normal(); + world_boundary->set_distance(p_point.dot(normal) / normal.length_squared()); } else { world_boundary->set_normal(p_point.normalized()); } diff --git a/scene/resources/world_boundary_shape_2d.cpp b/scene/resources/world_boundary_shape_2d.cpp index 35cb8ef13d0..294653659a9 100644 --- a/scene/resources/world_boundary_shape_2d.cpp +++ b/scene/resources/world_boundary_shape_2d.cpp @@ -35,8 +35,8 @@ #include "servers/rendering_server.h" bool WorldBoundaryShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { - Vector2 point = get_distance() * get_normal(); - Vector2 l[2][2] = { { point - get_normal().orthogonal() * 100, point + get_normal().orthogonal() * 100 }, { point, point + get_normal() * 30 } }; + Vector2 point = distance * normal; + Vector2 l[2][2] = { { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 }, { point, point + normal * 30 } }; for (int i = 0; i < 2; i++) { Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, l[i]); @@ -57,11 +57,19 @@ void WorldBoundaryShape2D::_update_shape() { } void WorldBoundaryShape2D::set_normal(const Vector2 &p_normal) { + // Can be non-unit but prevent zero. + ERR_FAIL_COND(p_normal.is_zero_approx()); + if (normal == p_normal) { + return; + } normal = p_normal; _update_shape(); } void WorldBoundaryShape2D::set_distance(real_t p_distance) { + if (distance == p_distance) { + return; + } distance = p_distance; _update_shape(); } @@ -75,20 +83,20 @@ real_t WorldBoundaryShape2D::get_distance() const { } void WorldBoundaryShape2D::draw(const RID &p_to_rid, const Color &p_color) { - Vector2 point = get_distance() * get_normal(); + Vector2 point = distance * normal; real_t line_width = 3.0; - Vector2 l1[2] = { point - get_normal().orthogonal() * 100, point + get_normal().orthogonal() * 100 }; + Vector2 l1[2] = { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 }; RS::get_singleton()->canvas_item_add_line(p_to_rid, l1[0], l1[1], p_color, line_width); - Vector2 l2[2] = { point + get_normal().normalized() * (0.5 * line_width), point + get_normal() * 30 }; + Vector2 l2[2] = { point + normal.normalized() * (0.5 * line_width), point + normal * 30 }; RS::get_singleton()->canvas_item_add_line(p_to_rid, l2[0], l2[1], p_color, line_width); } Rect2 WorldBoundaryShape2D::get_rect() const { - Vector2 point = get_distance() * get_normal(); + Vector2 point = distance * normal; - Vector2 l1[2] = { point - get_normal().orthogonal() * 100, point + get_normal().orthogonal() * 100 }; - Vector2 l2[2] = { point, point + get_normal() * 30 }; + Vector2 l1[2] = { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 }; + Vector2 l2[2] = { point, point + normal * 30 }; Rect2 rect; rect.position = l1[0]; rect.expand_to(l1[1]); @@ -109,7 +117,7 @@ void WorldBoundaryShape2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_distance"), &WorldBoundaryShape2D::get_distance); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "set_normal", "get_normal"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_distance", "get_distance"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01,or_greater,or_less,suffix:px"), "set_distance", "get_distance"); } WorldBoundaryShape2D::WorldBoundaryShape2D() :