diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-06-14 18:49:38 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-06-14 18:49:38 +0200 |
commit | eeb7f48203872b20bf0c588333ac851fa3fdca73 (patch) | |
tree | 431c7cad0738d660793e0f4a703c7fe56dd24621 | |
parent | 01453ab7f3e6940ecd611d63d675a484b2f55cb4 (diff) | |
parent | 77b8e0fcb7cfcdabed7e7217caedf69c049c7a3e (diff) | |
download | redot-engine-eeb7f48203872b20bf0c588333ac851fa3fdca73.tar.gz |
Merge pull request #77236 from kleonc/world_boundary_2d_allow_negative_distance
Allow setting negative `WorldBoundaryShape2D.distance` through the editor
-rw-r--r-- | core/math/plane.cpp | 4 | ||||
-rw-r--r-- | doc/classes/Plane.xml | 6 | ||||
-rw-r--r-- | doc/classes/WorldBoundaryShape2D.xml | 5 | ||||
-rw-r--r-- | editor/plugins/collision_shape_2d_editor_plugin.cpp | 3 | ||||
-rw-r--r-- | 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 99694d7871..6b9bcea081 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 fa29244c2a..0b147934d4 100644 --- a/doc/classes/Plane.xml +++ b/doc/classes/Plane.xml @@ -138,7 +138,7 @@ <method name="normalized" qualifiers="const"> <return type="Plane" /> <description> - 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). </description> </method> <method name="project" qualifiers="const"> @@ -151,11 +151,11 @@ </methods> <members> <member name="d" type="float" setter="" getter="" default="0.0"> - 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. </member> <member name="normal" type="Vector3" setter="" getter="" default="Vector3(0, 0, 0)"> - 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. </member> <member name="x" type="float" setter="" getter="" default="0.0"> diff --git a/doc/classes/WorldBoundaryShape2D.xml b/doc/classes/WorldBoundaryShape2D.xml index 4ee4b2551c..9de10a531f 100644 --- a/doc/classes/WorldBoundaryShape2D.xml +++ b/doc/classes/WorldBoundaryShape2D.xml @@ -10,10 +10,11 @@ </tutorials> <members> <member name="distance" type="float" setter="set_distance" getter="get_distance" default="0.0"> - 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. </member> <member name="normal" type="Vector2" setter="set_normal" getter="get_normal" default="Vector2(0, -1)"> - 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]. </member> </members> </class> diff --git a/editor/plugins/collision_shape_2d_editor_plugin.cpp b/editor/plugins/collision_shape_2d_editor_plugin.cpp index 22ce3e2d6e..65e7ee0a3d 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<WorldBoundaryShape2D> 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 35cb8ef13d..294653659a 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() : |