summaryrefslogtreecommitdiff
path: root/scene/2d
diff options
context:
space:
mode:
authorPouleyKetchoupp <pouleyketchoup@gmail.com>2021-11-10 11:20:02 -0700
committerPouleyKetchoupp <pouleyketchoup@gmail.com>2021-11-10 11:20:02 -0700
commit5da057adaf2127deaf1455641ccbe024f29f1ebd (patch)
tree5ecdf3db3afde0f1bab7d902c0b82d629abb6158 /scene/2d
parente8870ddefc36f6e04dc6212ab5ca1d6391739539 (diff)
Fix test_move reporting collision when touching another body
Reporting rest collision information is needed for move_and_collide and move_and_slide so floor detection can be done properly, but in the case of just testing the motion for collision, it makes sense to return false if the body is able to move all along the path without being stopped. Updated the logic in test_move and clarified the documentation for test_move and move_and_collide.
Diffstat (limited to 'scene/2d')
-rw-r--r--scene/2d/physics_body_2d.cpp12
1 files changed, 11 insertions, 1 deletions
diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp
index a43d498a62..2c17515a9e 100644
--- a/scene/2d/physics_body_2d.cpp
+++ b/scene/2d/physics_body_2d.cpp
@@ -133,9 +133,12 @@ bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_linear
ERR_FAIL_COND_V(!is_inside_tree(), false);
PhysicsServer2D::MotionResult *r = nullptr;
+ PhysicsServer2D::MotionResult temp_result;
if (r_collision.is_valid()) {
// Needs const_cast because method bindings don't support non-const Ref.
r = const_cast<PhysicsServer2D::MotionResult *>(&r_collision->result);
+ } else {
+ r = &temp_result;
}
// Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky.
@@ -143,7 +146,14 @@ bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_linear
PhysicsServer2D::MotionParameters parameters(p_from, p_linear_velocity * delta, p_margin);
- return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), parameters, r);
+ bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), parameters, r);
+
+ if (colliding) {
+ // Don't report collision when the whole motion is done.
+ return (r->collision_safe_fraction < 1.0);
+ } else {
+ return false;
+ }
}
TypedArray<PhysicsBody2D> PhysicsBody2D::get_collision_exceptions() {