Debian Codegraph @

scene/3d/physics/physics_body_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* physics_body_3d.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "physics_body_3d.h"
32
33 void PhysicsBody3D::_bind_methods() {
34 ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::_move, DEFVAL(false), DEFVAL(0.001), DEFVAL(false), DEFVAL(1));
35 ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::test_move, DEFVAL(Variant()), DEFVAL(0.001), DEFVAL(false), DEFVAL(1));
36 ClassDB::bind_method(D_METHOD("get_gravity"), &PhysicsBody3D::get_gravity);
37
38 ClassDB::bind_method(D_METHOD("set_axis_lock", "axis", "lock"), &PhysicsBody3D::set_axis_lock);
39 ClassDB::bind_method(D_METHOD("get_axis_lock", "axis"), &PhysicsBody3D::get_axis_lock);
40
41 ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody3D::get_collision_exceptions);
42 ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody3D::add_collision_exception_with);
43 ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody3D::remove_collision_exception_with);
44
45 ADD_GROUP("Axis Lock", "axis_lock_");
46 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_x"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_X);
47 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_y"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_Y);
48 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_z"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_Z);
49 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_x"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_X);
50 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_y"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_Y);
51 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_z"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_Z);
52 }
53
54 PhysicsBody3D::PhysicsBody3D(PhysicsServer3D::BodyMode p_mode) :
55 CollisionObject3D(PhysicsServer3D::get_singleton()->body_create(), false) {
56 _define_ancestry(AncestralClass::PHYSICS_BODY_3D);
57 set_body_mode(p_mode);
58 }
59
60 TypedArray<PhysicsBody3D> PhysicsBody3D::get_collision_exceptions() {
61 List<RID> exceptions;
62 PhysicsServer3D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
63 Array ret;
64 for (const RID &body : exceptions) {
65 ObjectID instance_id = PhysicsServer3D::get_singleton()->body_get_object_instance_id(body);
66 Object *obj = ObjectDB::get_instance(instance_id);
67 PhysicsBody3D *physics_body = Object::cast_to<PhysicsBody3D>(obj);
68 ret.append(physics_body);
69 }
70 return ret;
71 }
72
73 void PhysicsBody3D::add_collision_exception_with(RequiredParam<Node> rp_node) {
74 EXTRACT_PARAM_OR_FAIL(p_node, rp_node);
75 CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
76 ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D).");
77 PhysicsServer3D::get_singleton()->body_add_collision_exception(get_rid(), collision_object->get_rid());
78 }
79
80 void PhysicsBody3D::remove_collision_exception_with(RequiredParam<Node> rp_node) {
81 EXTRACT_PARAM_OR_FAIL(p_node, rp_node);
82 CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
83 ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D).");
84 PhysicsServer3D::get_singleton()->body_remove_collision_exception(get_rid(), collision_object->get_rid());
85 }
86
87 Ref<KinematicCollision3D> PhysicsBody3D::_move(const Vector3 &p_motion, bool p_test_only, real_t p_margin, bool p_recovery_as_collision, int p_max_collisions) {
88 PhysicsServer3D::MotionParameters parameters(get_global_transform(), p_motion, p_margin);
89 parameters.max_collisions = p_max_collisions;
90 parameters.recovery_as_collision = p_recovery_as_collision;
91
92 PhysicsServer3D::MotionResult result;
93
94 if (move_and_collide(parameters, result, p_test_only)) {
95 // Create a new instance when the cached reference is invalid or still in use in script.
96 if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
97 motion_cache.instantiate();
98 motion_cache->owner_id = get_instance_id();
99 }
100
101 motion_cache->result = result;
102
103 return motion_cache;
104 }
105
106 return Ref<KinematicCollision3D>();
107 }
108
109 bool PhysicsBody3D::move_and_collide(const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {
110 bool colliding = PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);
111
112 // Restore direction of motion to be along original motion,
113 // in order to avoid sliding due to recovery,
114 // but only if collision depth is low enough to avoid tunneling.
115 if (p_cancel_sliding) {
116 real_t motion_length = p_parameters.motion.length();
117 real_t precision = 0.001;
118
119 if (colliding) {
120 // Can't just use margin as a threshold because collision depth is calculated on unsafe motion,
121 // so even in normal resting cases the depth can be a bit more than the margin.
122 precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction);
123
124 if (r_result.collisions[0].depth > p_parameters.margin + precision) {
125 p_cancel_sliding = false;
126 }
127 }
128
129 if (p_cancel_sliding) {
130 // When motion is null, recovery is the resulting motion.
131 Vector3 motion_normal;
132 if (motion_length > CMP_EPSILON) {
133 motion_normal = p_parameters.motion / motion_length;
134 }
135
136 // Check depth of recovery.
137 real_t projected_length = r_result.travel.dot(motion_normal);
138 Vector3 recovery = r_result.travel - motion_normal * projected_length;
139 real_t recovery_length = recovery.length();
140 // Fixes cases where canceling slide causes the motion to go too deep into the ground,
141 // because we're only taking rest information into account and not general recovery.
142 if (recovery_length < p_parameters.margin + precision) {
143 // Apply adjustment to motion.
144 r_result.travel = motion_normal * projected_length;
145 r_result.remainder = p_parameters.motion - r_result.travel;
146 }
147 }
148 }
149
150 for (int i = 0; i < 3; i++) {
151 if (locked_axis & (1 << i)) {
152 r_result.travel[i] = 0;
153 }
154 }
155
156 if (!p_test_only) {
157 Transform3D gt = p_parameters.from;
158 gt.origin += r_result.travel;
159 set_global_transform(gt);
160 }
161
162 return colliding;
163 }
164
165 bool PhysicsBody3D::test_move(const Transform3D &p_from, const Vector3 &p_motion, const Ref<KinematicCollision3D> &r_collision, real_t p_margin, bool p_recovery_as_collision, int p_max_collisions) {
166 ERR_FAIL_COND_V(!is_inside_tree(), false);
167
168 PhysicsServer3D::MotionResult *r = nullptr;
169 PhysicsServer3D::MotionResult temp_result;
170 if (r_collision.is_valid()) {
171 r = &r_collision->result;
172 } else {
173 r = &temp_result;
174 }
175
176 PhysicsServer3D::MotionParameters parameters(p_from, p_motion, p_margin);
177 parameters.recovery_as_collision = p_recovery_as_collision;
178 parameters.max_collisions = p_max_collisions;
179
180 return PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), parameters, r);
181 }
182
183 Vector3 PhysicsBody3D::get_gravity() const {
184 PhysicsDirectBodyState3D *state = PhysicsServer3D::get_singleton()->body_get_direct_state(get_rid());
185 ERR_FAIL_NULL_V(state, Vector3());
186 return state->get_total_gravity();
187 }
188
189 void PhysicsBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_lock) {
190 if (p_lock) {
191 locked_axis |= p_axis;
192 } else {
193 locked_axis &= (~p_axis);
194 }
195 PhysicsServer3D::get_singleton()->body_set_axis_lock(get_rid(), p_axis, p_lock);
196 }
197
198 bool PhysicsBody3D::get_axis_lock(PhysicsServer3D::BodyAxis p_axis) const {
199 return (locked_axis & p_axis);
200 }
201
202 Vector3 PhysicsBody3D::get_linear_velocity() const {
203 return Vector3();
204 }
205
206 Vector3 PhysicsBody3D::get_angular_velocity() const {
207 return Vector3();
208 }
209
210 real_t PhysicsBody3D::get_inverse_mass() const {
211 return 0;
212 }
213
214 PackedStringArray PhysicsBody3D::get_configuration_warnings() const {
215 PackedStringArray warnings = CollisionObject3D::get_configuration_warnings();
216
217 if (SceneTree::is_fti_enabled_in_project() && !is_physics_interpolated()) {
218 warnings.push_back(RTR("PhysicsBody3D will not work correctly on a non-interpolated branch of the SceneTree.\nCheck the node's inherited physics_interpolation_mode."));
219 }
220
221 return warnings;
222 }
223
224 ///////////////////////////////////////
225
226 //so, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
227
228 ///////////////////////////////////////
229
230 ///////////////////////////////////////