diff options
Diffstat (limited to 'scene/2d/physics_body_2d.cpp')
-rw-r--r-- | scene/2d/physics_body_2d.cpp | 253 |
1 files changed, 100 insertions, 153 deletions
diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index 2d3feb30a1..fa19d5c2cf 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -39,8 +39,8 @@ #include "scene/scene_string_names.h" void PhysicsBody2D::_bind_methods() { - ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "infinite_inertia", "exclude_raycast_shapes", "test_only", "safe_margin"), &PhysicsBody2D::_move, DEFVAL(true), DEFVAL(true), DEFVAL(false), DEFVAL(0.08)); - ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "infinite_inertia", "exclude_raycast_shapes", "collision", "safe_margin"), &PhysicsBody2D::test_move, DEFVAL(true), DEFVAL(true), DEFVAL(Variant()), DEFVAL(0.08)); + ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "test_only", "safe_margin"), &PhysicsBody2D::_move, DEFVAL(false), DEFVAL(0.08)); + ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "collision", "safe_margin"), &PhysicsBody2D::test_move, DEFVAL(Variant()), DEFVAL(0.08)); ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions); ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with); @@ -59,10 +59,10 @@ PhysicsBody2D::~PhysicsBody2D() { } } -Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, bool p_test_only, real_t p_margin) { +Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_test_only, real_t p_margin) { PhysicsServer2D::MotionResult result; - if (move_and_collide(p_motion, p_infinite_inertia, result, p_margin, p_exclude_raycast_shapes, p_test_only)) { + if (move_and_collide(p_motion, result, p_margin, p_test_only)) { if (motion_cache.is_null()) { motion_cache.instantiate(); motion_cache->owner = this; @@ -75,12 +75,12 @@ Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_i return Ref<KinematicCollision2D>(); } -bool PhysicsBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_inertia, PhysicsServer2D::MotionResult &r_result, real_t p_margin, bool p_exclude_raycast_shapes, bool p_test_only, bool p_cancel_sliding, const Set<RID> &p_exclude) { +bool PhysicsBody2D::move_and_collide(const Vector2 &p_motion, PhysicsServer2D::MotionResult &r_result, real_t p_margin, bool p_test_only, bool p_cancel_sliding, const Set<RID> &p_exclude) { if (is_only_update_transform_changes_enabled()) { ERR_PRINT("Move functions do not work together with 'sync to physics' option. Please read the documentation."); } Transform2D gt = get_global_transform(); - bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_infinite_inertia, p_margin, &r_result, p_exclude_raycast_shapes, p_exclude); + bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_margin, &r_result, p_exclude); // Restore direction of motion to be along original motion, // in order to avoid sliding due to recovery, @@ -128,7 +128,7 @@ bool PhysicsBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_in return colliding; } -bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, const Ref<KinematicCollision2D> &r_collision, real_t p_margin) { +bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, const Ref<KinematicCollision2D> &r_collision, real_t p_margin) { ERR_FAIL_COND_V(!is_inside_tree(), false); PhysicsServer2D::MotionResult *r = nullptr; @@ -137,7 +137,7 @@ bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion r = const_cast<PhysicsServer2D::MotionResult *>(&r_collision->result); } - return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), p_from, p_motion, p_infinite_inertia, p_margin, r, p_exclude_raycast_shapes); + return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), p_from, p_motion, p_margin, r); } TypedArray<PhysicsBody2D> PhysicsBody2D::get_collision_exceptions() { @@ -1080,7 +1080,7 @@ void CharacterBody2D::move_and_slide() { PhysicsServer2D::MotionResult floor_result; Set<RID> exclude; exclude.insert(platform_rid); - if (move_and_collide(current_platform_velocity * delta, infinite_inertia, floor_result, true, false, false, false, exclude)) { + if (move_and_collide(current_platform_velocity * delta, floor_result, margin, false, false, exclude)) { motion_results.push_back(floor_result); _set_collision_direction(floor_result); } @@ -1113,123 +1113,113 @@ void CharacterBody2D::move_and_slide() { Vector2 prev_position = get_global_transform().elements[2]; - for (int i = 0; i < 2; ++i) { - bool collided; - if (i == 0) { // Collide. - collided = move_and_collide(motion, infinite_inertia, result, margin, true, false, !sliding_enabled); - } else { // Separate raycasts (if any). - collided = separate_raycast_shapes(result); - if (collided) { - result.remainder = motion; // Keep. - result.motion = Vector2(); + bool collided = move_and_collide(motion, result, margin, false, !sliding_enabled); + + if (collided) { + found_collision = true; + motion_results.push_back(result); + _set_collision_direction(result); + + if (on_floor && stop_on_slope && (linear_velocity.normalized() + up_direction).length() < 0.01) { + Transform2D gt = get_global_transform(); + if (result.motion.length() > margin) { + gt.elements[2] -= result.motion.slide(up_direction); + } else { + gt.elements[2] -= result.motion; } + set_global_transform(gt); + linear_velocity = Vector2(); + motion = Vector2(); + break; } - if (collided) { - found_collision = true; - motion_results.push_back(result); - _set_collision_direction(result); + if (result.remainder.is_equal_approx(Vector2())) { + motion = Vector2(); + break; + } - if (on_floor && stop_on_slope && (linear_velocity.normalized() + up_direction).length() < 0.01) { - Transform2D gt = get_global_transform(); - if (result.motion.length() > margin) { - gt.elements[2] -= result.motion.slide(up_direction); - } else { + // Move on floor only checks. + if (move_on_floor_only && on_wall && motion_slide_up.dot(result.collision_normal) <= 0) { + // Avoid to move forward on a wall if move_on_floor_only is true. + if (was_on_floor && !is_on_floor_only() && !vel_dir_facing_up) { + // If the movement is large the body can be prevented from reaching the walls. + if (result.motion.length() <= margin) { + // Cancels the motion. + Transform2D gt = get_global_transform(); gt.elements[2] -= result.motion; + set_global_transform(gt); } - set_global_transform(gt); + on_floor = true; + platform_rid = prev_platform_rid; + platform_layer = prev_platform_layer; + + platform_velocity = prev_platform_velocity; + floor_normal = prev_floor_normal; linear_velocity = Vector2(); motion = Vector2(); break; } - - if (result.remainder.is_equal_approx(Vector2())) { - motion = Vector2(); - break; + // Prevents the body from being able to climb a slope when it moves forward against the wall. + else if (!is_on_floor_only()) { + motion = up_direction * up_direction.dot(result.remainder); + motion = motion.slide(result.collision_normal); + } else { + motion = result.remainder; } - - // Move on floor only checks. - if (move_on_floor_only && on_wall && motion_slide_up.dot(result.collision_normal) <= 0) { - // Avoid to move forward on a wall if move_on_floor_only is true. - if (was_on_floor && !is_on_floor_only() && !vel_dir_facing_up) { - // If the movement is large the body can be prevented from reaching the walls. - if (result.motion.length() <= margin) { - // Cancels the motion. - Transform2D gt = get_global_transform(); - gt.elements[2] -= result.motion; - set_global_transform(gt); - } - on_floor = true; - platform_rid = prev_platform_rid; - platform_layer = prev_platform_layer; - - platform_velocity = prev_platform_velocity; - floor_normal = prev_floor_normal; - linear_velocity = Vector2(); - motion = Vector2(); - break; - } - // Prevents the body from being able to climb a slope when it moves forward against the wall. - else if (!is_on_floor_only()) { - motion = up_direction * up_direction.dot(result.remainder); - motion = motion.slide(result.collision_normal); - } else { - motion = result.remainder; - } + } + // Constant Speed when the slope is upward. + else if (constant_speed_on_floor && is_on_floor_only() && can_apply_constant_speed && was_on_floor && motion.dot(result.collision_normal) < 0) { + can_apply_constant_speed = false; + Vector2 motion_slide_norm = result.remainder.slide(result.collision_normal).normalized(); + if (!motion_slide_norm.is_equal_approx(Vector2())) { + motion = motion_slide_norm * (motion_slide_up.length() - result.motion.slide(up_direction).length() - last_travel.slide(up_direction).length()); } - // Constant Speed when the slope is upward. - else if (constant_speed_on_floor && is_on_floor_only() && can_apply_constant_speed && was_on_floor && motion.dot(result.collision_normal) < 0) { - can_apply_constant_speed = false; - Vector2 motion_slide_norm = result.remainder.slide(result.collision_normal).normalized(); - if (!motion_slide_norm.is_equal_approx(Vector2())) { - motion = motion_slide_norm * (motion_slide_up.length() - result.motion.slide(up_direction).length() - last_travel.slide(up_direction).length()); - } + } + // Regular sliding, the last part of the test handle the case when you don't want to slide on the ceiling. + else if ((sliding_enabled || !on_floor) && (!on_ceiling || slide_on_ceiling || !vel_dir_facing_up)) { + Vector2 slide_motion = result.remainder.slide(result.collision_normal); + if (slide_motion.dot(linear_velocity) > 0.0) { + motion = slide_motion; + } else { + motion = Vector2(); } - // Regular sliding, the last part of the test handle the case when you don't want to slide on the ceiling. - else if ((sliding_enabled || !on_floor) && (!on_ceiling || slide_on_ceiling || !vel_dir_facing_up)) { - Vector2 slide_motion = result.remainder.slide(result.collision_normal); - if (slide_motion.dot(linear_velocity) > 0.0) { - motion = slide_motion; + if (slide_on_ceiling && on_ceiling) { + // Apply slide only in the direction of the input motion, otherwise just stop to avoid jittering when moving against a wall. + if (vel_dir_facing_up) { + linear_velocity = linear_velocity.slide(result.collision_normal); } else { - motion = Vector2(); - } - if (slide_on_ceiling && on_ceiling) { - // Apply slide only in the direction of the input motion, otherwise just stop to avoid jittering when moving against a wall. - if (vel_dir_facing_up) { - linear_velocity = linear_velocity.slide(result.collision_normal); - } else { - // Avoid acceleration in slope when falling. - linear_velocity = up_direction * up_direction.dot(linear_velocity); - } + // Avoid acceleration in slope when falling. + linear_velocity = up_direction * up_direction.dot(linear_velocity); } } - // No sliding on first attempt to keep floor motion stable when possible. - else { - motion = result.remainder; - if (on_ceiling && !slide_on_ceiling && vel_dir_facing_up) { - linear_velocity = linear_velocity.slide(up_direction); - motion = motion.slide(up_direction); - } + } + // No sliding on first attempt to keep floor motion stable when possible. + else { + motion = result.remainder; + if (on_ceiling && !slide_on_ceiling && vel_dir_facing_up) { + linear_velocity = linear_velocity.slide(up_direction); + motion = motion.slide(up_direction); } - - last_travel = result.motion; } - // When you move forward in a downward slope you don’t collide because you will be in the air. - // This test ensures that constant speed is applied, only if the player is still on the ground after the snap is applied. - else if (i == 0 && constant_speed_on_floor && first_slide && _on_floor_if_snapped(was_on_floor, vel_dir_facing_up)) { - can_apply_constant_speed = false; - sliding_enabled = true; - Transform2D gt = get_global_transform(); - gt.elements[2] = prev_position; - set_global_transform(gt); - Vector2 motion_slide_norm = motion.slide(prev_floor_normal).normalized(); - if (!motion_slide_norm.is_equal_approx(Vector2())) { - motion = motion_slide_norm * (motion_slide_up.length()); - found_collision = true; - } + last_travel = result.motion; + } + // When you move forward in a downward slope you don’t collide because you will be in the air. + // This test ensures that constant speed is applied, only if the player is still on the ground after the snap is applied. + else if (constant_speed_on_floor && first_slide && _on_floor_if_snapped(was_on_floor, vel_dir_facing_up)) { + can_apply_constant_speed = false; + sliding_enabled = true; + Transform2D gt = get_global_transform(); + gt.elements[2] = prev_position; + set_global_transform(gt); + + Vector2 motion_slide_norm = motion.slide(prev_floor_normal).normalized(); + if (!motion_slide_norm.is_equal_approx(Vector2())) { + motion = motion_slide_norm * (motion_slide_up.length()); + found_collision = true; } } + can_apply_constant_speed = !can_apply_constant_speed && !sliding_enabled; sliding_enabled = true; first_slide = false; @@ -1259,7 +1249,7 @@ void CharacterBody2D::_snap_on_floor(bool was_on_floor, bool vel_dir_facing_up) Transform2D gt = get_global_transform(); PhysicsServer2D::MotionResult result; - if (move_and_collide(up_direction * -floor_snap_length, infinite_inertia, result, margin, false, true, false)) { + if (move_and_collide(up_direction * -floor_snap_length, result, margin, true, false)) { bool apply = true; float collision_angle = Math::acos(result.collision_normal.dot(up_direction)); if (collision_angle <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { @@ -1294,7 +1284,7 @@ bool CharacterBody2D::_on_floor_if_snapped(bool was_on_floor, bool vel_dir_facin } PhysicsServer2D::MotionResult result; - if (move_and_collide(up_direction * -floor_snap_length, infinite_inertia, result, margin, false, true, false)) { + if (move_and_collide(up_direction * -floor_snap_length, result, margin, true, false)) { if (Math::acos(result.collision_normal.dot(up_direction)) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { return true; } @@ -1331,42 +1321,6 @@ void CharacterBody2D::_set_platform_data(const PhysicsServer2D::MotionResult &p_ } } -bool CharacterBody2D::separate_raycast_shapes(PhysicsServer2D::MotionResult &r_result) { - PhysicsServer2D::SeparationResult sep_res[8]; //max 8 rays - - Transform2D gt = get_global_transform(); - - Vector2 recover; - int hits = PhysicsServer2D::get_singleton()->body_test_ray_separation(get_rid(), gt, infinite_inertia, recover, sep_res, 8, margin); - int deepest = -1; - real_t deepest_depth; - for (int i = 0; i < hits; i++) { - if (deepest == -1 || sep_res[i].collision_depth > deepest_depth) { - deepest = i; - deepest_depth = sep_res[i].collision_depth; - } - } - - gt.elements[2] += recover; - set_global_transform(gt); - - if (deepest != -1) { - r_result.collider_id = sep_res[deepest].collider_id; - r_result.collider_metadata = sep_res[deepest].collider_metadata; - r_result.collider_shape = sep_res[deepest].collider_shape; - r_result.collider_velocity = sep_res[deepest].collider_velocity; - r_result.collision_point = sep_res[deepest].collision_point; - r_result.collision_normal = sep_res[deepest].collision_normal; - r_result.collision_local_shape = sep_res[deepest].collision_local_shape; - r_result.motion = recover; - r_result.remainder = Vector2(); - - return true; - } else { - return false; - } -} - const Vector2 &CharacterBody2D::get_linear_velocity() const { return linear_velocity; } @@ -1447,16 +1401,10 @@ void CharacterBody2D::set_stop_on_slope_enabled(bool p_enabled) { stop_on_slope = p_enabled; } -bool CharacterBody2D::is_infinite_inertia_enabled() const { - return infinite_inertia; -} -void CharacterBody2D::set_infinite_inertia_enabled(bool p_enabled) { - infinite_inertia = p_enabled; -} - bool CharacterBody2D::is_constant_speed_on_floor_enabled() const { return constant_speed_on_floor; } + void CharacterBody2D::set_constant_speed_on_floor_enabled(bool p_enabled) { constant_speed_on_floor = p_enabled; } @@ -1464,6 +1412,7 @@ void CharacterBody2D::set_constant_speed_on_floor_enabled(bool p_enabled) { bool CharacterBody2D::is_move_on_floor_only_enabled() const { return move_on_floor_only; } + void CharacterBody2D::set_move_on_floor_only_enabled(bool p_enabled) { move_on_floor_only = p_enabled; } @@ -1471,6 +1420,7 @@ void CharacterBody2D::set_move_on_floor_only_enabled(bool p_enabled) { bool CharacterBody2D::is_slide_on_ceiling_enabled() const { return slide_on_ceiling; } + void CharacterBody2D::set_slide_on_ceiling_enabled(bool p_enabled) { slide_on_ceiling = p_enabled; } @@ -1541,7 +1491,6 @@ void CharacterBody2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_safe_margin"), &CharacterBody2D::get_safe_margin); ClassDB::bind_method(D_METHOD("is_stop_on_slope_enabled"), &CharacterBody2D::is_stop_on_slope_enabled); ClassDB::bind_method(D_METHOD("set_stop_on_slope_enabled", "enabled"), &CharacterBody2D::set_stop_on_slope_enabled); - ClassDB::bind_method(D_METHOD("is_infinite_inertia_enabled"), &CharacterBody2D::is_infinite_inertia_enabled); ClassDB::bind_method(D_METHOD("set_constant_speed_on_floor_enabled", "enabled"), &CharacterBody2D::set_constant_speed_on_floor_enabled); ClassDB::bind_method(D_METHOD("is_constant_speed_on_floor_enabled"), &CharacterBody2D::is_constant_speed_on_floor_enabled); ClassDB::bind_method(D_METHOD("set_move_on_floor_only_enabled", "enabled"), &CharacterBody2D::set_move_on_floor_only_enabled); @@ -1552,7 +1501,6 @@ void CharacterBody2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_exclude_body_layers", "exclude_layer"), &CharacterBody2D::set_exclude_body_layers); ClassDB::bind_method(D_METHOD("get_exclude_body_layers"), &CharacterBody2D::get_exclude_body_layers); - ClassDB::bind_method(D_METHOD("set_infinite_inertia_enabled", "enabled"), &CharacterBody2D::set_infinite_inertia_enabled); ClassDB::bind_method(D_METHOD("get_max_slides"), &CharacterBody2D::get_max_slides); ClassDB::bind_method(D_METHOD("set_max_slides", "max_slides"), &CharacterBody2D::set_max_slides); ClassDB::bind_method(D_METHOD("get_floor_max_angle"), &CharacterBody2D::get_floor_max_angle); @@ -1575,7 +1523,6 @@ void CharacterBody2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity"), "set_linear_velocity", "get_linear_velocity"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stop_on_slope"), "set_stop_on_slope_enabled", "is_stop_on_slope_enabled"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "infinite_inertia"), "set_infinite_inertia_enabled", "is_infinite_inertia_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "constant_speed_on_floor"), "set_constant_speed_on_floor_enabled", "is_constant_speed_on_floor_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "move_on_floor_only"), "set_move_on_floor_only_enabled", "is_move_on_floor_only_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slide_on_ceiling"), "set_slide_on_ceiling_enabled", "is_slide_on_ceiling_enabled"); |