Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More accurate calculation for recover motion in test_body_motion #45643

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions servers/physics_2d/space_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ int Space2DSW::test_body_ray_separation(Body2DSW *p_body, const Transform2D &p_t
CollisionSolver2DSW::CallbackResult cbkres = PhysicsServer2DSW::_shape_col_cbk;

do {
Vector2 recover_motion;
Vector2 recover_min;
Vector2 recover_max;

bool collided = false;

Expand Down Expand Up @@ -639,16 +640,20 @@ int Space2DSW::test_body_ray_separation(Body2DSW *p_body, const Transform2D &p_t
PhysicsServer2D::SeparationResult &result = r_results[ray_index];

for (int k = 0; k < cbk.amount; k++) {
Vector2 a = sr[k * 2 + 0];
Vector2 b = sr[k * 2 + 1];
const Vector2 &a = sr[k * 2 + 0];
const Vector2 &b = sr[k * 2 + 1];
Vector2 recover = b - a;

recover_motion += (b - a) / cbk.amount;
recover_min.x = MIN(recover_min.x, recover.x);
recover_min.y = MIN(recover_min.y, recover.y);
recover_max.x = MAX(recover_max.x, recover.x);
recover_max.y = MAX(recover_max.y, recover.y);

real_t depth = a.distance_to(b);
real_t depth = recover.length();
if (depth > result.collision_depth) {
result.collision_depth = depth;
result.collision_point = b;
result.collision_normal = (b - a).normalized();
result.collision_normal = recover / depth;
result.collision_local_shape = j;
result.collider_shape = shape_idx;
result.collider = col_obj->get_self();
Expand All @@ -667,6 +672,7 @@ int Space2DSW::test_body_ray_separation(Body2DSW *p_body, const Transform2D &p_t
}
}

Vector2 recover_motion = recover_min + recover_max;
if (!collided || recover_motion == Vector2()) {
break;
}
Expand Down Expand Up @@ -844,14 +850,21 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
break;
}

Vector2 recover_motion;
Vector2 recover_min;
Vector2 recover_max;

for (int i = 0; i < cbk.amount; i++) {
Vector2 a = sr[i * 2 + 0];
Vector2 b = sr[i * 2 + 1];
recover_motion += (b - a) / cbk.amount;
const Vector2 &a = sr[i * 2 + 0];
const Vector2 &b = sr[i * 2 + 1];
Vector2 recover = b - a;

recover_min.x = MIN(recover_min.x, recover.x);
recover_min.y = MIN(recover_min.y, recover.y);
recover_max.x = MAX(recover_max.x, recover.x);
recover_max.y = MAX(recover_max.y, recover.y);
}

Vector2 recover_motion = recover_min + recover_max;
if (recover_motion == Vector2()) {
collided = false;
break;
Expand Down
41 changes: 29 additions & 12 deletions servers/physics_3d/space_3d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ int Space3DSW::test_body_ray_separation(Body3DSW *p_body, const Transform &p_tra
CollisionSolver3DSW::CallbackResult cbkres = PhysicsServer3DSW::_shape_col_cbk;

do {
Vector3 recover_motion;
Vector3 recover_min;
Vector3 recover_max;

bool collided = false;

Expand Down Expand Up @@ -646,16 +647,22 @@ int Space3DSW::test_body_ray_separation(Body3DSW *p_body, const Transform &p_tra
PhysicsServer3D::SeparationResult &result = r_results[ray_index];

for (int k = 0; k < cbk.amount; k++) {
Vector3 a = sr[k * 2 + 0];
Vector3 b = sr[k * 2 + 1];

recover_motion += (b - a) / cbk.amount;

real_t depth = a.distance_to(b);
const Vector3 &a = sr[k * 2 + 0];
const Vector3 &b = sr[k * 2 + 1];
Vector3 recover = b - a;

recover_min.x = MIN(recover_min.x, recover.x);
recover_min.y = MIN(recover_min.y, recover.y);
recover_min.z = MIN(recover_min.z, recover.z);
recover_max.x = MAX(recover_max.x, recover.x);
recover_max.y = MAX(recover_max.y, recover.y);
recover_max.z = MAX(recover_max.z, recover.z);

real_t depth = recover.length();
if (depth > result.collision_depth) {
result.collision_depth = depth;
result.collision_point = b;
result.collision_normal = (b - a).normalized();
result.collision_normal = recover / depth;
result.collision_local_shape = j;
result.collider = col_obj->get_self();
result.collider_id = col_obj->get_instance_id();
Expand All @@ -675,6 +682,7 @@ int Space3DSW::test_body_ray_separation(Body3DSW *p_body, const Transform &p_tra
}
}

Vector3 recover_motion = recover_min + recover_max;
if (!collided || recover_motion == Vector3()) {
break;
}
Expand Down Expand Up @@ -786,14 +794,23 @@ bool Space3DSW::test_body_motion(Body3DSW *p_body, const Transform &p_from, cons
break;
}

Vector3 recover_motion;
Vector3 recover_min;
Vector3 recover_max;

for (int i = 0; i < cbk.amount; i++) {
Vector3 a = sr[i * 2 + 0];
Vector3 b = sr[i * 2 + 1];
recover_motion += (b - a) / cbk.amount;
const Vector3 &a = sr[i * 2 + 0];
const Vector3 &b = sr[i * 2 + 1];
Vector3 recover = b - a;

recover_min.x = MIN(recover_min.x, recover.x);
recover_min.y = MIN(recover_min.y, recover.y);
recover_min.z = MIN(recover_min.z, recover.z);
recover_max.x = MAX(recover_max.x, recover.x);
recover_max.y = MAX(recover_max.y, recover.y);
recover_max.z = MAX(recover_max.z, recover.z);
}

Vector3 recover_motion = recover_min + recover_max;
if (recover_motion == Vector3()) {
collided = false;
break;
Expand Down