Skip to content

Commit

Permalink
Fix wrong collision reported on move_and_collide
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriceci committed Mar 23, 2022
1 parent 788abaf commit 65b3200
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,10 @@ void RigidBody2D::_reload_physics_characteristics() {
Ref<KinematicCollision2D> KinematicBody2D::_move(const Vector2 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, bool p_test_only) {
Collision col;

if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only)) {
bool collided = move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only);

// Don't report collision when the whole motion is done.
if (collided && col.collision_safe_fraction < 1) {
// Create a new instance when the cached reference is invalid or still in use in script.
if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) {
motion_cache.instance();
Expand Down Expand Up @@ -1101,6 +1104,7 @@ bool KinematicBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_
r_collision.travel = result.motion;
r_collision.remainder = result.remainder;
r_collision.local_shape = result.collision_local_shape;
r_collision.collision_safe_fraction = result.collision_safe_fraction;
}

if (!p_test_only) {
Expand Down
1 change: 1 addition & 0 deletions scene/2d/physics_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class KinematicBody2D : public PhysicsBody2D {
Vector2 remainder;
Vector2 travel;
int local_shape;
real_t collision_safe_fraction;

real_t get_angle(const Vector2 &p_up_direction) const {
return Math::acos(normal.dot(p_up_direction));
Expand Down
7 changes: 6 additions & 1 deletion scene/3d/physics_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,11 @@ void RigidBody::_reload_physics_characteristics() {

Ref<KinematicCollision> KinematicBody::_move(const Vector3 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, bool p_test_only) {
Collision col;
if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only)) {

bool collided = move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only);

// Don't report collision when the whole motion is done.
if (collided && col.collision_safe_fraction < 1) {
// Create a new instance when the cached reference is invalid or still in use in script.
if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) {
motion_cache.instance();
Expand Down Expand Up @@ -1032,6 +1036,7 @@ bool KinematicBody::move_and_collide(const Vector3 &p_motion, bool p_infinite_in
r_collision.travel = result.motion;
r_collision.remainder = result.remainder;
r_collision.local_shape = result.collision_local_shape;
r_collision.collision_safe_fraction = result.collision_safe_fraction;
}

for (int i = 0; i < 3; i++) {
Expand Down
1 change: 1 addition & 0 deletions scene/3d/physics_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ class KinematicBody : public PhysicsBody {
Vector3 remainder;
Vector3 travel;
int local_shape;
real_t collision_safe_fraction;

real_t get_angle(const Vector3 &p_up_direction) const {
return Math::acos(normal.dot(p_up_direction));
Expand Down

0 comments on commit 65b3200

Please sign in to comment.