Skip to content

Commit

Permalink
(buggy) fix for is_on_floor flickering
Browse files Browse the repository at this point in the history
  • Loading branch information
theteleforce committed Feb 6, 2020
1 parent dc9774b commit b92325a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion servers/physics_2d/physics_2d_server_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,14 @@ void Physics2DServerSW::_shape_col_cbk(const Vector2 &p_point_A, const Vector2 &
if (cbk->max == 0)
return;

Vector2 rel_dir = (p_point_A - p_point_B).normalized();
cbk->best_normal = -rel_dir; // negate for consistency with _RestCallbackData2D.best_normal

if (cbk->valid_dir != Vector2()) {
if (p_point_A.distance_squared_to(p_point_B) > cbk->valid_depth * cbk->valid_depth) {
cbk->invalid_by_dir++;
return;
}
Vector2 rel_dir = (p_point_A - p_point_B).normalized();

if (cbk->valid_dir.dot(rel_dir) < Math_SQRT12) { //sqrt(2)/2.0 - 45 degrees
cbk->invalid_by_dir++;
Expand Down
1 change: 1 addition & 0 deletions servers/physics_2d/physics_2d_server_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Physics2DServerSW : public Physics2DServer {
int amount;
int passed;
int invalid_by_dir;
Vector2 best_normal;
Vector2 *ptr;
};

Expand Down
14 changes: 12 additions & 2 deletions servers/physics_2d/space_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
ExcludedShapeSW excluded_shape_pairs[max_excluded_shape_pairs];
int excluded_shape_pair_count = 0;

float separation_margin = MIN(p_margin, MAX(0.0, p_motion.length() - CMP_EPSILON)); //don't separate by more than the intended motion
float pmotion_len = p_motion.length(); // we'll use this more than once, so precompute it
float separation_margin = MIN(p_margin, MAX(0.0, pmotion_len - CMP_EPSILON)); //don't separate by more than the intended motion

Transform2D body_transform = p_from;

Expand Down Expand Up @@ -816,10 +817,19 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co

int current_passed = cbk.passed; //save how many points passed collision
bool did_collide = false;
bool motion_is_parallel = false;

Shape2DSW *against_shape = col_obj->get_shape(shape_idx);
if (CollisionSolver2DSW::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), cbkres, cbkptr, NULL, separation_margin)) {
did_collide = cbk.passed > current_passed; //more passed, so collision actually existed
motion_is_parallel = (p_motion != Vector2()) && (fabsf(cbk.best_normal.dot(p_motion)) < (CMP_EPSILON * pmotion_len)); // multiplying by pmotion_len on the right is faster than normalizing p_motion on the left
if (cbk.passed > current_passed) { // more passed, so collision actually existed
if (!motion_is_parallel) { // if a shape is moving horizontally across a floor or vertically against a wall, don't knock them out of the floor or wall
did_collide = true;
} else {
cbk.amount--; // if the collision was parallel to motion, don't consider it when applying recover_motion
}
}
//did_collide = (cbk.passed > current_passed) && !motion_is_perpendicular; //more passed, so collision actually existed, and motion isn't perpendicular to the other shape
}

if (!did_collide && cbk.invalid_by_dir > 0) {
Expand Down

0 comments on commit b92325a

Please sign in to comment.