Path: blob/master/scene/2d/physics/physics_body_2d.cpp
9906 views
/**************************************************************************/1/* physics_body_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "physics_body_2d.h"3132void PhysicsBody2D::_bind_methods() {33ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::_move, DEFVAL(false), DEFVAL(0.08), DEFVAL(false));34ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::test_move, DEFVAL(Variant()), DEFVAL(0.08), DEFVAL(false));35ClassDB::bind_method(D_METHOD("get_gravity"), &PhysicsBody2D::get_gravity);3637ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions);38ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with);39ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody2D::remove_collision_exception_with);40}4142PhysicsBody2D::PhysicsBody2D(PhysicsServer2D::BodyMode p_mode) :43CollisionObject2D(PhysicsServer2D::get_singleton()->body_create(), false) {44set_body_mode(p_mode);45set_pickable(false);46}4748Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_test_only, real_t p_margin, bool p_recovery_as_collision) {49PhysicsServer2D::MotionParameters parameters(get_global_transform(), p_motion, p_margin);50parameters.recovery_as_collision = p_recovery_as_collision;5152PhysicsServer2D::MotionResult result;5354if (move_and_collide(parameters, result, p_test_only)) {55// Create a new instance when the cached reference is invalid or still in use in script.56if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {57motion_cache.instantiate();58motion_cache->owner_id = get_instance_id();59}6061motion_cache->result = result;62return motion_cache;63}6465return Ref<KinematicCollision2D>();66}6768bool PhysicsBody2D::move_and_collide(const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {69if (is_only_update_transform_changes_enabled()) {70ERR_PRINT("Move functions do not work together with 'sync to physics' option. See the documentation for details.");71}7273bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);7475// Restore direction of motion to be along original motion,76// in order to avoid sliding due to recovery,77// but only if collision depth is low enough to avoid tunneling.78if (p_cancel_sliding) {79real_t motion_length = p_parameters.motion.length();80real_t precision = 0.001;8182if (colliding) {83// Can't just use margin as a threshold because collision depth is calculated on unsafe motion,84// so even in normal resting cases the depth can be a bit more than the margin.85precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction);8687if (r_result.collision_depth > p_parameters.margin + precision) {88p_cancel_sliding = false;89}90}9192if (p_cancel_sliding) {93// When motion is null, recovery is the resulting motion.94Vector2 motion_normal;95if (motion_length > CMP_EPSILON) {96motion_normal = p_parameters.motion / motion_length;97}9899// Check depth of recovery.100real_t projected_length = r_result.travel.dot(motion_normal);101Vector2 recovery = r_result.travel - motion_normal * projected_length;102real_t recovery_length = recovery.length();103// Fixes cases where canceling slide causes the motion to go too deep into the ground,104// because we're only taking rest information into account and not general recovery.105if (recovery_length < p_parameters.margin + precision) {106// Apply adjustment to motion.107r_result.travel = motion_normal * projected_length;108r_result.remainder = p_parameters.motion - r_result.travel;109}110}111}112113if (!p_test_only) {114Transform2D gt = p_parameters.from;115gt.columns[2] += r_result.travel;116set_global_transform(gt);117}118119return colliding;120}121122bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, const Ref<KinematicCollision2D> &r_collision, real_t p_margin, bool p_recovery_as_collision) {123ERR_FAIL_COND_V(!is_inside_tree(), false);124125PhysicsServer2D::MotionResult *r = nullptr;126PhysicsServer2D::MotionResult temp_result;127if (r_collision.is_valid()) {128r = &r_collision->result;129} else {130r = &temp_result;131}132133PhysicsServer2D::MotionParameters parameters(p_from, p_motion, p_margin);134parameters.recovery_as_collision = p_recovery_as_collision;135136return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), parameters, r);137}138139Vector2 PhysicsBody2D::get_gravity() const {140PhysicsDirectBodyState2D *state = PhysicsServer2D::get_singleton()->body_get_direct_state(get_rid());141ERR_FAIL_NULL_V(state, Vector2());142return state->get_total_gravity();143}144145TypedArray<PhysicsBody2D> PhysicsBody2D::get_collision_exceptions() {146List<RID> exceptions;147PhysicsServer2D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);148Array ret;149for (const RID &body : exceptions) {150ObjectID instance_id = PhysicsServer2D::get_singleton()->body_get_object_instance_id(body);151Object *obj = ObjectDB::get_instance(instance_id);152PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);153ret.append(physics_body);154}155return ret;156}157158void PhysicsBody2D::add_collision_exception_with(Node *p_node) {159ERR_FAIL_NULL(p_node);160PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);161ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");162PhysicsServer2D::get_singleton()->body_add_collision_exception(get_rid(), physics_body->get_rid());163}164165void PhysicsBody2D::remove_collision_exception_with(Node *p_node) {166ERR_FAIL_NULL(p_node);167PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);168ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");169PhysicsServer2D::get_singleton()->body_remove_collision_exception(get_rid(), physics_body->get_rid());170}171172PackedStringArray PhysicsBody2D::get_configuration_warnings() const {173PackedStringArray warnings = CollisionObject2D::get_configuration_warnings();174175if (SceneTree::is_fti_enabled_in_project() && !is_physics_interpolated()) {176warnings.push_back(RTR("PhysicsBody2D will not work correctly on a non-interpolated branch of the SceneTree.\nCheck the node's inherited physics_interpolation_mode."));177}178179return warnings;180}181182183