浏览代码

Allow setting negative WorldBoundaryShape2D.distance through the editor

kleonc 2 年之前
父节点
当前提交
77b8e0fcb7

+ 0 - 4
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;
 	Vector3 segment = p_dir;
 	real_t den = normal.dot(segment);
 	real_t den = normal.dot(segment);
 
 
-	//printf("den is %i\n",den);
 	if (Math::is_zero_approx(den)) {
 	if (Math::is_zero_approx(den)) {
 		return false;
 		return false;
 	}
 	}
 
 
 	real_t dist = (normal.dot(p_from) - d) / den;
 	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
 	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;
 	Vector3 segment = p_begin - p_end;
 	real_t den = normal.dot(segment);
 	real_t den = normal.dot(segment);
 
 
-	//printf("den is %i\n",den);
 	if (Math::is_zero_approx(den)) {
 	if (Math::is_zero_approx(den)) {
 		return false;
 		return false;
 	}
 	}
 
 
 	real_t dist = (normal.dot(p_begin) - d) / den;
 	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)) {
 	if (dist < (real_t)-CMP_EPSILON || dist > (1.0f + (real_t)CMP_EPSILON)) {
 		return false;
 		return false;

+ 3 - 3
doc/classes/Plane.xml

@@ -138,7 +138,7 @@
 		<method name="normalized" qualifiers="const">
 		<method name="normalized" qualifiers="const">
 			<return type="Plane" />
 			<return type="Plane" />
 			<description>
 			<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>
 			</description>
 		</method>
 		</method>
 		<method name="project" qualifiers="const">
 		<method name="project" qualifiers="const">
@@ -151,11 +151,11 @@
 	</methods>
 	</methods>
 	<members>
 	<members>
 		<member name="d" type="float" setter="" getter="" default="0.0">
 		<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.
 			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>
 		<member name="normal" type="Vector3" setter="" getter="" default="Vector3(0, 0, 0)">
 		<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.
 			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>
 		<member name="x" type="float" setter="" getter="" default="0.0">
 		<member name="x" type="float" setter="" getter="" default="0.0">

+ 3 - 2
doc/classes/WorldBoundaryShape2D.xml

@@ -10,10 +10,11 @@
 	</tutorials>
 	</tutorials>
 	<members>
 	<members>
 		<member name="distance" type="float" setter="set_distance" getter="get_distance" default="0.0">
 		<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>
 		<member name="normal" type="Vector2" setter="set_normal" getter="get_normal" default="Vector2(0, -1)">
 		<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>
 		</member>
 	</members>
 	</members>
 </class>
 </class>

+ 2 - 1
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();
 				Ref<WorldBoundaryShape2D> world_boundary = node->get_shape();
 
 
 				if (idx == 0) {
 				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 {
 				} else {
 					world_boundary->set_normal(p_point.normalized());
 					world_boundary->set_normal(p_point.normalized());
 				}
 				}

+ 17 - 9
scene/resources/world_boundary_shape_2d.cpp

@@ -35,8 +35,8 @@
 #include "servers/rendering_server.h"
 #include "servers/rendering_server.h"
 
 
 bool WorldBoundaryShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
 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++) {
 	for (int i = 0; i < 2; i++) {
 		Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, l[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) {
 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;
 	normal = p_normal;
 	_update_shape();
 	_update_shape();
 }
 }
 
 
 void WorldBoundaryShape2D::set_distance(real_t p_distance) {
 void WorldBoundaryShape2D::set_distance(real_t p_distance) {
+	if (distance == p_distance) {
+		return;
+	}
 	distance = p_distance;
 	distance = p_distance;
 	_update_shape();
 	_update_shape();
 }
 }
@@ -75,20 +83,20 @@ real_t WorldBoundaryShape2D::get_distance() const {
 }
 }
 
 
 void WorldBoundaryShape2D::draw(const RID &p_to_rid, const Color &p_color) {
 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;
 	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);
 	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);
 	RS::get_singleton()->canvas_item_add_line(p_to_rid, l2[0], l2[1], p_color, line_width);
 }
 }
 
 
 Rect2 WorldBoundaryShape2D::get_rect() const {
 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;
 	Rect2 rect;
 	rect.position = l1[0];
 	rect.position = l1[0];
 	rect.expand_to(l1[1]);
 	rect.expand_to(l1[1]);
@@ -109,7 +117,7 @@ void WorldBoundaryShape2D::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("get_distance"), &WorldBoundaryShape2D::get_distance);
 	ClassDB::bind_method(D_METHOD("get_distance"), &WorldBoundaryShape2D::get_distance);
 
 
 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "set_normal", "get_normal");
 	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() :
 WorldBoundaryShape2D::WorldBoundaryShape2D() :