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 | #include "scene/scene_string_names.h" |
34 | |
35 | void PhysicsBody3D::_bind_methods() { |
36 | 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)); |
37 | 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)); |
38 | |
39 | ClassDB::bind_method(D_METHOD("set_axis_lock" , "axis" , "lock" ), &PhysicsBody3D::set_axis_lock); |
40 | ClassDB::bind_method(D_METHOD("get_axis_lock" , "axis" ), &PhysicsBody3D::get_axis_lock); |
41 | |
42 | ClassDB::bind_method(D_METHOD("get_collision_exceptions" ), &PhysicsBody3D::get_collision_exceptions); |
43 | ClassDB::bind_method(D_METHOD("add_collision_exception_with" , "body" ), &PhysicsBody3D::add_collision_exception_with); |
44 | ClassDB::bind_method(D_METHOD("remove_collision_exception_with" , "body" ), &PhysicsBody3D::remove_collision_exception_with); |
45 | |
46 | ADD_GROUP("Axis Lock" , "axis_lock_" ); |
47 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_x" ), "set_axis_lock" , "get_axis_lock" , PhysicsServer3D::BODY_AXIS_LINEAR_X); |
48 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_y" ), "set_axis_lock" , "get_axis_lock" , PhysicsServer3D::BODY_AXIS_LINEAR_Y); |
49 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_z" ), "set_axis_lock" , "get_axis_lock" , PhysicsServer3D::BODY_AXIS_LINEAR_Z); |
50 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_x" ), "set_axis_lock" , "get_axis_lock" , PhysicsServer3D::BODY_AXIS_ANGULAR_X); |
51 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_y" ), "set_axis_lock" , "get_axis_lock" , PhysicsServer3D::BODY_AXIS_ANGULAR_Y); |
52 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_z" ), "set_axis_lock" , "get_axis_lock" , PhysicsServer3D::BODY_AXIS_ANGULAR_Z); |
53 | } |
54 | |
55 | PhysicsBody3D::PhysicsBody3D(PhysicsServer3D::BodyMode p_mode) : |
56 | CollisionObject3D(PhysicsServer3D::get_singleton()->body_create(), false) { |
57 | set_body_mode(p_mode); |
58 | } |
59 | |
60 | PhysicsBody3D::~PhysicsBody3D() { |
61 | if (motion_cache.is_valid()) { |
62 | motion_cache->owner = nullptr; |
63 | } |
64 | } |
65 | |
66 | TypedArray<PhysicsBody3D> PhysicsBody3D::get_collision_exceptions() { |
67 | List<RID> exceptions; |
68 | PhysicsServer3D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions); |
69 | Array ret; |
70 | for (const RID &body : exceptions) { |
71 | ObjectID instance_id = PhysicsServer3D::get_singleton()->body_get_object_instance_id(body); |
72 | Object *obj = ObjectDB::get_instance(instance_id); |
73 | PhysicsBody3D *physics_body = Object::cast_to<PhysicsBody3D>(obj); |
74 | ret.append(physics_body); |
75 | } |
76 | return ret; |
77 | } |
78 | |
79 | void PhysicsBody3D::add_collision_exception_with(Node *p_node) { |
80 | ERR_FAIL_NULL(p_node); |
81 | CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node); |
82 | ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D)." ); |
83 | PhysicsServer3D::get_singleton()->body_add_collision_exception(get_rid(), collision_object->get_rid()); |
84 | } |
85 | |
86 | void PhysicsBody3D::remove_collision_exception_with(Node *p_node) { |
87 | ERR_FAIL_NULL(p_node); |
88 | CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node); |
89 | ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D)." ); |
90 | PhysicsServer3D::get_singleton()->body_remove_collision_exception(get_rid(), collision_object->get_rid()); |
91 | } |
92 | |
93 | 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) { |
94 | PhysicsServer3D::MotionParameters parameters(get_global_transform(), p_motion, p_margin); |
95 | parameters.max_collisions = p_max_collisions; |
96 | parameters.recovery_as_collision = p_recovery_as_collision; |
97 | |
98 | PhysicsServer3D::MotionResult result; |
99 | |
100 | if (move_and_collide(parameters, result, p_test_only)) { |
101 | // Create a new instance when the cached reference is invalid or still in use in script. |
102 | if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) { |
103 | motion_cache.instantiate(); |
104 | motion_cache->owner = this; |
105 | } |
106 | |
107 | motion_cache->result = result; |
108 | |
109 | return motion_cache; |
110 | } |
111 | |
112 | return Ref<KinematicCollision3D>(); |
113 | } |
114 | |
115 | bool PhysicsBody3D::move_and_collide(const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) { |
116 | bool colliding = PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result); |
117 | |
118 | // Restore direction of motion to be along original motion, |
119 | // in order to avoid sliding due to recovery, |
120 | // but only if collision depth is low enough to avoid tunneling. |
121 | if (p_cancel_sliding) { |
122 | real_t motion_length = p_parameters.motion.length(); |
123 | real_t precision = 0.001; |
124 | |
125 | if (colliding) { |
126 | // Can't just use margin as a threshold because collision depth is calculated on unsafe motion, |
127 | // so even in normal resting cases the depth can be a bit more than the margin. |
128 | precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction); |
129 | |
130 | if (r_result.collisions[0].depth > p_parameters.margin + precision) { |
131 | p_cancel_sliding = false; |
132 | } |
133 | } |
134 | |
135 | if (p_cancel_sliding) { |
136 | // When motion is null, recovery is the resulting motion. |
137 | Vector3 motion_normal; |
138 | if (motion_length > CMP_EPSILON) { |
139 | motion_normal = p_parameters.motion / motion_length; |
140 | } |
141 | |
142 | // Check depth of recovery. |
143 | real_t projected_length = r_result.travel.dot(motion_normal); |
144 | Vector3 recovery = r_result.travel - motion_normal * projected_length; |
145 | real_t recovery_length = recovery.length(); |
146 | // Fixes cases where canceling slide causes the motion to go too deep into the ground, |
147 | // because we're only taking rest information into account and not general recovery. |
148 | if (recovery_length < p_parameters.margin + precision) { |
149 | // Apply adjustment to motion. |
150 | r_result.travel = motion_normal * projected_length; |
151 | r_result.remainder = p_parameters.motion - r_result.travel; |
152 | } |
153 | } |
154 | } |
155 | |
156 | for (int i = 0; i < 3; i++) { |
157 | if (locked_axis & (1 << i)) { |
158 | r_result.travel[i] = 0; |
159 | } |
160 | } |
161 | |
162 | if (!p_test_only) { |
163 | Transform3D gt = p_parameters.from; |
164 | gt.origin += r_result.travel; |
165 | set_global_transform(gt); |
166 | } |
167 | |
168 | return colliding; |
169 | } |
170 | |
171 | 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) { |
172 | ERR_FAIL_COND_V(!is_inside_tree(), false); |
173 | |
174 | PhysicsServer3D::MotionResult *r = nullptr; |
175 | PhysicsServer3D::MotionResult temp_result; |
176 | if (r_collision.is_valid()) { |
177 | // Needs const_cast because method bindings don't support non-const Ref. |
178 | r = const_cast<PhysicsServer3D::MotionResult *>(&r_collision->result); |
179 | } else { |
180 | r = &temp_result; |
181 | } |
182 | |
183 | PhysicsServer3D::MotionParameters parameters(p_from, p_motion, p_margin); |
184 | parameters.recovery_as_collision = p_recovery_as_collision; |
185 | |
186 | return PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), parameters, r); |
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 | void StaticBody3D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) { |
215 | if (physics_material_override.is_valid()) { |
216 | physics_material_override->disconnect_changed(callable_mp(this, &StaticBody3D::_reload_physics_characteristics)); |
217 | } |
218 | |
219 | physics_material_override = p_physics_material_override; |
220 | |
221 | if (physics_material_override.is_valid()) { |
222 | physics_material_override->connect_changed(callable_mp(this, &StaticBody3D::_reload_physics_characteristics)); |
223 | } |
224 | _reload_physics_characteristics(); |
225 | } |
226 | |
227 | Ref<PhysicsMaterial> StaticBody3D::get_physics_material_override() const { |
228 | return physics_material_override; |
229 | } |
230 | |
231 | void StaticBody3D::set_constant_linear_velocity(const Vector3 &p_vel) { |
232 | constant_linear_velocity = p_vel; |
233 | |
234 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity); |
235 | } |
236 | |
237 | void StaticBody3D::set_constant_angular_velocity(const Vector3 &p_vel) { |
238 | constant_angular_velocity = p_vel; |
239 | |
240 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity); |
241 | } |
242 | |
243 | Vector3 StaticBody3D::get_constant_linear_velocity() const { |
244 | return constant_linear_velocity; |
245 | } |
246 | |
247 | Vector3 StaticBody3D::get_constant_angular_velocity() const { |
248 | return constant_angular_velocity; |
249 | } |
250 | |
251 | void StaticBody3D::_bind_methods() { |
252 | ClassDB::bind_method(D_METHOD("set_constant_linear_velocity" , "vel" ), &StaticBody3D::set_constant_linear_velocity); |
253 | ClassDB::bind_method(D_METHOD("set_constant_angular_velocity" , "vel" ), &StaticBody3D::set_constant_angular_velocity); |
254 | ClassDB::bind_method(D_METHOD("get_constant_linear_velocity" ), &StaticBody3D::get_constant_linear_velocity); |
255 | ClassDB::bind_method(D_METHOD("get_constant_angular_velocity" ), &StaticBody3D::get_constant_angular_velocity); |
256 | |
257 | ClassDB::bind_method(D_METHOD("set_physics_material_override" , "physics_material_override" ), &StaticBody3D::set_physics_material_override); |
258 | ClassDB::bind_method(D_METHOD("get_physics_material_override" ), &StaticBody3D::get_physics_material_override); |
259 | |
260 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override" , PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial" ), "set_physics_material_override" , "get_physics_material_override" ); |
261 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_linear_velocity" , PROPERTY_HINT_NONE, "suffix:m/s" ), "set_constant_linear_velocity" , "get_constant_linear_velocity" ); |
262 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_angular_velocity" , PROPERTY_HINT_NONE, U"radians,suffix:\u00B0/s" ), "set_constant_angular_velocity" , "get_constant_angular_velocity" ); |
263 | } |
264 | |
265 | StaticBody3D::StaticBody3D(PhysicsServer3D::BodyMode p_mode) : |
266 | PhysicsBody3D(p_mode) { |
267 | } |
268 | |
269 | void StaticBody3D::_reload_physics_characteristics() { |
270 | if (physics_material_override.is_null()) { |
271 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, 0); |
272 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, 1); |
273 | } else { |
274 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce()); |
275 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, physics_material_override->computed_friction()); |
276 | } |
277 | } |
278 | |
279 | Vector3 AnimatableBody3D::get_linear_velocity() const { |
280 | return linear_velocity; |
281 | } |
282 | |
283 | Vector3 AnimatableBody3D::get_angular_velocity() const { |
284 | return angular_velocity; |
285 | } |
286 | |
287 | void AnimatableBody3D::set_sync_to_physics(bool p_enable) { |
288 | if (sync_to_physics == p_enable) { |
289 | return; |
290 | } |
291 | |
292 | sync_to_physics = p_enable; |
293 | |
294 | _update_kinematic_motion(); |
295 | } |
296 | |
297 | bool AnimatableBody3D::is_sync_to_physics_enabled() const { |
298 | return sync_to_physics; |
299 | } |
300 | |
301 | void AnimatableBody3D::_update_kinematic_motion() { |
302 | #ifdef TOOLS_ENABLED |
303 | if (Engine::get_singleton()->is_editor_hint()) { |
304 | return; |
305 | } |
306 | #endif |
307 | |
308 | if (sync_to_physics) { |
309 | set_only_update_transform_changes(true); |
310 | set_notify_local_transform(true); |
311 | } else { |
312 | set_only_update_transform_changes(false); |
313 | set_notify_local_transform(false); |
314 | } |
315 | } |
316 | |
317 | void AnimatableBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) { |
318 | linear_velocity = p_state->get_linear_velocity(); |
319 | angular_velocity = p_state->get_angular_velocity(); |
320 | |
321 | if (!sync_to_physics) { |
322 | return; |
323 | } |
324 | |
325 | last_valid_transform = p_state->get_transform(); |
326 | set_notify_local_transform(false); |
327 | set_global_transform(last_valid_transform); |
328 | set_notify_local_transform(true); |
329 | _on_transform_changed(); |
330 | } |
331 | |
332 | void AnimatableBody3D::_notification(int p_what) { |
333 | #ifdef TOOLS_ENABLED |
334 | if (Engine::get_singleton()->is_editor_hint()) { |
335 | return; |
336 | } |
337 | #endif |
338 | switch (p_what) { |
339 | case NOTIFICATION_ENTER_TREE: { |
340 | last_valid_transform = get_global_transform(); |
341 | _update_kinematic_motion(); |
342 | } break; |
343 | |
344 | case NOTIFICATION_EXIT_TREE: { |
345 | set_only_update_transform_changes(false); |
346 | set_notify_local_transform(false); |
347 | } break; |
348 | |
349 | case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { |
350 | // Used by sync to physics, send the new transform to the physics... |
351 | Transform3D new_transform = get_global_transform(); |
352 | |
353 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_TRANSFORM, new_transform); |
354 | |
355 | // ... but then revert changes. |
356 | set_notify_local_transform(false); |
357 | set_global_transform(last_valid_transform); |
358 | set_notify_local_transform(true); |
359 | _on_transform_changed(); |
360 | } break; |
361 | } |
362 | } |
363 | |
364 | void AnimatableBody3D::_bind_methods() { |
365 | ClassDB::bind_method(D_METHOD("set_sync_to_physics" , "enable" ), &AnimatableBody3D::set_sync_to_physics); |
366 | ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled" ), &AnimatableBody3D::is_sync_to_physics_enabled); |
367 | |
368 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_to_physics" ), "set_sync_to_physics" , "is_sync_to_physics_enabled" ); |
369 | } |
370 | |
371 | AnimatableBody3D::AnimatableBody3D() : |
372 | StaticBody3D(PhysicsServer3D::BODY_MODE_KINEMATIC) { |
373 | PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &AnimatableBody3D::_body_state_changed)); |
374 | } |
375 | |
376 | void RigidBody3D::_body_enter_tree(ObjectID p_id) { |
377 | Object *obj = ObjectDB::get_instance(p_id); |
378 | Node *node = Object::cast_to<Node>(obj); |
379 | ERR_FAIL_NULL(node); |
380 | ERR_FAIL_NULL(contact_monitor); |
381 | HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id); |
382 | ERR_FAIL_COND(!E); |
383 | ERR_FAIL_COND(E->value.in_tree); |
384 | |
385 | E->value.in_tree = true; |
386 | |
387 | contact_monitor->locked = true; |
388 | |
389 | emit_signal(SceneStringNames::get_singleton()->body_entered, node); |
390 | |
391 | for (int i = 0; i < E->value.shapes.size(); i++) { |
392 | emit_signal(SceneStringNames::get_singleton()->body_shape_entered, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape); |
393 | } |
394 | |
395 | contact_monitor->locked = false; |
396 | } |
397 | |
398 | void RigidBody3D::_body_exit_tree(ObjectID p_id) { |
399 | Object *obj = ObjectDB::get_instance(p_id); |
400 | Node *node = Object::cast_to<Node>(obj); |
401 | ERR_FAIL_NULL(node); |
402 | ERR_FAIL_NULL(contact_monitor); |
403 | HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id); |
404 | ERR_FAIL_COND(!E); |
405 | ERR_FAIL_COND(!E->value.in_tree); |
406 | E->value.in_tree = false; |
407 | |
408 | contact_monitor->locked = true; |
409 | |
410 | emit_signal(SceneStringNames::get_singleton()->body_exited, node); |
411 | |
412 | for (int i = 0; i < E->value.shapes.size(); i++) { |
413 | emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape); |
414 | } |
415 | |
416 | contact_monitor->locked = false; |
417 | } |
418 | |
419 | void RigidBody3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape) { |
420 | bool body_in = p_status == 1; |
421 | ObjectID objid = p_instance; |
422 | |
423 | Object *obj = ObjectDB::get_instance(objid); |
424 | Node *node = Object::cast_to<Node>(obj); |
425 | |
426 | ERR_FAIL_NULL(contact_monitor); |
427 | HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(objid); |
428 | |
429 | ERR_FAIL_COND(!body_in && !E); |
430 | |
431 | if (body_in) { |
432 | if (!E) { |
433 | E = contact_monitor->body_map.insert(objid, BodyState()); |
434 | E->value.rid = p_body; |
435 | //E->value.rc=0; |
436 | E->value.in_tree = node && node->is_inside_tree(); |
437 | if (node) { |
438 | node->connect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody3D::_body_enter_tree).bind(objid)); |
439 | node->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody3D::_body_exit_tree).bind(objid)); |
440 | if (E->value.in_tree) { |
441 | emit_signal(SceneStringNames::get_singleton()->body_entered, node); |
442 | } |
443 | } |
444 | } |
445 | //E->value.rc++; |
446 | if (node) { |
447 | E->value.shapes.insert(ShapePair(p_body_shape, p_local_shape)); |
448 | } |
449 | |
450 | if (E->value.in_tree) { |
451 | emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_body, node, p_body_shape, p_local_shape); |
452 | } |
453 | |
454 | } else { |
455 | //E->value.rc--; |
456 | |
457 | if (node) { |
458 | E->value.shapes.erase(ShapePair(p_body_shape, p_local_shape)); |
459 | } |
460 | |
461 | bool in_tree = E->value.in_tree; |
462 | |
463 | if (E->value.shapes.is_empty()) { |
464 | if (node) { |
465 | node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody3D::_body_enter_tree)); |
466 | node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody3D::_body_exit_tree)); |
467 | if (in_tree) { |
468 | emit_signal(SceneStringNames::get_singleton()->body_exited, node); |
469 | } |
470 | } |
471 | |
472 | contact_monitor->body_map.remove(E); |
473 | } |
474 | if (node && in_tree) { |
475 | emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_body, obj, p_body_shape, p_local_shape); |
476 | } |
477 | } |
478 | } |
479 | |
480 | struct _RigidBodyInOut { |
481 | RID rid; |
482 | ObjectID id; |
483 | int shape = 0; |
484 | int local_shape = 0; |
485 | }; |
486 | |
487 | void RigidBody3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) { |
488 | set_global_transform(p_state->get_transform()); |
489 | |
490 | linear_velocity = p_state->get_linear_velocity(); |
491 | angular_velocity = p_state->get_angular_velocity(); |
492 | |
493 | inverse_inertia_tensor = p_state->get_inverse_inertia_tensor(); |
494 | |
495 | if (sleeping != p_state->is_sleeping()) { |
496 | sleeping = p_state->is_sleeping(); |
497 | emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed); |
498 | } |
499 | } |
500 | |
501 | void RigidBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) { |
502 | lock_callback(); |
503 | |
504 | set_ignore_transform_notification(true); |
505 | _sync_body_state(p_state); |
506 | |
507 | GDVIRTUAL_CALL(_integrate_forces, p_state); |
508 | |
509 | _sync_body_state(p_state); |
510 | set_ignore_transform_notification(false); |
511 | _on_transform_changed(); |
512 | |
513 | if (contact_monitor) { |
514 | contact_monitor->locked = true; |
515 | |
516 | //untag all |
517 | int rc = 0; |
518 | for (KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) { |
519 | for (int i = 0; i < E.value.shapes.size(); i++) { |
520 | E.value.shapes[i].tagged = false; |
521 | rc++; |
522 | } |
523 | } |
524 | |
525 | _RigidBodyInOut *toadd = (_RigidBodyInOut *)alloca(p_state->get_contact_count() * sizeof(_RigidBodyInOut)); |
526 | int toadd_count = 0; |
527 | RigidBody3D_RemoveAction *toremove = (RigidBody3D_RemoveAction *)alloca(rc * sizeof(RigidBody3D_RemoveAction)); |
528 | int toremove_count = 0; |
529 | |
530 | //put the ones to add |
531 | |
532 | for (int i = 0; i < p_state->get_contact_count(); i++) { |
533 | RID col_rid = p_state->get_contact_collider(i); |
534 | ObjectID col_obj = p_state->get_contact_collider_id(i); |
535 | int local_shape = p_state->get_contact_local_shape(i); |
536 | int col_shape = p_state->get_contact_collider_shape(i); |
537 | |
538 | HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(col_obj); |
539 | if (!E) { |
540 | toadd[toadd_count].rid = col_rid; |
541 | toadd[toadd_count].local_shape = local_shape; |
542 | toadd[toadd_count].id = col_obj; |
543 | toadd[toadd_count].shape = col_shape; |
544 | toadd_count++; |
545 | continue; |
546 | } |
547 | |
548 | ShapePair sp(col_shape, local_shape); |
549 | int idx = E->value.shapes.find(sp); |
550 | if (idx == -1) { |
551 | toadd[toadd_count].rid = col_rid; |
552 | toadd[toadd_count].local_shape = local_shape; |
553 | toadd[toadd_count].id = col_obj; |
554 | toadd[toadd_count].shape = col_shape; |
555 | toadd_count++; |
556 | continue; |
557 | } |
558 | |
559 | E->value.shapes[idx].tagged = true; |
560 | } |
561 | |
562 | //put the ones to remove |
563 | |
564 | for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) { |
565 | for (int i = 0; i < E.value.shapes.size(); i++) { |
566 | if (!E.value.shapes[i].tagged) { |
567 | toremove[toremove_count].rid = E.value.rid; |
568 | toremove[toremove_count].body_id = E.key; |
569 | toremove[toremove_count].pair = E.value.shapes[i]; |
570 | toremove_count++; |
571 | } |
572 | } |
573 | } |
574 | |
575 | //process removals |
576 | |
577 | for (int i = 0; i < toremove_count; i++) { |
578 | _body_inout(0, toremove[i].rid, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape); |
579 | } |
580 | |
581 | //process additions |
582 | |
583 | for (int i = 0; i < toadd_count; i++) { |
584 | _body_inout(1, toremove[i].rid, toadd[i].id, toadd[i].shape, toadd[i].local_shape); |
585 | } |
586 | |
587 | contact_monitor->locked = false; |
588 | } |
589 | |
590 | unlock_callback(); |
591 | } |
592 | |
593 | void RigidBody3D::_notification(int p_what) { |
594 | #ifdef TOOLS_ENABLED |
595 | switch (p_what) { |
596 | case NOTIFICATION_ENTER_TREE: { |
597 | if (Engine::get_singleton()->is_editor_hint()) { |
598 | set_notify_local_transform(true); // Used for warnings and only in editor. |
599 | } |
600 | } break; |
601 | |
602 | case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { |
603 | update_configuration_warnings(); |
604 | } break; |
605 | } |
606 | #endif |
607 | } |
608 | |
609 | void RigidBody3D::_apply_body_mode() { |
610 | if (freeze) { |
611 | switch (freeze_mode) { |
612 | case FREEZE_MODE_STATIC: { |
613 | set_body_mode(PhysicsServer3D::BODY_MODE_STATIC); |
614 | } break; |
615 | case FREEZE_MODE_KINEMATIC: { |
616 | set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC); |
617 | } break; |
618 | } |
619 | } else if (lock_rotation) { |
620 | set_body_mode(PhysicsServer3D::BODY_MODE_RIGID_LINEAR); |
621 | } else { |
622 | set_body_mode(PhysicsServer3D::BODY_MODE_RIGID); |
623 | } |
624 | } |
625 | |
626 | void RigidBody3D::set_lock_rotation_enabled(bool p_lock_rotation) { |
627 | if (p_lock_rotation == lock_rotation) { |
628 | return; |
629 | } |
630 | |
631 | lock_rotation = p_lock_rotation; |
632 | _apply_body_mode(); |
633 | } |
634 | |
635 | bool RigidBody3D::is_lock_rotation_enabled() const { |
636 | return lock_rotation; |
637 | } |
638 | |
639 | void RigidBody3D::set_freeze_enabled(bool p_freeze) { |
640 | if (p_freeze == freeze) { |
641 | return; |
642 | } |
643 | |
644 | freeze = p_freeze; |
645 | _apply_body_mode(); |
646 | } |
647 | |
648 | bool RigidBody3D::is_freeze_enabled() const { |
649 | return freeze; |
650 | } |
651 | |
652 | void RigidBody3D::set_freeze_mode(FreezeMode p_freeze_mode) { |
653 | if (p_freeze_mode == freeze_mode) { |
654 | return; |
655 | } |
656 | |
657 | freeze_mode = p_freeze_mode; |
658 | _apply_body_mode(); |
659 | } |
660 | |
661 | RigidBody3D::FreezeMode RigidBody3D::get_freeze_mode() const { |
662 | return freeze_mode; |
663 | } |
664 | |
665 | void RigidBody3D::set_mass(real_t p_mass) { |
666 | ERR_FAIL_COND(p_mass <= 0); |
667 | mass = p_mass; |
668 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_MASS, mass); |
669 | } |
670 | |
671 | real_t RigidBody3D::get_mass() const { |
672 | return mass; |
673 | } |
674 | |
675 | void RigidBody3D::set_inertia(const Vector3 &p_inertia) { |
676 | ERR_FAIL_COND(p_inertia.x < 0); |
677 | ERR_FAIL_COND(p_inertia.y < 0); |
678 | ERR_FAIL_COND(p_inertia.z < 0); |
679 | |
680 | inertia = p_inertia; |
681 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_INERTIA, inertia); |
682 | } |
683 | |
684 | const Vector3 &RigidBody3D::get_inertia() const { |
685 | return inertia; |
686 | } |
687 | |
688 | void RigidBody3D::set_center_of_mass_mode(CenterOfMassMode p_mode) { |
689 | if (center_of_mass_mode == p_mode) { |
690 | return; |
691 | } |
692 | |
693 | center_of_mass_mode = p_mode; |
694 | |
695 | switch (center_of_mass_mode) { |
696 | case CENTER_OF_MASS_MODE_AUTO: { |
697 | center_of_mass = Vector3(); |
698 | PhysicsServer3D::get_singleton()->body_reset_mass_properties(get_rid()); |
699 | if (inertia != Vector3()) { |
700 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_INERTIA, inertia); |
701 | } |
702 | } break; |
703 | |
704 | case CENTER_OF_MASS_MODE_CUSTOM: { |
705 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS, center_of_mass); |
706 | } break; |
707 | } |
708 | } |
709 | |
710 | RigidBody3D::CenterOfMassMode RigidBody3D::get_center_of_mass_mode() const { |
711 | return center_of_mass_mode; |
712 | } |
713 | |
714 | void RigidBody3D::set_center_of_mass(const Vector3 &p_center_of_mass) { |
715 | if (center_of_mass == p_center_of_mass) { |
716 | return; |
717 | } |
718 | |
719 | ERR_FAIL_COND(center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM); |
720 | center_of_mass = p_center_of_mass; |
721 | |
722 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS, center_of_mass); |
723 | } |
724 | |
725 | const Vector3 &RigidBody3D::get_center_of_mass() const { |
726 | return center_of_mass; |
727 | } |
728 | |
729 | void RigidBody3D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) { |
730 | if (physics_material_override.is_valid()) { |
731 | physics_material_override->disconnect_changed(callable_mp(this, &RigidBody3D::_reload_physics_characteristics)); |
732 | } |
733 | |
734 | physics_material_override = p_physics_material_override; |
735 | |
736 | if (physics_material_override.is_valid()) { |
737 | physics_material_override->connect_changed(callable_mp(this, &RigidBody3D::_reload_physics_characteristics)); |
738 | } |
739 | _reload_physics_characteristics(); |
740 | } |
741 | |
742 | Ref<PhysicsMaterial> RigidBody3D::get_physics_material_override() const { |
743 | return physics_material_override; |
744 | } |
745 | |
746 | void RigidBody3D::set_gravity_scale(real_t p_gravity_scale) { |
747 | gravity_scale = p_gravity_scale; |
748 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, gravity_scale); |
749 | } |
750 | |
751 | real_t RigidBody3D::get_gravity_scale() const { |
752 | return gravity_scale; |
753 | } |
754 | |
755 | void RigidBody3D::set_linear_damp_mode(DampMode p_mode) { |
756 | linear_damp_mode = p_mode; |
757 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode); |
758 | } |
759 | |
760 | RigidBody3D::DampMode RigidBody3D::get_linear_damp_mode() const { |
761 | return linear_damp_mode; |
762 | } |
763 | |
764 | void RigidBody3D::set_angular_damp_mode(DampMode p_mode) { |
765 | angular_damp_mode = p_mode; |
766 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode); |
767 | } |
768 | |
769 | RigidBody3D::DampMode RigidBody3D::get_angular_damp_mode() const { |
770 | return angular_damp_mode; |
771 | } |
772 | |
773 | void RigidBody3D::set_linear_damp(real_t p_linear_damp) { |
774 | ERR_FAIL_COND(p_linear_damp < 0.0); |
775 | linear_damp = p_linear_damp; |
776 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP, linear_damp); |
777 | } |
778 | |
779 | real_t RigidBody3D::get_linear_damp() const { |
780 | return linear_damp; |
781 | } |
782 | |
783 | void RigidBody3D::set_angular_damp(real_t p_angular_damp) { |
784 | ERR_FAIL_COND(p_angular_damp < 0.0); |
785 | angular_damp = p_angular_damp; |
786 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP, angular_damp); |
787 | } |
788 | |
789 | real_t RigidBody3D::get_angular_damp() const { |
790 | return angular_damp; |
791 | } |
792 | |
793 | void RigidBody3D::set_axis_velocity(const Vector3 &p_axis) { |
794 | Vector3 axis = p_axis.normalized(); |
795 | linear_velocity -= axis * axis.dot(linear_velocity); |
796 | linear_velocity += p_axis; |
797 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity); |
798 | } |
799 | |
800 | void RigidBody3D::set_linear_velocity(const Vector3 &p_velocity) { |
801 | linear_velocity = p_velocity; |
802 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity); |
803 | } |
804 | |
805 | Vector3 RigidBody3D::get_linear_velocity() const { |
806 | return linear_velocity; |
807 | } |
808 | |
809 | void RigidBody3D::set_angular_velocity(const Vector3 &p_velocity) { |
810 | angular_velocity = p_velocity; |
811 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity); |
812 | } |
813 | |
814 | Vector3 RigidBody3D::get_angular_velocity() const { |
815 | return angular_velocity; |
816 | } |
817 | |
818 | Basis RigidBody3D::get_inverse_inertia_tensor() const { |
819 | return inverse_inertia_tensor; |
820 | } |
821 | |
822 | void RigidBody3D::set_use_custom_integrator(bool p_enable) { |
823 | if (custom_integrator == p_enable) { |
824 | return; |
825 | } |
826 | |
827 | custom_integrator = p_enable; |
828 | PhysicsServer3D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable); |
829 | } |
830 | |
831 | bool RigidBody3D::is_using_custom_integrator() { |
832 | return custom_integrator; |
833 | } |
834 | |
835 | void RigidBody3D::set_sleeping(bool p_sleeping) { |
836 | sleeping = p_sleeping; |
837 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_SLEEPING, sleeping); |
838 | } |
839 | |
840 | void RigidBody3D::set_can_sleep(bool p_active) { |
841 | can_sleep = p_active; |
842 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_CAN_SLEEP, p_active); |
843 | } |
844 | |
845 | bool RigidBody3D::is_able_to_sleep() const { |
846 | return can_sleep; |
847 | } |
848 | |
849 | bool RigidBody3D::is_sleeping() const { |
850 | return sleeping; |
851 | } |
852 | |
853 | void RigidBody3D::set_max_contacts_reported(int p_amount) { |
854 | max_contacts_reported = p_amount; |
855 | PhysicsServer3D::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount); |
856 | } |
857 | |
858 | int RigidBody3D::get_max_contacts_reported() const { |
859 | return max_contacts_reported; |
860 | } |
861 | |
862 | int RigidBody3D::get_contact_count() const { |
863 | PhysicsDirectBodyState3D *bs = PhysicsServer3D::get_singleton()->body_get_direct_state(get_rid()); |
864 | ERR_FAIL_NULL_V(bs, 0); |
865 | return bs->get_contact_count(); |
866 | } |
867 | |
868 | void RigidBody3D::apply_central_impulse(const Vector3 &p_impulse) { |
869 | PhysicsServer3D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse); |
870 | } |
871 | |
872 | void RigidBody3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) { |
873 | PhysicsServer3D *singleton = PhysicsServer3D::get_singleton(); |
874 | singleton->body_apply_impulse(get_rid(), p_impulse, p_position); |
875 | } |
876 | |
877 | void RigidBody3D::apply_torque_impulse(const Vector3 &p_impulse) { |
878 | PhysicsServer3D::get_singleton()->body_apply_torque_impulse(get_rid(), p_impulse); |
879 | } |
880 | |
881 | void RigidBody3D::apply_central_force(const Vector3 &p_force) { |
882 | PhysicsServer3D::get_singleton()->body_apply_central_force(get_rid(), p_force); |
883 | } |
884 | |
885 | void RigidBody3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) { |
886 | PhysicsServer3D *singleton = PhysicsServer3D::get_singleton(); |
887 | singleton->body_apply_force(get_rid(), p_force, p_position); |
888 | } |
889 | |
890 | void RigidBody3D::apply_torque(const Vector3 &p_torque) { |
891 | PhysicsServer3D::get_singleton()->body_apply_torque(get_rid(), p_torque); |
892 | } |
893 | |
894 | void RigidBody3D::add_constant_central_force(const Vector3 &p_force) { |
895 | PhysicsServer3D::get_singleton()->body_add_constant_central_force(get_rid(), p_force); |
896 | } |
897 | |
898 | void RigidBody3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_position) { |
899 | PhysicsServer3D *singleton = PhysicsServer3D::get_singleton(); |
900 | singleton->body_add_constant_force(get_rid(), p_force, p_position); |
901 | } |
902 | |
903 | void RigidBody3D::add_constant_torque(const Vector3 &p_torque) { |
904 | PhysicsServer3D::get_singleton()->body_add_constant_torque(get_rid(), p_torque); |
905 | } |
906 | |
907 | void RigidBody3D::set_constant_force(const Vector3 &p_force) { |
908 | PhysicsServer3D::get_singleton()->body_set_constant_force(get_rid(), p_force); |
909 | } |
910 | |
911 | Vector3 RigidBody3D::get_constant_force() const { |
912 | return PhysicsServer3D::get_singleton()->body_get_constant_force(get_rid()); |
913 | } |
914 | |
915 | void RigidBody3D::set_constant_torque(const Vector3 &p_torque) { |
916 | PhysicsServer3D::get_singleton()->body_set_constant_torque(get_rid(), p_torque); |
917 | } |
918 | |
919 | Vector3 RigidBody3D::get_constant_torque() const { |
920 | return PhysicsServer3D::get_singleton()->body_get_constant_torque(get_rid()); |
921 | } |
922 | |
923 | void RigidBody3D::set_use_continuous_collision_detection(bool p_enable) { |
924 | ccd = p_enable; |
925 | PhysicsServer3D::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(), p_enable); |
926 | } |
927 | |
928 | bool RigidBody3D::is_using_continuous_collision_detection() const { |
929 | return ccd; |
930 | } |
931 | |
932 | void RigidBody3D::set_contact_monitor(bool p_enabled) { |
933 | if (p_enabled == is_contact_monitor_enabled()) { |
934 | return; |
935 | } |
936 | |
937 | if (!p_enabled) { |
938 | ERR_FAIL_COND_MSG(contact_monitor->locked, "Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\", false) instead." ); |
939 | |
940 | for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) { |
941 | //clean up mess |
942 | Object *obj = ObjectDB::get_instance(E.key); |
943 | Node *node = Object::cast_to<Node>(obj); |
944 | |
945 | if (node) { |
946 | node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody3D::_body_enter_tree)); |
947 | node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody3D::_body_exit_tree)); |
948 | } |
949 | } |
950 | |
951 | memdelete(contact_monitor); |
952 | contact_monitor = nullptr; |
953 | } else { |
954 | contact_monitor = memnew(ContactMonitor); |
955 | contact_monitor->locked = false; |
956 | } |
957 | } |
958 | |
959 | bool RigidBody3D::is_contact_monitor_enabled() const { |
960 | return contact_monitor != nullptr; |
961 | } |
962 | |
963 | TypedArray<Node3D> RigidBody3D::get_colliding_bodies() const { |
964 | ERR_FAIL_NULL_V(contact_monitor, TypedArray<Node3D>()); |
965 | |
966 | TypedArray<Node3D> ret; |
967 | ret.resize(contact_monitor->body_map.size()); |
968 | int idx = 0; |
969 | for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) { |
970 | Object *obj = ObjectDB::get_instance(E.key); |
971 | if (!obj) { |
972 | ret.resize(ret.size() - 1); //ops |
973 | } else { |
974 | ret[idx++] = obj; |
975 | } |
976 | } |
977 | |
978 | return ret; |
979 | } |
980 | |
981 | PackedStringArray RigidBody3D::get_configuration_warnings() const { |
982 | PackedStringArray warnings = CollisionObject3D::get_configuration_warnings(); |
983 | |
984 | Vector3 scale = get_transform().get_basis().get_scale(); |
985 | if (ABS(scale.x - 1.0) > 0.05 || ABS(scale.y - 1.0) > 0.05 || ABS(scale.z - 1.0) > 0.05) { |
986 | warnings.push_back(RTR("Scale changes to RigidBody3D will be overridden by the physics engine when running.\nPlease change the size in children collision shapes instead." )); |
987 | } |
988 | |
989 | return warnings; |
990 | } |
991 | |
992 | void RigidBody3D::_bind_methods() { |
993 | ClassDB::bind_method(D_METHOD("set_mass" , "mass" ), &RigidBody3D::set_mass); |
994 | ClassDB::bind_method(D_METHOD("get_mass" ), &RigidBody3D::get_mass); |
995 | |
996 | ClassDB::bind_method(D_METHOD("set_inertia" , "inertia" ), &RigidBody3D::set_inertia); |
997 | ClassDB::bind_method(D_METHOD("get_inertia" ), &RigidBody3D::get_inertia); |
998 | |
999 | ClassDB::bind_method(D_METHOD("set_center_of_mass_mode" , "mode" ), &RigidBody3D::set_center_of_mass_mode); |
1000 | ClassDB::bind_method(D_METHOD("get_center_of_mass_mode" ), &RigidBody3D::get_center_of_mass_mode); |
1001 | |
1002 | ClassDB::bind_method(D_METHOD("set_center_of_mass" , "center_of_mass" ), &RigidBody3D::set_center_of_mass); |
1003 | ClassDB::bind_method(D_METHOD("get_center_of_mass" ), &RigidBody3D::get_center_of_mass); |
1004 | |
1005 | ClassDB::bind_method(D_METHOD("set_physics_material_override" , "physics_material_override" ), &RigidBody3D::set_physics_material_override); |
1006 | ClassDB::bind_method(D_METHOD("get_physics_material_override" ), &RigidBody3D::get_physics_material_override); |
1007 | |
1008 | ClassDB::bind_method(D_METHOD("set_linear_velocity" , "linear_velocity" ), &RigidBody3D::set_linear_velocity); |
1009 | ClassDB::bind_method(D_METHOD("get_linear_velocity" ), &RigidBody3D::get_linear_velocity); |
1010 | |
1011 | ClassDB::bind_method(D_METHOD("set_angular_velocity" , "angular_velocity" ), &RigidBody3D::set_angular_velocity); |
1012 | ClassDB::bind_method(D_METHOD("get_angular_velocity" ), &RigidBody3D::get_angular_velocity); |
1013 | |
1014 | ClassDB::bind_method(D_METHOD("get_inverse_inertia_tensor" ), &RigidBody3D::get_inverse_inertia_tensor); |
1015 | |
1016 | ClassDB::bind_method(D_METHOD("set_gravity_scale" , "gravity_scale" ), &RigidBody3D::set_gravity_scale); |
1017 | ClassDB::bind_method(D_METHOD("get_gravity_scale" ), &RigidBody3D::get_gravity_scale); |
1018 | |
1019 | ClassDB::bind_method(D_METHOD("set_linear_damp_mode" , "linear_damp_mode" ), &RigidBody3D::set_linear_damp_mode); |
1020 | ClassDB::bind_method(D_METHOD("get_linear_damp_mode" ), &RigidBody3D::get_linear_damp_mode); |
1021 | |
1022 | ClassDB::bind_method(D_METHOD("set_angular_damp_mode" , "angular_damp_mode" ), &RigidBody3D::set_angular_damp_mode); |
1023 | ClassDB::bind_method(D_METHOD("get_angular_damp_mode" ), &RigidBody3D::get_angular_damp_mode); |
1024 | |
1025 | ClassDB::bind_method(D_METHOD("set_linear_damp" , "linear_damp" ), &RigidBody3D::set_linear_damp); |
1026 | ClassDB::bind_method(D_METHOD("get_linear_damp" ), &RigidBody3D::get_linear_damp); |
1027 | |
1028 | ClassDB::bind_method(D_METHOD("set_angular_damp" , "angular_damp" ), &RigidBody3D::set_angular_damp); |
1029 | ClassDB::bind_method(D_METHOD("get_angular_damp" ), &RigidBody3D::get_angular_damp); |
1030 | |
1031 | ClassDB::bind_method(D_METHOD("set_max_contacts_reported" , "amount" ), &RigidBody3D::set_max_contacts_reported); |
1032 | ClassDB::bind_method(D_METHOD("get_max_contacts_reported" ), &RigidBody3D::get_max_contacts_reported); |
1033 | ClassDB::bind_method(D_METHOD("get_contact_count" ), &RigidBody3D::get_contact_count); |
1034 | |
1035 | ClassDB::bind_method(D_METHOD("set_use_custom_integrator" , "enable" ), &RigidBody3D::set_use_custom_integrator); |
1036 | ClassDB::bind_method(D_METHOD("is_using_custom_integrator" ), &RigidBody3D::is_using_custom_integrator); |
1037 | |
1038 | ClassDB::bind_method(D_METHOD("set_contact_monitor" , "enabled" ), &RigidBody3D::set_contact_monitor); |
1039 | ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled" ), &RigidBody3D::is_contact_monitor_enabled); |
1040 | |
1041 | ClassDB::bind_method(D_METHOD("set_use_continuous_collision_detection" , "enable" ), &RigidBody3D::set_use_continuous_collision_detection); |
1042 | ClassDB::bind_method(D_METHOD("is_using_continuous_collision_detection" ), &RigidBody3D::is_using_continuous_collision_detection); |
1043 | |
1044 | ClassDB::bind_method(D_METHOD("set_axis_velocity" , "axis_velocity" ), &RigidBody3D::set_axis_velocity); |
1045 | |
1046 | ClassDB::bind_method(D_METHOD("apply_central_impulse" , "impulse" ), &RigidBody3D::apply_central_impulse); |
1047 | ClassDB::bind_method(D_METHOD("apply_impulse" , "impulse" , "position" ), &RigidBody3D::apply_impulse, Vector3()); |
1048 | ClassDB::bind_method(D_METHOD("apply_torque_impulse" , "impulse" ), &RigidBody3D::apply_torque_impulse); |
1049 | |
1050 | ClassDB::bind_method(D_METHOD("apply_central_force" , "force" ), &RigidBody3D::apply_central_force); |
1051 | ClassDB::bind_method(D_METHOD("apply_force" , "force" , "position" ), &RigidBody3D::apply_force, Vector3()); |
1052 | ClassDB::bind_method(D_METHOD("apply_torque" , "torque" ), &RigidBody3D::apply_torque); |
1053 | |
1054 | ClassDB::bind_method(D_METHOD("add_constant_central_force" , "force" ), &RigidBody3D::add_constant_central_force); |
1055 | ClassDB::bind_method(D_METHOD("add_constant_force" , "force" , "position" ), &RigidBody3D::add_constant_force, Vector3()); |
1056 | ClassDB::bind_method(D_METHOD("add_constant_torque" , "torque" ), &RigidBody3D::add_constant_torque); |
1057 | |
1058 | ClassDB::bind_method(D_METHOD("set_constant_force" , "force" ), &RigidBody3D::set_constant_force); |
1059 | ClassDB::bind_method(D_METHOD("get_constant_force" ), &RigidBody3D::get_constant_force); |
1060 | |
1061 | ClassDB::bind_method(D_METHOD("set_constant_torque" , "torque" ), &RigidBody3D::set_constant_torque); |
1062 | ClassDB::bind_method(D_METHOD("get_constant_torque" ), &RigidBody3D::get_constant_torque); |
1063 | |
1064 | ClassDB::bind_method(D_METHOD("set_sleeping" , "sleeping" ), &RigidBody3D::set_sleeping); |
1065 | ClassDB::bind_method(D_METHOD("is_sleeping" ), &RigidBody3D::is_sleeping); |
1066 | |
1067 | ClassDB::bind_method(D_METHOD("set_can_sleep" , "able_to_sleep" ), &RigidBody3D::set_can_sleep); |
1068 | ClassDB::bind_method(D_METHOD("is_able_to_sleep" ), &RigidBody3D::is_able_to_sleep); |
1069 | |
1070 | ClassDB::bind_method(D_METHOD("set_lock_rotation_enabled" , "lock_rotation" ), &RigidBody3D::set_lock_rotation_enabled); |
1071 | ClassDB::bind_method(D_METHOD("is_lock_rotation_enabled" ), &RigidBody3D::is_lock_rotation_enabled); |
1072 | |
1073 | ClassDB::bind_method(D_METHOD("set_freeze_enabled" , "freeze_mode" ), &RigidBody3D::set_freeze_enabled); |
1074 | ClassDB::bind_method(D_METHOD("is_freeze_enabled" ), &RigidBody3D::is_freeze_enabled); |
1075 | |
1076 | ClassDB::bind_method(D_METHOD("set_freeze_mode" , "freeze_mode" ), &RigidBody3D::set_freeze_mode); |
1077 | ClassDB::bind_method(D_METHOD("get_freeze_mode" ), &RigidBody3D::get_freeze_mode); |
1078 | |
1079 | ClassDB::bind_method(D_METHOD("get_colliding_bodies" ), &RigidBody3D::get_colliding_bodies); |
1080 | |
1081 | GDVIRTUAL_BIND(_integrate_forces, "state" ); |
1082 | |
1083 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass" , PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp,suffix:kg" ), "set_mass" , "get_mass" ); |
1084 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override" , PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial" ), "set_physics_material_override" , "get_physics_material_override" ); |
1085 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale" , PROPERTY_HINT_RANGE, "-128,128,0.01" ), "set_gravity_scale" , "get_gravity_scale" ); |
1086 | ADD_GROUP("Mass Distribution" , "" ); |
1087 | ADD_PROPERTY(PropertyInfo(Variant::INT, "center_of_mass_mode" , PROPERTY_HINT_ENUM, "Auto,Custom" , PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_center_of_mass_mode" , "get_center_of_mass_mode" ); |
1088 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass" , PROPERTY_HINT_RANGE, "-10,10,0.01,or_less,or_greater,suffix:m" ), "set_center_of_mass" , "get_center_of_mass" ); |
1089 | ADD_LINKED_PROPERTY("center_of_mass_mode" , "center_of_mass" ); |
1090 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inertia" , PROPERTY_HINT_RANGE, U"0,1000,0.01,or_greater,exp,suffix:kg\u22C5m\u00B2" ), "set_inertia" , "get_inertia" ); |
1091 | ADD_GROUP("Deactivation" , "" ); |
1092 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping" ), "set_sleeping" , "is_sleeping" ); |
1093 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep" ), "set_can_sleep" , "is_able_to_sleep" ); |
1094 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lock_rotation" ), "set_lock_rotation_enabled" , "is_lock_rotation_enabled" ); |
1095 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "freeze" ), "set_freeze_enabled" , "is_freeze_enabled" ); |
1096 | ADD_PROPERTY(PropertyInfo(Variant::INT, "freeze_mode" , PROPERTY_HINT_ENUM, "Static,Kinematic" ), "set_freeze_mode" , "get_freeze_mode" ); |
1097 | ADD_GROUP("Solver" , "" ); |
1098 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator" ), "set_use_custom_integrator" , "is_using_custom_integrator" ); |
1099 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "continuous_cd" ), "set_use_continuous_collision_detection" , "is_using_continuous_collision_detection" ); |
1100 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_contacts_reported" , PROPERTY_HINT_RANGE, "0,64,1,or_greater" ), "set_max_contacts_reported" , "get_max_contacts_reported" ); |
1101 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor" ), "set_contact_monitor" , "is_contact_monitor_enabled" ); |
1102 | ADD_GROUP("Linear" , "linear_" ); |
1103 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity" , PROPERTY_HINT_NONE, "suffix:m/s" ), "set_linear_velocity" , "get_linear_velocity" ); |
1104 | ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode" , PROPERTY_HINT_ENUM, "Combine,Replace" ), "set_linear_damp_mode" , "get_linear_damp_mode" ); |
1105 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp" , PROPERTY_HINT_RANGE, "0,100,0.001,or_greater" ), "set_linear_damp" , "get_linear_damp" ); |
1106 | ADD_GROUP("Angular" , "angular_" ); |
1107 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity" , PROPERTY_HINT_NONE, U"radians,suffix:\u00B0/s" ), "set_angular_velocity" , "get_angular_velocity" ); |
1108 | ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode" , PROPERTY_HINT_ENUM, "Combine,Replace" ), "set_angular_damp_mode" , "get_angular_damp_mode" ); |
1109 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp" , PROPERTY_HINT_RANGE, "0,100,0.001,or_greater" ), "set_angular_damp" , "get_angular_damp" ); |
1110 | ADD_GROUP("Constant Forces" , "constant_" ); |
1111 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_force" , PROPERTY_HINT_NONE, U"suffix:kg\u22C5m/s\u00B2 (N)" ), "set_constant_force" , "get_constant_force" ); |
1112 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_torque" , PROPERTY_HINT_NONE, U"suffix:kg\u22C5m\u00B2/s\u00B2/rad" ), "set_constant_torque" , "get_constant_torque" ); |
1113 | |
1114 | ADD_SIGNAL(MethodInfo("body_shape_entered" , PropertyInfo(Variant::RID, "body_rid" ), PropertyInfo(Variant::OBJECT, "body" , PROPERTY_HINT_RESOURCE_TYPE, "Node" ), PropertyInfo(Variant::INT, "body_shape_index" ), PropertyInfo(Variant::INT, "local_shape_index" ))); |
1115 | ADD_SIGNAL(MethodInfo("body_shape_exited" , PropertyInfo(Variant::RID, "body_rid" ), PropertyInfo(Variant::OBJECT, "body" , PROPERTY_HINT_RESOURCE_TYPE, "Node" ), PropertyInfo(Variant::INT, "body_shape_index" ), PropertyInfo(Variant::INT, "local_shape_index" ))); |
1116 | ADD_SIGNAL(MethodInfo("body_entered" , PropertyInfo(Variant::OBJECT, "body" , PROPERTY_HINT_RESOURCE_TYPE, "Node" ))); |
1117 | ADD_SIGNAL(MethodInfo("body_exited" , PropertyInfo(Variant::OBJECT, "body" , PROPERTY_HINT_RESOURCE_TYPE, "Node" ))); |
1118 | ADD_SIGNAL(MethodInfo("sleeping_state_changed" )); |
1119 | |
1120 | BIND_ENUM_CONSTANT(FREEZE_MODE_STATIC); |
1121 | BIND_ENUM_CONSTANT(FREEZE_MODE_KINEMATIC); |
1122 | |
1123 | BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO); |
1124 | BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM); |
1125 | |
1126 | BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE); |
1127 | BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE); |
1128 | } |
1129 | |
1130 | void RigidBody3D::_validate_property(PropertyInfo &p_property) const { |
1131 | if (center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM) { |
1132 | if (p_property.name == "center_of_mass" ) { |
1133 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
1134 | } |
1135 | } |
1136 | } |
1137 | |
1138 | RigidBody3D::RigidBody3D() : |
1139 | PhysicsBody3D(PhysicsServer3D::BODY_MODE_RIGID) { |
1140 | PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &RigidBody3D::_body_state_changed)); |
1141 | } |
1142 | |
1143 | RigidBody3D::~RigidBody3D() { |
1144 | if (contact_monitor) { |
1145 | memdelete(contact_monitor); |
1146 | } |
1147 | } |
1148 | |
1149 | void RigidBody3D::_reload_physics_characteristics() { |
1150 | if (physics_material_override.is_null()) { |
1151 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, 0); |
1152 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, 1); |
1153 | } else { |
1154 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce()); |
1155 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, physics_material_override->computed_friction()); |
1156 | } |
1157 | } |
1158 | |
1159 | /////////////////////////////////////// |
1160 | |
1161 | //so, if you pass 45 as limit, avoid numerical precision errors when angle is 45. |
1162 | #define FLOOR_ANGLE_THRESHOLD 0.01 |
1163 | |
1164 | bool CharacterBody3D::move_and_slide() { |
1165 | // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky |
1166 | double delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time(); |
1167 | |
1168 | for (int i = 0; i < 3; i++) { |
1169 | if (locked_axis & (1 << i)) { |
1170 | velocity[i] = 0.0; |
1171 | } |
1172 | } |
1173 | |
1174 | Transform3D gt = get_global_transform(); |
1175 | previous_position = gt.origin; |
1176 | |
1177 | Vector3 current_platform_velocity = platform_velocity; |
1178 | |
1179 | if ((collision_state.floor || collision_state.wall) && platform_rid.is_valid()) { |
1180 | bool excluded = false; |
1181 | if (collision_state.floor) { |
1182 | excluded = (platform_floor_layers & platform_layer) == 0; |
1183 | } else if (collision_state.wall) { |
1184 | excluded = (platform_wall_layers & platform_layer) == 0; |
1185 | } |
1186 | if (!excluded) { |
1187 | //this approach makes sure there is less delay between the actual body velocity and the one we saved |
1188 | PhysicsDirectBodyState3D *bs = PhysicsServer3D::get_singleton()->body_get_direct_state(platform_rid); |
1189 | if (bs) { |
1190 | Vector3 local_position = gt.origin - bs->get_transform().origin; |
1191 | current_platform_velocity = bs->get_velocity_at_local_position(local_position); |
1192 | } else { |
1193 | // Body is removed or destroyed, invalidate floor. |
1194 | current_platform_velocity = Vector3(); |
1195 | platform_rid = RID(); |
1196 | } |
1197 | } else { |
1198 | current_platform_velocity = Vector3(); |
1199 | } |
1200 | } |
1201 | |
1202 | motion_results.clear(); |
1203 | |
1204 | bool was_on_floor = collision_state.floor; |
1205 | collision_state.state = 0; |
1206 | |
1207 | last_motion = Vector3(); |
1208 | |
1209 | if (!current_platform_velocity.is_zero_approx()) { |
1210 | PhysicsServer3D::MotionParameters parameters(get_global_transform(), current_platform_velocity * delta, margin); |
1211 | parameters.recovery_as_collision = true; // Also report collisions generated only from recovery. |
1212 | |
1213 | parameters.exclude_bodies.insert(platform_rid); |
1214 | if (platform_object_id.is_valid()) { |
1215 | parameters.exclude_objects.insert(platform_object_id); |
1216 | } |
1217 | |
1218 | PhysicsServer3D::MotionResult floor_result; |
1219 | if (move_and_collide(parameters, floor_result, false, false)) { |
1220 | motion_results.push_back(floor_result); |
1221 | |
1222 | CollisionState result_state; |
1223 | _set_collision_direction(floor_result, result_state); |
1224 | } |
1225 | } |
1226 | |
1227 | if (motion_mode == MOTION_MODE_GROUNDED) { |
1228 | _move_and_slide_grounded(delta, was_on_floor); |
1229 | } else { |
1230 | _move_and_slide_floating(delta); |
1231 | } |
1232 | |
1233 | // Compute real velocity. |
1234 | real_velocity = get_position_delta() / delta; |
1235 | |
1236 | if (platform_on_leave != PLATFORM_ON_LEAVE_DO_NOTHING) { |
1237 | // Add last platform velocity when just left a moving platform. |
1238 | if (!collision_state.floor && !collision_state.wall) { |
1239 | if (platform_on_leave == PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY && current_platform_velocity.dot(up_direction) < 0) { |
1240 | current_platform_velocity = current_platform_velocity.slide(up_direction); |
1241 | } |
1242 | velocity += current_platform_velocity; |
1243 | } |
1244 | } |
1245 | |
1246 | return motion_results.size() > 0; |
1247 | } |
1248 | |
1249 | void CharacterBody3D::_move_and_slide_grounded(double p_delta, bool p_was_on_floor) { |
1250 | Vector3 motion = velocity * p_delta; |
1251 | Vector3 motion_slide_up = motion.slide(up_direction); |
1252 | Vector3 prev_floor_normal = floor_normal; |
1253 | |
1254 | platform_rid = RID(); |
1255 | platform_object_id = ObjectID(); |
1256 | platform_velocity = Vector3(); |
1257 | platform_angular_velocity = Vector3(); |
1258 | platform_ceiling_velocity = Vector3(); |
1259 | floor_normal = Vector3(); |
1260 | wall_normal = Vector3(); |
1261 | ceiling_normal = Vector3(); |
1262 | |
1263 | // No sliding on first attempt to keep floor motion stable when possible, |
1264 | // When stop on slope is enabled or when there is no up direction. |
1265 | bool sliding_enabled = !floor_stop_on_slope; |
1266 | // Constant speed can be applied only the first time sliding is enabled. |
1267 | bool can_apply_constant_speed = sliding_enabled; |
1268 | // If the platform's ceiling push down the body. |
1269 | bool apply_ceiling_velocity = false; |
1270 | bool first_slide = true; |
1271 | bool vel_dir_facing_up = velocity.dot(up_direction) > 0; |
1272 | Vector3 total_travel; |
1273 | |
1274 | for (int iteration = 0; iteration < max_slides; ++iteration) { |
1275 | PhysicsServer3D::MotionParameters parameters(get_global_transform(), motion, margin); |
1276 | parameters.max_collisions = 6; // There can be 4 collisions between 2 walls + 2 more for the floor. |
1277 | parameters.recovery_as_collision = true; // Also report collisions generated only from recovery. |
1278 | |
1279 | PhysicsServer3D::MotionResult result; |
1280 | bool collided = move_and_collide(parameters, result, false, !sliding_enabled); |
1281 | |
1282 | last_motion = result.travel; |
1283 | |
1284 | if (collided) { |
1285 | motion_results.push_back(result); |
1286 | |
1287 | CollisionState previous_state = collision_state; |
1288 | |
1289 | CollisionState result_state; |
1290 | _set_collision_direction(result, result_state); |
1291 | |
1292 | // If we hit a ceiling platform, we set the vertical velocity to at least the platform one. |
1293 | if (collision_state.ceiling && platform_ceiling_velocity != Vector3() && platform_ceiling_velocity.dot(up_direction) < 0) { |
1294 | // If ceiling sliding is on, only apply when the ceiling is flat or when the motion is upward. |
1295 | if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (ceiling_normal + up_direction).length() < 0.01) { |
1296 | apply_ceiling_velocity = true; |
1297 | Vector3 ceiling_vertical_velocity = up_direction * up_direction.dot(platform_ceiling_velocity); |
1298 | Vector3 motion_vertical_velocity = up_direction * up_direction.dot(velocity); |
1299 | if (motion_vertical_velocity.dot(up_direction) > 0 || ceiling_vertical_velocity.length_squared() > motion_vertical_velocity.length_squared()) { |
1300 | velocity = ceiling_vertical_velocity + velocity.slide(up_direction); |
1301 | } |
1302 | } |
1303 | } |
1304 | |
1305 | if (collision_state.floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01) { |
1306 | Transform3D gt = get_global_transform(); |
1307 | if (result.travel.length() <= margin + CMP_EPSILON) { |
1308 | gt.origin -= result.travel; |
1309 | } |
1310 | set_global_transform(gt); |
1311 | velocity = Vector3(); |
1312 | motion = Vector3(); |
1313 | last_motion = Vector3(); |
1314 | break; |
1315 | } |
1316 | |
1317 | if (result.remainder.is_zero_approx()) { |
1318 | motion = Vector3(); |
1319 | break; |
1320 | } |
1321 | |
1322 | // Apply regular sliding by default. |
1323 | bool apply_default_sliding = true; |
1324 | |
1325 | // Wall collision checks. |
1326 | if (result_state.wall && (motion_slide_up.dot(wall_normal) <= 0)) { |
1327 | // Move on floor only checks. |
1328 | if (floor_block_on_wall) { |
1329 | // Needs horizontal motion from current motion instead of motion_slide_up |
1330 | // to properly test the angle and avoid standing on slopes |
1331 | Vector3 horizontal_motion = motion.slide(up_direction); |
1332 | Vector3 horizontal_normal = wall_normal.slide(up_direction).normalized(); |
1333 | real_t motion_angle = Math::abs(Math::acos(-horizontal_normal.dot(horizontal_motion.normalized()))); |
1334 | |
1335 | // Avoid to move forward on a wall if floor_block_on_wall is true. |
1336 | // Applies only when the motion angle is under 90 degrees, |
1337 | // in order to avoid blocking lateral motion along a wall. |
1338 | if (motion_angle < .5 * Math_PI) { |
1339 | apply_default_sliding = false; |
1340 | if (p_was_on_floor && !vel_dir_facing_up) { |
1341 | // Cancel the motion. |
1342 | Transform3D gt = get_global_transform(); |
1343 | real_t travel_total = result.travel.length(); |
1344 | real_t cancel_dist_max = MIN(0.1, margin * 20); |
1345 | if (travel_total <= margin + CMP_EPSILON) { |
1346 | gt.origin -= result.travel; |
1347 | result.travel = Vector3(); // Cancel for constant speed computation. |
1348 | } else if (travel_total < cancel_dist_max) { // If the movement is large the body can be prevented from reaching the walls. |
1349 | gt.origin -= result.travel.slide(up_direction); |
1350 | // Keep remaining motion in sync with amount canceled. |
1351 | motion = motion.slide(up_direction); |
1352 | result.travel = Vector3(); |
1353 | } else { |
1354 | // Travel is too high to be safely canceled, we take it into account. |
1355 | result.travel = result.travel.slide(up_direction); |
1356 | motion = motion.normalized() * result.travel.length(); |
1357 | } |
1358 | set_global_transform(gt); |
1359 | // Determines if you are on the ground, and limits the possibility of climbing on the walls because of the approximations. |
1360 | _snap_on_floor(true, false); |
1361 | } else { |
1362 | // If the movement is not canceled we only keep the remaining. |
1363 | motion = result.remainder; |
1364 | } |
1365 | |
1366 | // Apply slide on forward in order to allow only lateral motion on next step. |
1367 | Vector3 forward = wall_normal.slide(up_direction).normalized(); |
1368 | motion = motion.slide(forward); |
1369 | |
1370 | // Scales the horizontal velocity according to the wall slope. |
1371 | if (vel_dir_facing_up) { |
1372 | Vector3 slide_motion = velocity.slide(result.collisions[0].normal); |
1373 | // Keeps the vertical motion from velocity and add the horizontal motion of the projection. |
1374 | velocity = up_direction * up_direction.dot(velocity) + slide_motion.slide(up_direction); |
1375 | } else { |
1376 | velocity = velocity.slide(forward); |
1377 | } |
1378 | |
1379 | // Allow only lateral motion along previous floor when already on floor. |
1380 | // Fixes slowing down when moving in diagonal against an inclined wall. |
1381 | if (p_was_on_floor && !vel_dir_facing_up && (motion.dot(up_direction) > 0.0)) { |
1382 | // Slide along the corner between the wall and previous floor. |
1383 | Vector3 floor_side = prev_floor_normal.cross(wall_normal); |
1384 | if (floor_side != Vector3()) { |
1385 | motion = floor_side * motion.dot(floor_side); |
1386 | } |
1387 | } |
1388 | |
1389 | // Stop all motion when a second wall is hit (unless sliding down or jumping), |
1390 | // in order to avoid jittering in corner cases. |
1391 | bool stop_all_motion = previous_state.wall && !vel_dir_facing_up; |
1392 | |
1393 | // Allow sliding when the body falls. |
1394 | if (!collision_state.floor && motion.dot(up_direction) < 0) { |
1395 | Vector3 slide_motion = motion.slide(wall_normal); |
1396 | // Test again to allow sliding only if the result goes downwards. |
1397 | // Fixes jittering issues at the bottom of inclined walls. |
1398 | if (slide_motion.dot(up_direction) < 0) { |
1399 | stop_all_motion = false; |
1400 | motion = slide_motion; |
1401 | } |
1402 | } |
1403 | |
1404 | if (stop_all_motion) { |
1405 | motion = Vector3(); |
1406 | velocity = Vector3(); |
1407 | } |
1408 | } |
1409 | } |
1410 | |
1411 | // Stop horizontal motion when under wall slide threshold. |
1412 | if (p_was_on_floor && (wall_min_slide_angle > 0.0) && result_state.wall) { |
1413 | Vector3 horizontal_normal = wall_normal.slide(up_direction).normalized(); |
1414 | real_t motion_angle = Math::abs(Math::acos(-horizontal_normal.dot(motion_slide_up.normalized()))); |
1415 | if (motion_angle < wall_min_slide_angle) { |
1416 | motion = up_direction * motion.dot(up_direction); |
1417 | velocity = up_direction * velocity.dot(up_direction); |
1418 | |
1419 | apply_default_sliding = false; |
1420 | } |
1421 | } |
1422 | } |
1423 | |
1424 | if (apply_default_sliding) { |
1425 | // Regular sliding, the last part of the test handle the case when you don't want to slide on the ceiling. |
1426 | if ((sliding_enabled || !collision_state.floor) && (!collision_state.ceiling || slide_on_ceiling || !vel_dir_facing_up) && !apply_ceiling_velocity) { |
1427 | const PhysicsServer3D::MotionCollision &collision = result.collisions[0]; |
1428 | |
1429 | Vector3 slide_motion = result.remainder.slide(collision.normal); |
1430 | if (collision_state.floor && !collision_state.wall && !motion_slide_up.is_zero_approx()) { |
1431 | // Slide using the intersection between the motion plane and the floor plane, |
1432 | // in order to keep the direction intact. |
1433 | real_t motion_length = slide_motion.length(); |
1434 | slide_motion = up_direction.cross(result.remainder).cross(floor_normal); |
1435 | |
1436 | // Keep the length from default slide to change speed in slopes by default, |
1437 | // when constant speed is not enabled. |
1438 | slide_motion.normalize(); |
1439 | slide_motion *= motion_length; |
1440 | } |
1441 | |
1442 | if (slide_motion.dot(velocity) > 0.0) { |
1443 | motion = slide_motion; |
1444 | } else { |
1445 | motion = Vector3(); |
1446 | } |
1447 | |
1448 | if (slide_on_ceiling && result_state.ceiling) { |
1449 | // Apply slide only in the direction of the input motion, otherwise just stop to avoid jittering when moving against a wall. |
1450 | if (vel_dir_facing_up) { |
1451 | velocity = velocity.slide(collision.normal); |
1452 | } else { |
1453 | // Avoid acceleration in slope when falling. |
1454 | velocity = up_direction * up_direction.dot(velocity); |
1455 | } |
1456 | } |
1457 | } |
1458 | // No sliding on first attempt to keep floor motion stable when possible. |
1459 | else { |
1460 | motion = result.remainder; |
1461 | if (result_state.ceiling && !slide_on_ceiling && vel_dir_facing_up) { |
1462 | velocity = velocity.slide(up_direction); |
1463 | motion = motion.slide(up_direction); |
1464 | } |
1465 | } |
1466 | } |
1467 | |
1468 | total_travel += result.travel; |
1469 | |
1470 | // Apply Constant Speed. |
1471 | if (p_was_on_floor && floor_constant_speed && can_apply_constant_speed && collision_state.floor && !motion.is_zero_approx()) { |
1472 | Vector3 travel_slide_up = total_travel.slide(up_direction); |
1473 | motion = motion.normalized() * MAX(0, (motion_slide_up.length() - travel_slide_up.length())); |
1474 | } |
1475 | } |
1476 | // When you move forward in a downward slope you don’t collide because you will be in the air. |
1477 | // This test ensures that constant speed is applied, only if the player is still on the ground after the snap is applied. |
1478 | else if (floor_constant_speed && first_slide && _on_floor_if_snapped(p_was_on_floor, vel_dir_facing_up)) { |
1479 | can_apply_constant_speed = false; |
1480 | sliding_enabled = true; |
1481 | Transform3D gt = get_global_transform(); |
1482 | gt.origin = gt.origin - result.travel; |
1483 | set_global_transform(gt); |
1484 | |
1485 | // Slide using the intersection between the motion plane and the floor plane, |
1486 | // in order to keep the direction intact. |
1487 | Vector3 motion_slide_norm = up_direction.cross(motion).cross(prev_floor_normal); |
1488 | motion_slide_norm.normalize(); |
1489 | |
1490 | motion = motion_slide_norm * (motion_slide_up.length()); |
1491 | collided = true; |
1492 | } |
1493 | |
1494 | if (!collided || motion.is_zero_approx()) { |
1495 | break; |
1496 | } |
1497 | |
1498 | can_apply_constant_speed = !can_apply_constant_speed && !sliding_enabled; |
1499 | sliding_enabled = true; |
1500 | first_slide = false; |
1501 | } |
1502 | |
1503 | _snap_on_floor(p_was_on_floor, vel_dir_facing_up); |
1504 | |
1505 | // Reset the gravity accumulation when touching the ground. |
1506 | if (collision_state.floor && !vel_dir_facing_up) { |
1507 | velocity = velocity.slide(up_direction); |
1508 | } |
1509 | } |
1510 | |
1511 | void CharacterBody3D::_move_and_slide_floating(double p_delta) { |
1512 | Vector3 motion = velocity * p_delta; |
1513 | |
1514 | platform_rid = RID(); |
1515 | platform_object_id = ObjectID(); |
1516 | floor_normal = Vector3(); |
1517 | platform_velocity = Vector3(); |
1518 | platform_angular_velocity = Vector3(); |
1519 | |
1520 | bool first_slide = true; |
1521 | for (int iteration = 0; iteration < max_slides; ++iteration) { |
1522 | PhysicsServer3D::MotionParameters parameters(get_global_transform(), motion, margin); |
1523 | parameters.recovery_as_collision = true; // Also report collisions generated only from recovery. |
1524 | |
1525 | PhysicsServer3D::MotionResult result; |
1526 | bool collided = move_and_collide(parameters, result, false, false); |
1527 | |
1528 | last_motion = result.travel; |
1529 | |
1530 | if (collided) { |
1531 | motion_results.push_back(result); |
1532 | |
1533 | CollisionState result_state; |
1534 | _set_collision_direction(result, result_state); |
1535 | |
1536 | if (result.remainder.is_zero_approx()) { |
1537 | motion = Vector3(); |
1538 | break; |
1539 | } |
1540 | |
1541 | if (wall_min_slide_angle != 0 && Math::acos(wall_normal.dot(-velocity.normalized())) < wall_min_slide_angle + FLOOR_ANGLE_THRESHOLD) { |
1542 | motion = Vector3(); |
1543 | if (result.travel.length() < margin + CMP_EPSILON) { |
1544 | Transform3D gt = get_global_transform(); |
1545 | gt.origin -= result.travel; |
1546 | set_global_transform(gt); |
1547 | } |
1548 | } else if (first_slide) { |
1549 | Vector3 motion_slide_norm = result.remainder.slide(wall_normal).normalized(); |
1550 | motion = motion_slide_norm * (motion.length() - result.travel.length()); |
1551 | } else { |
1552 | motion = result.remainder.slide(wall_normal); |
1553 | } |
1554 | |
1555 | if (motion.dot(velocity) <= 0.0) { |
1556 | motion = Vector3(); |
1557 | } |
1558 | } |
1559 | |
1560 | if (!collided || motion.is_zero_approx()) { |
1561 | break; |
1562 | } |
1563 | |
1564 | first_slide = false; |
1565 | } |
1566 | } |
1567 | |
1568 | void CharacterBody3D::apply_floor_snap() { |
1569 | if (collision_state.floor) { |
1570 | return; |
1571 | } |
1572 | |
1573 | // Snap by at least collision margin to keep floor state consistent. |
1574 | real_t length = MAX(floor_snap_length, margin); |
1575 | |
1576 | PhysicsServer3D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin); |
1577 | parameters.max_collisions = 4; |
1578 | parameters.recovery_as_collision = true; // Also report collisions generated only from recovery. |
1579 | parameters.collide_separation_ray = true; |
1580 | |
1581 | PhysicsServer3D::MotionResult result; |
1582 | if (move_and_collide(parameters, result, true, false)) { |
1583 | CollisionState result_state; |
1584 | // Apply direction for floor only. |
1585 | _set_collision_direction(result, result_state, CollisionState(true, false, false)); |
1586 | |
1587 | if (result_state.floor) { |
1588 | if (floor_stop_on_slope) { |
1589 | // move and collide may stray the object a bit because of pre un-stucking, |
1590 | // so only ensure that motion happens on floor direction in this case. |
1591 | if (result.travel.length() > margin) { |
1592 | result.travel = up_direction * up_direction.dot(result.travel); |
1593 | } else { |
1594 | result.travel = Vector3(); |
1595 | } |
1596 | } |
1597 | |
1598 | parameters.from.origin += result.travel; |
1599 | set_global_transform(parameters.from); |
1600 | } |
1601 | } |
1602 | } |
1603 | |
1604 | void CharacterBody3D::_snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up) { |
1605 | if (collision_state.floor || !p_was_on_floor || p_vel_dir_facing_up) { |
1606 | return; |
1607 | } |
1608 | |
1609 | apply_floor_snap(); |
1610 | } |
1611 | |
1612 | bool CharacterBody3D::_on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up) { |
1613 | if (up_direction == Vector3() || collision_state.floor || !p_was_on_floor || p_vel_dir_facing_up) { |
1614 | return false; |
1615 | } |
1616 | |
1617 | // Snap by at least collision margin to keep floor state consistent. |
1618 | real_t length = MAX(floor_snap_length, margin); |
1619 | |
1620 | PhysicsServer3D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin); |
1621 | parameters.max_collisions = 4; |
1622 | parameters.recovery_as_collision = true; // Also report collisions generated only from recovery. |
1623 | parameters.collide_separation_ray = true; |
1624 | |
1625 | PhysicsServer3D::MotionResult result; |
1626 | if (move_and_collide(parameters, result, true, false)) { |
1627 | CollisionState result_state; |
1628 | // Don't apply direction for any type. |
1629 | _set_collision_direction(result, result_state, CollisionState()); |
1630 | |
1631 | return result_state.floor; |
1632 | } |
1633 | |
1634 | return false; |
1635 | } |
1636 | |
1637 | void CharacterBody3D::_set_collision_direction(const PhysicsServer3D::MotionResult &p_result, CollisionState &r_state, CollisionState p_apply_state) { |
1638 | r_state.state = 0; |
1639 | |
1640 | real_t wall_depth = -1.0; |
1641 | real_t floor_depth = -1.0; |
1642 | |
1643 | bool was_on_wall = collision_state.wall; |
1644 | Vector3 prev_wall_normal = wall_normal; |
1645 | int wall_collision_count = 0; |
1646 | Vector3 combined_wall_normal; |
1647 | Vector3 tmp_wall_col; // Avoid duplicate on average calculation. |
1648 | |
1649 | for (int i = p_result.collision_count - 1; i >= 0; i--) { |
1650 | const PhysicsServer3D::MotionCollision &collision = p_result.collisions[i]; |
1651 | |
1652 | if (motion_mode == MOTION_MODE_GROUNDED) { |
1653 | // Check if any collision is floor. |
1654 | real_t floor_angle = collision.get_angle(up_direction); |
1655 | if (floor_angle <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { |
1656 | r_state.floor = true; |
1657 | if (p_apply_state.floor && collision.depth > floor_depth) { |
1658 | collision_state.floor = true; |
1659 | floor_normal = collision.normal; |
1660 | floor_depth = collision.depth; |
1661 | _set_platform_data(collision); |
1662 | } |
1663 | continue; |
1664 | } |
1665 | |
1666 | // Check if any collision is ceiling. |
1667 | real_t ceiling_angle = collision.get_angle(-up_direction); |
1668 | if (ceiling_angle <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { |
1669 | r_state.ceiling = true; |
1670 | if (p_apply_state.ceiling) { |
1671 | platform_ceiling_velocity = collision.collider_velocity; |
1672 | ceiling_normal = collision.normal; |
1673 | collision_state.ceiling = true; |
1674 | } |
1675 | continue; |
1676 | } |
1677 | } |
1678 | |
1679 | // Collision is wall by default. |
1680 | r_state.wall = true; |
1681 | |
1682 | if (p_apply_state.wall && collision.depth > wall_depth) { |
1683 | collision_state.wall = true; |
1684 | wall_depth = collision.depth; |
1685 | wall_normal = collision.normal; |
1686 | |
1687 | // Don't apply wall velocity when the collider is a CharacterBody3D. |
1688 | if (Object::cast_to<CharacterBody3D>(ObjectDB::get_instance(collision.collider_id)) == nullptr) { |
1689 | _set_platform_data(collision); |
1690 | } |
1691 | } |
1692 | |
1693 | // Collect normal for calculating average. |
1694 | if (!collision.normal.is_equal_approx(tmp_wall_col)) { |
1695 | tmp_wall_col = collision.normal; |
1696 | combined_wall_normal += collision.normal; |
1697 | wall_collision_count++; |
1698 | } |
1699 | } |
1700 | |
1701 | if (r_state.wall) { |
1702 | if (wall_collision_count > 1 && !r_state.floor) { |
1703 | // Check if wall normals cancel out to floor support. |
1704 | if (!r_state.floor && motion_mode == MOTION_MODE_GROUNDED) { |
1705 | combined_wall_normal.normalize(); |
1706 | real_t floor_angle = Math::acos(combined_wall_normal.dot(up_direction)); |
1707 | if (floor_angle <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { |
1708 | r_state.floor = true; |
1709 | r_state.wall = false; |
1710 | if (p_apply_state.floor) { |
1711 | collision_state.floor = true; |
1712 | floor_normal = combined_wall_normal; |
1713 | } |
1714 | if (p_apply_state.wall) { |
1715 | collision_state.wall = was_on_wall; |
1716 | wall_normal = prev_wall_normal; |
1717 | } |
1718 | return; |
1719 | } |
1720 | } |
1721 | } |
1722 | } |
1723 | } |
1724 | |
1725 | void CharacterBody3D::_set_platform_data(const PhysicsServer3D::MotionCollision &p_collision) { |
1726 | platform_rid = p_collision.collider; |
1727 | platform_object_id = p_collision.collider_id; |
1728 | platform_velocity = p_collision.collider_velocity; |
1729 | platform_angular_velocity = p_collision.collider_angular_velocity; |
1730 | platform_layer = PhysicsServer3D::get_singleton()->body_get_collision_layer(platform_rid); |
1731 | } |
1732 | |
1733 | void CharacterBody3D::set_safe_margin(real_t p_margin) { |
1734 | margin = p_margin; |
1735 | } |
1736 | |
1737 | real_t CharacterBody3D::get_safe_margin() const { |
1738 | return margin; |
1739 | } |
1740 | |
1741 | const Vector3 &CharacterBody3D::get_velocity() const { |
1742 | return velocity; |
1743 | } |
1744 | |
1745 | void CharacterBody3D::set_velocity(const Vector3 &p_velocity) { |
1746 | velocity = p_velocity; |
1747 | } |
1748 | |
1749 | bool CharacterBody3D::is_on_floor() const { |
1750 | return collision_state.floor; |
1751 | } |
1752 | |
1753 | bool CharacterBody3D::is_on_floor_only() const { |
1754 | return collision_state.floor && !collision_state.wall && !collision_state.ceiling; |
1755 | } |
1756 | |
1757 | bool CharacterBody3D::is_on_wall() const { |
1758 | return collision_state.wall; |
1759 | } |
1760 | |
1761 | bool CharacterBody3D::is_on_wall_only() const { |
1762 | return collision_state.wall && !collision_state.floor && !collision_state.ceiling; |
1763 | } |
1764 | |
1765 | bool CharacterBody3D::is_on_ceiling() const { |
1766 | return collision_state.ceiling; |
1767 | } |
1768 | |
1769 | bool CharacterBody3D::is_on_ceiling_only() const { |
1770 | return collision_state.ceiling && !collision_state.floor && !collision_state.wall; |
1771 | } |
1772 | |
1773 | const Vector3 &CharacterBody3D::get_floor_normal() const { |
1774 | return floor_normal; |
1775 | } |
1776 | |
1777 | const Vector3 &CharacterBody3D::get_wall_normal() const { |
1778 | return wall_normal; |
1779 | } |
1780 | |
1781 | const Vector3 &CharacterBody3D::get_last_motion() const { |
1782 | return last_motion; |
1783 | } |
1784 | |
1785 | Vector3 CharacterBody3D::get_position_delta() const { |
1786 | return get_global_transform().origin - previous_position; |
1787 | } |
1788 | |
1789 | const Vector3 &CharacterBody3D::get_real_velocity() const { |
1790 | return real_velocity; |
1791 | } |
1792 | |
1793 | real_t CharacterBody3D::get_floor_angle(const Vector3 &p_up_direction) const { |
1794 | ERR_FAIL_COND_V(p_up_direction == Vector3(), 0); |
1795 | return Math::acos(floor_normal.dot(p_up_direction)); |
1796 | } |
1797 | |
1798 | const Vector3 &CharacterBody3D::get_platform_velocity() const { |
1799 | return platform_velocity; |
1800 | } |
1801 | |
1802 | const Vector3 &CharacterBody3D::get_platform_angular_velocity() const { |
1803 | return platform_angular_velocity; |
1804 | } |
1805 | |
1806 | Vector3 CharacterBody3D::get_linear_velocity() const { |
1807 | return get_real_velocity(); |
1808 | } |
1809 | |
1810 | int CharacterBody3D::get_slide_collision_count() const { |
1811 | return motion_results.size(); |
1812 | } |
1813 | |
1814 | PhysicsServer3D::MotionResult CharacterBody3D::get_slide_collision(int p_bounce) const { |
1815 | ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), PhysicsServer3D::MotionResult()); |
1816 | return motion_results[p_bounce]; |
1817 | } |
1818 | |
1819 | Ref<KinematicCollision3D> CharacterBody3D::_get_slide_collision(int p_bounce) { |
1820 | ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), Ref<KinematicCollision3D>()); |
1821 | if (p_bounce >= slide_colliders.size()) { |
1822 | slide_colliders.resize(p_bounce + 1); |
1823 | } |
1824 | |
1825 | // Create a new instance when the cached reference is invalid or still in use in script. |
1826 | if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->get_reference_count() > 1) { |
1827 | slide_colliders.write[p_bounce].instantiate(); |
1828 | slide_colliders.write[p_bounce]->owner = this; |
1829 | } |
1830 | |
1831 | slide_colliders.write[p_bounce]->result = motion_results[p_bounce]; |
1832 | return slide_colliders[p_bounce]; |
1833 | } |
1834 | |
1835 | Ref<KinematicCollision3D> CharacterBody3D::_get_last_slide_collision() { |
1836 | if (motion_results.size() == 0) { |
1837 | return Ref<KinematicCollision3D>(); |
1838 | } |
1839 | return _get_slide_collision(motion_results.size() - 1); |
1840 | } |
1841 | |
1842 | bool CharacterBody3D::is_floor_stop_on_slope_enabled() const { |
1843 | return floor_stop_on_slope; |
1844 | } |
1845 | |
1846 | void CharacterBody3D::set_floor_stop_on_slope_enabled(bool p_enabled) { |
1847 | floor_stop_on_slope = p_enabled; |
1848 | } |
1849 | |
1850 | bool CharacterBody3D::is_floor_constant_speed_enabled() const { |
1851 | return floor_constant_speed; |
1852 | } |
1853 | |
1854 | void CharacterBody3D::set_floor_constant_speed_enabled(bool p_enabled) { |
1855 | floor_constant_speed = p_enabled; |
1856 | } |
1857 | |
1858 | bool CharacterBody3D::is_floor_block_on_wall_enabled() const { |
1859 | return floor_block_on_wall; |
1860 | } |
1861 | |
1862 | void CharacterBody3D::set_floor_block_on_wall_enabled(bool p_enabled) { |
1863 | floor_block_on_wall = p_enabled; |
1864 | } |
1865 | |
1866 | bool CharacterBody3D::is_slide_on_ceiling_enabled() const { |
1867 | return slide_on_ceiling; |
1868 | } |
1869 | |
1870 | void CharacterBody3D::set_slide_on_ceiling_enabled(bool p_enabled) { |
1871 | slide_on_ceiling = p_enabled; |
1872 | } |
1873 | |
1874 | uint32_t CharacterBody3D::get_platform_floor_layers() const { |
1875 | return platform_floor_layers; |
1876 | } |
1877 | |
1878 | void CharacterBody3D::set_platform_floor_layers(uint32_t p_exclude_layers) { |
1879 | platform_floor_layers = p_exclude_layers; |
1880 | } |
1881 | |
1882 | uint32_t CharacterBody3D::get_platform_wall_layers() const { |
1883 | return platform_wall_layers; |
1884 | } |
1885 | |
1886 | void CharacterBody3D::set_platform_wall_layers(uint32_t p_exclude_layers) { |
1887 | platform_wall_layers = p_exclude_layers; |
1888 | } |
1889 | |
1890 | void CharacterBody3D::set_motion_mode(MotionMode p_mode) { |
1891 | motion_mode = p_mode; |
1892 | } |
1893 | |
1894 | CharacterBody3D::MotionMode CharacterBody3D::get_motion_mode() const { |
1895 | return motion_mode; |
1896 | } |
1897 | |
1898 | void CharacterBody3D::set_platform_on_leave(PlatformOnLeave p_on_leave_apply_velocity) { |
1899 | platform_on_leave = p_on_leave_apply_velocity; |
1900 | } |
1901 | |
1902 | CharacterBody3D::PlatformOnLeave CharacterBody3D::get_platform_on_leave() const { |
1903 | return platform_on_leave; |
1904 | } |
1905 | |
1906 | int CharacterBody3D::get_max_slides() const { |
1907 | return max_slides; |
1908 | } |
1909 | |
1910 | void CharacterBody3D::set_max_slides(int p_max_slides) { |
1911 | ERR_FAIL_COND(p_max_slides < 1); |
1912 | max_slides = p_max_slides; |
1913 | } |
1914 | |
1915 | real_t CharacterBody3D::get_floor_max_angle() const { |
1916 | return floor_max_angle; |
1917 | } |
1918 | |
1919 | void CharacterBody3D::set_floor_max_angle(real_t p_radians) { |
1920 | floor_max_angle = p_radians; |
1921 | } |
1922 | |
1923 | real_t CharacterBody3D::get_floor_snap_length() { |
1924 | return floor_snap_length; |
1925 | } |
1926 | |
1927 | void CharacterBody3D::set_floor_snap_length(real_t p_floor_snap_length) { |
1928 | ERR_FAIL_COND(p_floor_snap_length < 0); |
1929 | floor_snap_length = p_floor_snap_length; |
1930 | } |
1931 | |
1932 | real_t CharacterBody3D::get_wall_min_slide_angle() const { |
1933 | return wall_min_slide_angle; |
1934 | } |
1935 | |
1936 | void CharacterBody3D::set_wall_min_slide_angle(real_t p_radians) { |
1937 | wall_min_slide_angle = p_radians; |
1938 | } |
1939 | |
1940 | const Vector3 &CharacterBody3D::get_up_direction() const { |
1941 | return up_direction; |
1942 | } |
1943 | |
1944 | void CharacterBody3D::set_up_direction(const Vector3 &p_up_direction) { |
1945 | ERR_FAIL_COND_MSG(p_up_direction == Vector3(), "up_direction can't be equal to Vector3.ZERO, consider using Floating motion mode instead." ); |
1946 | up_direction = p_up_direction.normalized(); |
1947 | } |
1948 | |
1949 | void CharacterBody3D::_notification(int p_what) { |
1950 | switch (p_what) { |
1951 | case NOTIFICATION_ENTER_TREE: { |
1952 | // Reset move_and_slide() data. |
1953 | collision_state.state = 0; |
1954 | platform_rid = RID(); |
1955 | platform_object_id = ObjectID(); |
1956 | motion_results.clear(); |
1957 | platform_velocity = Vector3(); |
1958 | platform_angular_velocity = Vector3(); |
1959 | } break; |
1960 | } |
1961 | } |
1962 | |
1963 | void CharacterBody3D::_bind_methods() { |
1964 | ClassDB::bind_method(D_METHOD("move_and_slide" ), &CharacterBody3D::move_and_slide); |
1965 | ClassDB::bind_method(D_METHOD("apply_floor_snap" ), &CharacterBody3D::apply_floor_snap); |
1966 | |
1967 | ClassDB::bind_method(D_METHOD("set_velocity" , "velocity" ), &CharacterBody3D::set_velocity); |
1968 | ClassDB::bind_method(D_METHOD("get_velocity" ), &CharacterBody3D::get_velocity); |
1969 | |
1970 | ClassDB::bind_method(D_METHOD("set_safe_margin" , "margin" ), &CharacterBody3D::set_safe_margin); |
1971 | ClassDB::bind_method(D_METHOD("get_safe_margin" ), &CharacterBody3D::get_safe_margin); |
1972 | ClassDB::bind_method(D_METHOD("is_floor_stop_on_slope_enabled" ), &CharacterBody3D::is_floor_stop_on_slope_enabled); |
1973 | ClassDB::bind_method(D_METHOD("set_floor_stop_on_slope_enabled" , "enabled" ), &CharacterBody3D::set_floor_stop_on_slope_enabled); |
1974 | ClassDB::bind_method(D_METHOD("set_floor_constant_speed_enabled" , "enabled" ), &CharacterBody3D::set_floor_constant_speed_enabled); |
1975 | ClassDB::bind_method(D_METHOD("is_floor_constant_speed_enabled" ), &CharacterBody3D::is_floor_constant_speed_enabled); |
1976 | ClassDB::bind_method(D_METHOD("set_floor_block_on_wall_enabled" , "enabled" ), &CharacterBody3D::set_floor_block_on_wall_enabled); |
1977 | ClassDB::bind_method(D_METHOD("is_floor_block_on_wall_enabled" ), &CharacterBody3D::is_floor_block_on_wall_enabled); |
1978 | ClassDB::bind_method(D_METHOD("set_slide_on_ceiling_enabled" , "enabled" ), &CharacterBody3D::set_slide_on_ceiling_enabled); |
1979 | ClassDB::bind_method(D_METHOD("is_slide_on_ceiling_enabled" ), &CharacterBody3D::is_slide_on_ceiling_enabled); |
1980 | |
1981 | ClassDB::bind_method(D_METHOD("set_platform_floor_layers" , "exclude_layer" ), &CharacterBody3D::set_platform_floor_layers); |
1982 | ClassDB::bind_method(D_METHOD("get_platform_floor_layers" ), &CharacterBody3D::get_platform_floor_layers); |
1983 | ClassDB::bind_method(D_METHOD("set_platform_wall_layers" , "exclude_layer" ), &CharacterBody3D::set_platform_wall_layers); |
1984 | ClassDB::bind_method(D_METHOD("get_platform_wall_layers" ), &CharacterBody3D::get_platform_wall_layers); |
1985 | |
1986 | ClassDB::bind_method(D_METHOD("get_max_slides" ), &CharacterBody3D::get_max_slides); |
1987 | ClassDB::bind_method(D_METHOD("set_max_slides" , "max_slides" ), &CharacterBody3D::set_max_slides); |
1988 | ClassDB::bind_method(D_METHOD("get_floor_max_angle" ), &CharacterBody3D::get_floor_max_angle); |
1989 | ClassDB::bind_method(D_METHOD("set_floor_max_angle" , "radians" ), &CharacterBody3D::set_floor_max_angle); |
1990 | ClassDB::bind_method(D_METHOD("get_floor_snap_length" ), &CharacterBody3D::get_floor_snap_length); |
1991 | ClassDB::bind_method(D_METHOD("set_floor_snap_length" , "floor_snap_length" ), &CharacterBody3D::set_floor_snap_length); |
1992 | ClassDB::bind_method(D_METHOD("get_wall_min_slide_angle" ), &CharacterBody3D::get_wall_min_slide_angle); |
1993 | ClassDB::bind_method(D_METHOD("set_wall_min_slide_angle" , "radians" ), &CharacterBody3D::set_wall_min_slide_angle); |
1994 | ClassDB::bind_method(D_METHOD("get_up_direction" ), &CharacterBody3D::get_up_direction); |
1995 | ClassDB::bind_method(D_METHOD("set_up_direction" , "up_direction" ), &CharacterBody3D::set_up_direction); |
1996 | ClassDB::bind_method(D_METHOD("set_motion_mode" , "mode" ), &CharacterBody3D::set_motion_mode); |
1997 | ClassDB::bind_method(D_METHOD("get_motion_mode" ), &CharacterBody3D::get_motion_mode); |
1998 | ClassDB::bind_method(D_METHOD("set_platform_on_leave" , "on_leave_apply_velocity" ), &CharacterBody3D::set_platform_on_leave); |
1999 | ClassDB::bind_method(D_METHOD("get_platform_on_leave" ), &CharacterBody3D::get_platform_on_leave); |
2000 | |
2001 | ClassDB::bind_method(D_METHOD("is_on_floor" ), &CharacterBody3D::is_on_floor); |
2002 | ClassDB::bind_method(D_METHOD("is_on_floor_only" ), &CharacterBody3D::is_on_floor_only); |
2003 | ClassDB::bind_method(D_METHOD("is_on_ceiling" ), &CharacterBody3D::is_on_ceiling); |
2004 | ClassDB::bind_method(D_METHOD("is_on_ceiling_only" ), &CharacterBody3D::is_on_ceiling_only); |
2005 | ClassDB::bind_method(D_METHOD("is_on_wall" ), &CharacterBody3D::is_on_wall); |
2006 | ClassDB::bind_method(D_METHOD("is_on_wall_only" ), &CharacterBody3D::is_on_wall_only); |
2007 | ClassDB::bind_method(D_METHOD("get_floor_normal" ), &CharacterBody3D::get_floor_normal); |
2008 | ClassDB::bind_method(D_METHOD("get_wall_normal" ), &CharacterBody3D::get_wall_normal); |
2009 | ClassDB::bind_method(D_METHOD("get_last_motion" ), &CharacterBody3D::get_last_motion); |
2010 | ClassDB::bind_method(D_METHOD("get_position_delta" ), &CharacterBody3D::get_position_delta); |
2011 | ClassDB::bind_method(D_METHOD("get_real_velocity" ), &CharacterBody3D::get_real_velocity); |
2012 | ClassDB::bind_method(D_METHOD("get_floor_angle" , "up_direction" ), &CharacterBody3D::get_floor_angle, DEFVAL(Vector3(0.0, 1.0, 0.0))); |
2013 | ClassDB::bind_method(D_METHOD("get_platform_velocity" ), &CharacterBody3D::get_platform_velocity); |
2014 | ClassDB::bind_method(D_METHOD("get_platform_angular_velocity" ), &CharacterBody3D::get_platform_angular_velocity); |
2015 | ClassDB::bind_method(D_METHOD("get_slide_collision_count" ), &CharacterBody3D::get_slide_collision_count); |
2016 | ClassDB::bind_method(D_METHOD("get_slide_collision" , "slide_idx" ), &CharacterBody3D::_get_slide_collision); |
2017 | ClassDB::bind_method(D_METHOD("get_last_slide_collision" ), &CharacterBody3D::_get_last_slide_collision); |
2018 | |
2019 | ADD_PROPERTY(PropertyInfo(Variant::INT, "motion_mode" , PROPERTY_HINT_ENUM, "Grounded,Floating" , PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_motion_mode" , "get_motion_mode" ); |
2020 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "up_direction" ), "set_up_direction" , "get_up_direction" ); |
2021 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slide_on_ceiling" ), "set_slide_on_ceiling_enabled" , "is_slide_on_ceiling_enabled" ); |
2022 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity" , PROPERTY_HINT_NONE, "suffix:m/s" , PROPERTY_USAGE_NO_EDITOR), "set_velocity" , "get_velocity" ); |
2023 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_slides" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_max_slides" , "get_max_slides" ); |
2024 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wall_min_slide_angle" , PROPERTY_HINT_RANGE, "0,180,0.1,radians" , PROPERTY_USAGE_DEFAULT), "set_wall_min_slide_angle" , "get_wall_min_slide_angle" ); |
2025 | |
2026 | ADD_GROUP("Floor" , "floor_" ); |
2027 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_stop_on_slope" ), "set_floor_stop_on_slope_enabled" , "is_floor_stop_on_slope_enabled" ); |
2028 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_constant_speed" ), "set_floor_constant_speed_enabled" , "is_floor_constant_speed_enabled" ); |
2029 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_block_on_wall" ), "set_floor_block_on_wall_enabled" , "is_floor_block_on_wall_enabled" ); |
2030 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "floor_max_angle" , PROPERTY_HINT_RANGE, "0,180,0.1,radians" ), "set_floor_max_angle" , "get_floor_max_angle" ); |
2031 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "floor_snap_length" , PROPERTY_HINT_RANGE, "0,1,0.01,or_greater,suffix:m" ), "set_floor_snap_length" , "get_floor_snap_length" ); |
2032 | |
2033 | ADD_GROUP("Moving Platform" , "platform_" ); |
2034 | ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_on_leave" , PROPERTY_HINT_ENUM, "Add Velocity,Add Upward Velocity,Do Nothing" , PROPERTY_USAGE_DEFAULT), "set_platform_on_leave" , "get_platform_on_leave" ); |
2035 | ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_floor_layers" , PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_platform_floor_layers" , "get_platform_floor_layers" ); |
2036 | ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_wall_layers" , PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_platform_wall_layers" , "get_platform_wall_layers" ); |
2037 | |
2038 | ADD_GROUP("Collision" , "" ); |
2039 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "safe_margin" , PROPERTY_HINT_RANGE, "0.001,256,0.001,suffix:m" ), "set_safe_margin" , "get_safe_margin" ); |
2040 | |
2041 | BIND_ENUM_CONSTANT(MOTION_MODE_GROUNDED); |
2042 | BIND_ENUM_CONSTANT(MOTION_MODE_FLOATING); |
2043 | |
2044 | BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_VELOCITY); |
2045 | BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY); |
2046 | BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_DO_NOTHING); |
2047 | } |
2048 | |
2049 | void CharacterBody3D::_validate_property(PropertyInfo &p_property) const { |
2050 | if (motion_mode == MOTION_MODE_FLOATING) { |
2051 | if (p_property.name.begins_with("floor_" ) || p_property.name == "up_direction" || p_property.name == "slide_on_ceiling" ) { |
2052 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
2053 | } |
2054 | } |
2055 | } |
2056 | |
2057 | CharacterBody3D::CharacterBody3D() : |
2058 | PhysicsBody3D(PhysicsServer3D::BODY_MODE_KINEMATIC) { |
2059 | } |
2060 | |
2061 | CharacterBody3D::~CharacterBody3D() { |
2062 | for (int i = 0; i < slide_colliders.size(); i++) { |
2063 | if (slide_colliders[i].is_valid()) { |
2064 | slide_colliders.write[i]->owner = nullptr; |
2065 | } |
2066 | } |
2067 | } |
2068 | |
2069 | /////////////////////////////////////// |
2070 | |
2071 | Vector3 KinematicCollision3D::get_travel() const { |
2072 | return result.travel; |
2073 | } |
2074 | |
2075 | Vector3 KinematicCollision3D::get_remainder() const { |
2076 | return result.remainder; |
2077 | } |
2078 | |
2079 | int KinematicCollision3D::get_collision_count() const { |
2080 | return result.collision_count; |
2081 | } |
2082 | |
2083 | real_t KinematicCollision3D::get_depth() const { |
2084 | return result.collision_depth; |
2085 | } |
2086 | |
2087 | Vector3 KinematicCollision3D::get_position(int p_collision_index) const { |
2088 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3()); |
2089 | return result.collisions[p_collision_index].position; |
2090 | } |
2091 | |
2092 | Vector3 KinematicCollision3D::get_normal(int p_collision_index) const { |
2093 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3()); |
2094 | return result.collisions[p_collision_index].normal; |
2095 | } |
2096 | |
2097 | real_t KinematicCollision3D::get_angle(int p_collision_index, const Vector3 &p_up_direction) const { |
2098 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0.0); |
2099 | ERR_FAIL_COND_V(p_up_direction == Vector3(), 0); |
2100 | return result.collisions[p_collision_index].get_angle(p_up_direction); |
2101 | } |
2102 | |
2103 | Object *KinematicCollision3D::get_local_shape(int p_collision_index) const { |
2104 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr); |
2105 | if (!owner) { |
2106 | return nullptr; |
2107 | } |
2108 | uint32_t ownerid = owner->shape_find_owner(result.collisions[p_collision_index].local_shape); |
2109 | return owner->shape_owner_get_owner(ownerid); |
2110 | } |
2111 | |
2112 | Object *KinematicCollision3D::get_collider(int p_collision_index) const { |
2113 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr); |
2114 | if (result.collisions[p_collision_index].collider_id.is_valid()) { |
2115 | return ObjectDB::get_instance(result.collisions[p_collision_index].collider_id); |
2116 | } |
2117 | |
2118 | return nullptr; |
2119 | } |
2120 | |
2121 | ObjectID KinematicCollision3D::get_collider_id(int p_collision_index) const { |
2122 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, ObjectID()); |
2123 | return result.collisions[p_collision_index].collider_id; |
2124 | } |
2125 | |
2126 | RID KinematicCollision3D::get_collider_rid(int p_collision_index) const { |
2127 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, RID()); |
2128 | return result.collisions[p_collision_index].collider; |
2129 | } |
2130 | |
2131 | Object *KinematicCollision3D::get_collider_shape(int p_collision_index) const { |
2132 | Object *collider = get_collider(p_collision_index); |
2133 | if (collider) { |
2134 | CollisionObject3D *obj2d = Object::cast_to<CollisionObject3D>(collider); |
2135 | if (obj2d) { |
2136 | uint32_t ownerid = obj2d->shape_find_owner(result.collisions[p_collision_index].collider_shape); |
2137 | return obj2d->shape_owner_get_owner(ownerid); |
2138 | } |
2139 | } |
2140 | |
2141 | return nullptr; |
2142 | } |
2143 | |
2144 | int KinematicCollision3D::get_collider_shape_index(int p_collision_index) const { |
2145 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0); |
2146 | return result.collisions[p_collision_index].collider_shape; |
2147 | } |
2148 | |
2149 | Vector3 KinematicCollision3D::get_collider_velocity(int p_collision_index) const { |
2150 | ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3()); |
2151 | return result.collisions[p_collision_index].collider_velocity; |
2152 | } |
2153 | |
2154 | void KinematicCollision3D::_bind_methods() { |
2155 | ClassDB::bind_method(D_METHOD("get_travel" ), &KinematicCollision3D::get_travel); |
2156 | ClassDB::bind_method(D_METHOD("get_remainder" ), &KinematicCollision3D::get_remainder); |
2157 | ClassDB::bind_method(D_METHOD("get_depth" ), &KinematicCollision3D::get_depth); |
2158 | ClassDB::bind_method(D_METHOD("get_collision_count" ), &KinematicCollision3D::get_collision_count); |
2159 | ClassDB::bind_method(D_METHOD("get_position" , "collision_index" ), &KinematicCollision3D::get_position, DEFVAL(0)); |
2160 | ClassDB::bind_method(D_METHOD("get_normal" , "collision_index" ), &KinematicCollision3D::get_normal, DEFVAL(0)); |
2161 | ClassDB::bind_method(D_METHOD("get_angle" , "collision_index" , "up_direction" ), &KinematicCollision3D::get_angle, DEFVAL(0), DEFVAL(Vector3(0.0, 1.0, 0.0))); |
2162 | ClassDB::bind_method(D_METHOD("get_local_shape" , "collision_index" ), &KinematicCollision3D::get_local_shape, DEFVAL(0)); |
2163 | ClassDB::bind_method(D_METHOD("get_collider" , "collision_index" ), &KinematicCollision3D::get_collider, DEFVAL(0)); |
2164 | ClassDB::bind_method(D_METHOD("get_collider_id" , "collision_index" ), &KinematicCollision3D::get_collider_id, DEFVAL(0)); |
2165 | ClassDB::bind_method(D_METHOD("get_collider_rid" , "collision_index" ), &KinematicCollision3D::get_collider_rid, DEFVAL(0)); |
2166 | ClassDB::bind_method(D_METHOD("get_collider_shape" , "collision_index" ), &KinematicCollision3D::get_collider_shape, DEFVAL(0)); |
2167 | ClassDB::bind_method(D_METHOD("get_collider_shape_index" , "collision_index" ), &KinematicCollision3D::get_collider_shape_index, DEFVAL(0)); |
2168 | ClassDB::bind_method(D_METHOD("get_collider_velocity" , "collision_index" ), &KinematicCollision3D::get_collider_velocity, DEFVAL(0)); |
2169 | } |
2170 | |
2171 | /////////////////////////////////////// |
2172 | |
2173 | bool PhysicalBone3D::JointData::_set(const StringName &p_name, const Variant &p_value, RID j) { |
2174 | return false; |
2175 | } |
2176 | |
2177 | bool PhysicalBone3D::JointData::_get(const StringName &p_name, Variant &r_ret) const { |
2178 | return false; |
2179 | } |
2180 | |
2181 | void PhysicalBone3D::JointData::_get_property_list(List<PropertyInfo> *p_list) const { |
2182 | } |
2183 | |
2184 | void PhysicalBone3D::apply_central_impulse(const Vector3 &p_impulse) { |
2185 | PhysicsServer3D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse); |
2186 | } |
2187 | |
2188 | void PhysicalBone3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) { |
2189 | PhysicsServer3D::get_singleton()->body_apply_impulse(get_rid(), p_impulse, p_position); |
2190 | } |
2191 | |
2192 | void PhysicalBone3D::set_linear_velocity(const Vector3 &p_velocity) { |
2193 | linear_velocity = p_velocity; |
2194 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity); |
2195 | } |
2196 | |
2197 | Vector3 PhysicalBone3D::get_linear_velocity() const { |
2198 | return linear_velocity; |
2199 | } |
2200 | |
2201 | void PhysicalBone3D::set_angular_velocity(const Vector3 &p_velocity) { |
2202 | angular_velocity = p_velocity; |
2203 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity); |
2204 | } |
2205 | |
2206 | Vector3 PhysicalBone3D::get_angular_velocity() const { |
2207 | return angular_velocity; |
2208 | } |
2209 | |
2210 | void PhysicalBone3D::set_use_custom_integrator(bool p_enable) { |
2211 | if (custom_integrator == p_enable) { |
2212 | return; |
2213 | } |
2214 | |
2215 | custom_integrator = p_enable; |
2216 | PhysicsServer3D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable); |
2217 | } |
2218 | |
2219 | bool PhysicalBone3D::is_using_custom_integrator() { |
2220 | return custom_integrator; |
2221 | } |
2222 | |
2223 | void PhysicalBone3D::reset_physics_simulation_state() { |
2224 | if (simulate_physics) { |
2225 | _start_physics_simulation(); |
2226 | } else { |
2227 | _stop_physics_simulation(); |
2228 | } |
2229 | } |
2230 | |
2231 | void PhysicalBone3D::reset_to_rest_position() { |
2232 | if (parent_skeleton) { |
2233 | if (-1 == bone_id) { |
2234 | set_global_transform(parent_skeleton->get_global_transform() * body_offset); |
2235 | } else { |
2236 | set_global_transform(parent_skeleton->get_global_transform() * parent_skeleton->get_bone_global_pose(bone_id) * body_offset); |
2237 | } |
2238 | } |
2239 | } |
2240 | |
2241 | bool PhysicalBone3D::PinJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { |
2242 | if (JointData::_set(p_name, p_value, j)) { |
2243 | return true; |
2244 | } |
2245 | |
2246 | bool is_valid_pin = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_PIN; |
2247 | if ("joint_constraints/bias" == p_name) { |
2248 | bias = p_value; |
2249 | if (is_valid_pin) { |
2250 | PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_BIAS, bias); |
2251 | } |
2252 | |
2253 | } else if ("joint_constraints/damping" == p_name) { |
2254 | damping = p_value; |
2255 | if (is_valid_pin) { |
2256 | PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_DAMPING, damping); |
2257 | } |
2258 | |
2259 | } else if ("joint_constraints/impulse_clamp" == p_name) { |
2260 | impulse_clamp = p_value; |
2261 | if (is_valid_pin) { |
2262 | PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, impulse_clamp); |
2263 | } |
2264 | |
2265 | } else { |
2266 | return false; |
2267 | } |
2268 | |
2269 | return true; |
2270 | } |
2271 | |
2272 | bool PhysicalBone3D::PinJointData::_get(const StringName &p_name, Variant &r_ret) const { |
2273 | if (JointData::_get(p_name, r_ret)) { |
2274 | return true; |
2275 | } |
2276 | |
2277 | if ("joint_constraints/bias" == p_name) { |
2278 | r_ret = bias; |
2279 | } else if ("joint_constraints/damping" == p_name) { |
2280 | r_ret = damping; |
2281 | } else if ("joint_constraints/impulse_clamp" == p_name) { |
2282 | r_ret = impulse_clamp; |
2283 | } else { |
2284 | return false; |
2285 | } |
2286 | |
2287 | return true; |
2288 | } |
2289 | |
2290 | void PhysicalBone3D::PinJointData::_get_property_list(List<PropertyInfo> *p_list) const { |
2291 | JointData::_get_property_list(p_list); |
2292 | |
2293 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias" ), PROPERTY_HINT_RANGE, "0.01,0.99,0.01" )); |
2294 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/damping" ), PROPERTY_HINT_RANGE, "0.01,8.0,0.01" )); |
2295 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/impulse_clamp" ), PROPERTY_HINT_RANGE, "0.0,64.0,0.01" )); |
2296 | } |
2297 | |
2298 | bool PhysicalBone3D::ConeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { |
2299 | if (JointData::_set(p_name, p_value, j)) { |
2300 | return true; |
2301 | } |
2302 | |
2303 | bool is_valid_cone = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_CONE_TWIST; |
2304 | if ("joint_constraints/swing_span" == p_name) { |
2305 | swing_span = Math::deg_to_rad(real_t(p_value)); |
2306 | if (is_valid_cone) { |
2307 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, swing_span); |
2308 | } |
2309 | |
2310 | } else if ("joint_constraints/twist_span" == p_name) { |
2311 | twist_span = Math::deg_to_rad(real_t(p_value)); |
2312 | if (is_valid_cone) { |
2313 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, twist_span); |
2314 | } |
2315 | |
2316 | } else if ("joint_constraints/bias" == p_name) { |
2317 | bias = p_value; |
2318 | if (is_valid_cone) { |
2319 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, bias); |
2320 | } |
2321 | |
2322 | } else if ("joint_constraints/softness" == p_name) { |
2323 | softness = p_value; |
2324 | if (is_valid_cone) { |
2325 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, softness); |
2326 | } |
2327 | |
2328 | } else if ("joint_constraints/relaxation" == p_name) { |
2329 | relaxation = p_value; |
2330 | if (is_valid_cone) { |
2331 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, relaxation); |
2332 | } |
2333 | |
2334 | } else { |
2335 | return false; |
2336 | } |
2337 | |
2338 | return true; |
2339 | } |
2340 | |
2341 | bool PhysicalBone3D::ConeJointData::_get(const StringName &p_name, Variant &r_ret) const { |
2342 | if (JointData::_get(p_name, r_ret)) { |
2343 | return true; |
2344 | } |
2345 | |
2346 | if ("joint_constraints/swing_span" == p_name) { |
2347 | r_ret = Math::rad_to_deg(swing_span); |
2348 | } else if ("joint_constraints/twist_span" == p_name) { |
2349 | r_ret = Math::rad_to_deg(twist_span); |
2350 | } else if ("joint_constraints/bias" == p_name) { |
2351 | r_ret = bias; |
2352 | } else if ("joint_constraints/softness" == p_name) { |
2353 | r_ret = softness; |
2354 | } else if ("joint_constraints/relaxation" == p_name) { |
2355 | r_ret = relaxation; |
2356 | } else { |
2357 | return false; |
2358 | } |
2359 | |
2360 | return true; |
2361 | } |
2362 | |
2363 | void PhysicalBone3D::ConeJointData::_get_property_list(List<PropertyInfo> *p_list) const { |
2364 | JointData::_get_property_list(p_list); |
2365 | |
2366 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/swing_span" ), PROPERTY_HINT_RANGE, "-180,180,0.01" )); |
2367 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/twist_span" ), PROPERTY_HINT_RANGE, "-40000,40000,0.1,or_less,or_greater" )); |
2368 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias" ), PROPERTY_HINT_RANGE, "0.01,16.0,0.01" )); |
2369 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/softness" ), PROPERTY_HINT_RANGE, "0.01,16.0,0.01" )); |
2370 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/relaxation" ), PROPERTY_HINT_RANGE, "0.01,16.0,0.01" )); |
2371 | } |
2372 | |
2373 | bool PhysicalBone3D::HingeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { |
2374 | if (JointData::_set(p_name, p_value, j)) { |
2375 | return true; |
2376 | } |
2377 | |
2378 | bool is_valid_hinge = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_HINGE; |
2379 | if ("joint_constraints/angular_limit_enabled" == p_name) { |
2380 | angular_limit_enabled = p_value; |
2381 | if (is_valid_hinge) { |
2382 | PhysicsServer3D::get_singleton()->hinge_joint_set_flag(j, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, angular_limit_enabled); |
2383 | } |
2384 | |
2385 | } else if ("joint_constraints/angular_limit_upper" == p_name) { |
2386 | angular_limit_upper = Math::deg_to_rad(real_t(p_value)); |
2387 | if (is_valid_hinge) { |
2388 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, angular_limit_upper); |
2389 | } |
2390 | |
2391 | } else if ("joint_constraints/angular_limit_lower" == p_name) { |
2392 | angular_limit_lower = Math::deg_to_rad(real_t(p_value)); |
2393 | if (is_valid_hinge) { |
2394 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, angular_limit_lower); |
2395 | } |
2396 | |
2397 | } else if ("joint_constraints/angular_limit_bias" == p_name) { |
2398 | angular_limit_bias = p_value; |
2399 | if (is_valid_hinge) { |
2400 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, angular_limit_bias); |
2401 | } |
2402 | |
2403 | } else if ("joint_constraints/angular_limit_softness" == p_name) { |
2404 | angular_limit_softness = p_value; |
2405 | if (is_valid_hinge) { |
2406 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, angular_limit_softness); |
2407 | } |
2408 | |
2409 | } else if ("joint_constraints/angular_limit_relaxation" == p_name) { |
2410 | angular_limit_relaxation = p_value; |
2411 | if (is_valid_hinge) { |
2412 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, angular_limit_relaxation); |
2413 | } |
2414 | |
2415 | } else { |
2416 | return false; |
2417 | } |
2418 | |
2419 | return true; |
2420 | } |
2421 | |
2422 | bool PhysicalBone3D::HingeJointData::_get(const StringName &p_name, Variant &r_ret) const { |
2423 | if (JointData::_get(p_name, r_ret)) { |
2424 | return true; |
2425 | } |
2426 | |
2427 | if ("joint_constraints/angular_limit_enabled" == p_name) { |
2428 | r_ret = angular_limit_enabled; |
2429 | } else if ("joint_constraints/angular_limit_upper" == p_name) { |
2430 | r_ret = Math::rad_to_deg(angular_limit_upper); |
2431 | } else if ("joint_constraints/angular_limit_lower" == p_name) { |
2432 | r_ret = Math::rad_to_deg(angular_limit_lower); |
2433 | } else if ("joint_constraints/angular_limit_bias" == p_name) { |
2434 | r_ret = angular_limit_bias; |
2435 | } else if ("joint_constraints/angular_limit_softness" == p_name) { |
2436 | r_ret = angular_limit_softness; |
2437 | } else if ("joint_constraints/angular_limit_relaxation" == p_name) { |
2438 | r_ret = angular_limit_relaxation; |
2439 | } else { |
2440 | return false; |
2441 | } |
2442 | |
2443 | return true; |
2444 | } |
2445 | |
2446 | void PhysicalBone3D::HingeJointData::_get_property_list(List<PropertyInfo> *p_list) const { |
2447 | JointData::_get_property_list(p_list); |
2448 | |
2449 | p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("joint_constraints/angular_limit_enabled" ))); |
2450 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper" ), PROPERTY_HINT_RANGE, "-180,180,0.01" )); |
2451 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower" ), PROPERTY_HINT_RANGE, "-180,180,0.01" )); |
2452 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_bias" ), PROPERTY_HINT_RANGE, "0.01,0.99,0.01" )); |
2453 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2454 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_relaxation" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2455 | } |
2456 | |
2457 | bool PhysicalBone3D::SliderJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { |
2458 | if (JointData::_set(p_name, p_value, j)) { |
2459 | return true; |
2460 | } |
2461 | |
2462 | bool is_valid_slider = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_SLIDER; |
2463 | if ("joint_constraints/linear_limit_upper" == p_name) { |
2464 | linear_limit_upper = p_value; |
2465 | if (is_valid_slider) { |
2466 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, linear_limit_upper); |
2467 | } |
2468 | |
2469 | } else if ("joint_constraints/linear_limit_lower" == p_name) { |
2470 | linear_limit_lower = p_value; |
2471 | if (is_valid_slider) { |
2472 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, linear_limit_lower); |
2473 | } |
2474 | |
2475 | } else if ("joint_constraints/linear_limit_softness" == p_name) { |
2476 | linear_limit_softness = p_value; |
2477 | if (is_valid_slider) { |
2478 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, linear_limit_softness); |
2479 | } |
2480 | |
2481 | } else if ("joint_constraints/linear_limit_restitution" == p_name) { |
2482 | linear_limit_restitution = p_value; |
2483 | if (is_valid_slider) { |
2484 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, linear_limit_restitution); |
2485 | } |
2486 | |
2487 | } else if ("joint_constraints/linear_limit_damping" == p_name) { |
2488 | linear_limit_damping = p_value; |
2489 | if (is_valid_slider) { |
2490 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, linear_limit_restitution); |
2491 | } |
2492 | |
2493 | } else if ("joint_constraints/angular_limit_upper" == p_name) { |
2494 | angular_limit_upper = Math::deg_to_rad(real_t(p_value)); |
2495 | if (is_valid_slider) { |
2496 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, angular_limit_upper); |
2497 | } |
2498 | |
2499 | } else if ("joint_constraints/angular_limit_lower" == p_name) { |
2500 | angular_limit_lower = Math::deg_to_rad(real_t(p_value)); |
2501 | if (is_valid_slider) { |
2502 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, angular_limit_lower); |
2503 | } |
2504 | |
2505 | } else if ("joint_constraints/angular_limit_softness" == p_name) { |
2506 | angular_limit_softness = p_value; |
2507 | if (is_valid_slider) { |
2508 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness); |
2509 | } |
2510 | |
2511 | } else if ("joint_constraints/angular_limit_restitution" == p_name) { |
2512 | angular_limit_restitution = p_value; |
2513 | if (is_valid_slider) { |
2514 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness); |
2515 | } |
2516 | |
2517 | } else if ("joint_constraints/angular_limit_damping" == p_name) { |
2518 | angular_limit_damping = p_value; |
2519 | if (is_valid_slider) { |
2520 | PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, angular_limit_damping); |
2521 | } |
2522 | |
2523 | } else { |
2524 | return false; |
2525 | } |
2526 | |
2527 | return true; |
2528 | } |
2529 | |
2530 | bool PhysicalBone3D::SliderJointData::_get(const StringName &p_name, Variant &r_ret) const { |
2531 | if (JointData::_get(p_name, r_ret)) { |
2532 | return true; |
2533 | } |
2534 | |
2535 | if ("joint_constraints/linear_limit_upper" == p_name) { |
2536 | r_ret = linear_limit_upper; |
2537 | } else if ("joint_constraints/linear_limit_lower" == p_name) { |
2538 | r_ret = linear_limit_lower; |
2539 | } else if ("joint_constraints/linear_limit_softness" == p_name) { |
2540 | r_ret = linear_limit_softness; |
2541 | } else if ("joint_constraints/linear_limit_restitution" == p_name) { |
2542 | r_ret = linear_limit_restitution; |
2543 | } else if ("joint_constraints/linear_limit_damping" == p_name) { |
2544 | r_ret = linear_limit_damping; |
2545 | } else if ("joint_constraints/angular_limit_upper" == p_name) { |
2546 | r_ret = Math::rad_to_deg(angular_limit_upper); |
2547 | } else if ("joint_constraints/angular_limit_lower" == p_name) { |
2548 | r_ret = Math::rad_to_deg(angular_limit_lower); |
2549 | } else if ("joint_constraints/angular_limit_softness" == p_name) { |
2550 | r_ret = angular_limit_softness; |
2551 | } else if ("joint_constraints/angular_limit_restitution" == p_name) { |
2552 | r_ret = angular_limit_restitution; |
2553 | } else if ("joint_constraints/angular_limit_damping" == p_name) { |
2554 | r_ret = angular_limit_damping; |
2555 | } else { |
2556 | return false; |
2557 | } |
2558 | |
2559 | return true; |
2560 | } |
2561 | |
2562 | void PhysicalBone3D::SliderJointData::_get_property_list(List<PropertyInfo> *p_list) const { |
2563 | JointData::_get_property_list(p_list); |
2564 | |
2565 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_upper" ))); |
2566 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_lower" ))); |
2567 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_softness" ), PROPERTY_HINT_RANGE, "0.01,16.0,0.01" )); |
2568 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_restitution" ), PROPERTY_HINT_RANGE, "0.01,16.0,0.01" )); |
2569 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_damping" ), PROPERTY_HINT_RANGE, "0,16.0,0.01" )); |
2570 | |
2571 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper" ), PROPERTY_HINT_RANGE, "-180,180,0.01" )); |
2572 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower" ), PROPERTY_HINT_RANGE, "-180,180,0.01" )); |
2573 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness" ), PROPERTY_HINT_RANGE, "0.01,16.0,0.01" )); |
2574 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_restitution" ), PROPERTY_HINT_RANGE, "0.01,16.0,0.01" )); |
2575 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_damping" ), PROPERTY_HINT_RANGE, "0,16.0,0.01" )); |
2576 | } |
2577 | |
2578 | bool PhysicalBone3D::SixDOFJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { |
2579 | if (JointData::_set(p_name, p_value, j)) { |
2580 | return true; |
2581 | } |
2582 | |
2583 | String path = p_name; |
2584 | |
2585 | if (!path.begins_with("joint_constraints/" )) { |
2586 | return false; |
2587 | } |
2588 | |
2589 | Vector3::Axis axis; |
2590 | { |
2591 | const String axis_s = path.get_slicec('/', 1); |
2592 | if ("x" == axis_s) { |
2593 | axis = Vector3::AXIS_X; |
2594 | } else if ("y" == axis_s) { |
2595 | axis = Vector3::AXIS_Y; |
2596 | } else if ("z" == axis_s) { |
2597 | axis = Vector3::AXIS_Z; |
2598 | } else { |
2599 | return false; |
2600 | } |
2601 | } |
2602 | |
2603 | String var_name = path.get_slicec('/', 2); |
2604 | bool is_valid_6dof = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_6DOF; |
2605 | if ("linear_limit_enabled" == var_name) { |
2606 | axis_data[axis].linear_limit_enabled = p_value; |
2607 | if (is_valid_6dof) { |
2608 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, axis_data[axis].linear_limit_enabled); |
2609 | } |
2610 | |
2611 | } else if ("linear_limit_upper" == var_name) { |
2612 | axis_data[axis].linear_limit_upper = p_value; |
2613 | if (is_valid_6dof) { |
2614 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, axis_data[axis].linear_limit_upper); |
2615 | } |
2616 | |
2617 | } else if ("linear_limit_lower" == var_name) { |
2618 | axis_data[axis].linear_limit_lower = p_value; |
2619 | if (is_valid_6dof) { |
2620 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, axis_data[axis].linear_limit_lower); |
2621 | } |
2622 | |
2623 | } else if ("linear_limit_softness" == var_name) { |
2624 | axis_data[axis].linear_limit_softness = p_value; |
2625 | if (is_valid_6dof) { |
2626 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, axis_data[axis].linear_limit_softness); |
2627 | } |
2628 | |
2629 | } else if ("linear_spring_enabled" == var_name) { |
2630 | axis_data[axis].linear_spring_enabled = p_value; |
2631 | if (is_valid_6dof) { |
2632 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, axis_data[axis].linear_spring_enabled); |
2633 | } |
2634 | |
2635 | } else if ("linear_spring_stiffness" == var_name) { |
2636 | axis_data[axis].linear_spring_stiffness = p_value; |
2637 | if (is_valid_6dof) { |
2638 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, axis_data[axis].linear_spring_stiffness); |
2639 | } |
2640 | |
2641 | } else if ("linear_spring_damping" == var_name) { |
2642 | axis_data[axis].linear_spring_damping = p_value; |
2643 | if (is_valid_6dof) { |
2644 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, axis_data[axis].linear_spring_damping); |
2645 | } |
2646 | |
2647 | } else if ("linear_equilibrium_point" == var_name) { |
2648 | axis_data[axis].linear_equilibrium_point = p_value; |
2649 | if (is_valid_6dof) { |
2650 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].linear_equilibrium_point); |
2651 | } |
2652 | |
2653 | } else if ("linear_restitution" == var_name) { |
2654 | axis_data[axis].linear_restitution = p_value; |
2655 | if (is_valid_6dof) { |
2656 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, axis_data[axis].linear_restitution); |
2657 | } |
2658 | |
2659 | } else if ("linear_damping" == var_name) { |
2660 | axis_data[axis].linear_damping = p_value; |
2661 | if (is_valid_6dof) { |
2662 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, axis_data[axis].linear_damping); |
2663 | } |
2664 | |
2665 | } else if ("angular_limit_enabled" == var_name) { |
2666 | axis_data[axis].angular_limit_enabled = p_value; |
2667 | if (is_valid_6dof) { |
2668 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, axis_data[axis].angular_limit_enabled); |
2669 | } |
2670 | |
2671 | } else if ("angular_limit_upper" == var_name) { |
2672 | axis_data[axis].angular_limit_upper = Math::deg_to_rad(real_t(p_value)); |
2673 | if (is_valid_6dof) { |
2674 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, axis_data[axis].angular_limit_upper); |
2675 | } |
2676 | |
2677 | } else if ("angular_limit_lower" == var_name) { |
2678 | axis_data[axis].angular_limit_lower = Math::deg_to_rad(real_t(p_value)); |
2679 | if (is_valid_6dof) { |
2680 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, axis_data[axis].angular_limit_lower); |
2681 | } |
2682 | |
2683 | } else if ("angular_limit_softness" == var_name) { |
2684 | axis_data[axis].angular_limit_softness = p_value; |
2685 | if (is_valid_6dof) { |
2686 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, axis_data[axis].angular_limit_softness); |
2687 | } |
2688 | |
2689 | } else if ("angular_restitution" == var_name) { |
2690 | axis_data[axis].angular_restitution = p_value; |
2691 | if (is_valid_6dof) { |
2692 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, axis_data[axis].angular_restitution); |
2693 | } |
2694 | |
2695 | } else if ("angular_damping" == var_name) { |
2696 | axis_data[axis].angular_damping = p_value; |
2697 | if (is_valid_6dof) { |
2698 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, axis_data[axis].angular_damping); |
2699 | } |
2700 | |
2701 | } else if ("erp" == var_name) { |
2702 | axis_data[axis].erp = p_value; |
2703 | if (is_valid_6dof) { |
2704 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, axis_data[axis].erp); |
2705 | } |
2706 | |
2707 | } else if ("angular_spring_enabled" == var_name) { |
2708 | axis_data[axis].angular_spring_enabled = p_value; |
2709 | if (is_valid_6dof) { |
2710 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, axis_data[axis].angular_spring_enabled); |
2711 | } |
2712 | |
2713 | } else if ("angular_spring_stiffness" == var_name) { |
2714 | axis_data[axis].angular_spring_stiffness = p_value; |
2715 | if (is_valid_6dof) { |
2716 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, axis_data[axis].angular_spring_stiffness); |
2717 | } |
2718 | |
2719 | } else if ("angular_spring_damping" == var_name) { |
2720 | axis_data[axis].angular_spring_damping = p_value; |
2721 | if (is_valid_6dof) { |
2722 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, axis_data[axis].angular_spring_damping); |
2723 | } |
2724 | |
2725 | } else if ("angular_equilibrium_point" == var_name) { |
2726 | axis_data[axis].angular_equilibrium_point = p_value; |
2727 | if (is_valid_6dof) { |
2728 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].angular_equilibrium_point); |
2729 | } |
2730 | |
2731 | } else { |
2732 | return false; |
2733 | } |
2734 | |
2735 | return true; |
2736 | } |
2737 | |
2738 | bool PhysicalBone3D::SixDOFJointData::_get(const StringName &p_name, Variant &r_ret) const { |
2739 | if (JointData::_get(p_name, r_ret)) { |
2740 | return true; |
2741 | } |
2742 | |
2743 | String path = p_name; |
2744 | |
2745 | if (!path.begins_with("joint_constraints/" )) { |
2746 | return false; |
2747 | } |
2748 | |
2749 | int axis; |
2750 | { |
2751 | const String axis_s = path.get_slicec('/', 1); |
2752 | if ("x" == axis_s) { |
2753 | axis = 0; |
2754 | } else if ("y" == axis_s) { |
2755 | axis = 1; |
2756 | } else if ("z" == axis_s) { |
2757 | axis = 2; |
2758 | } else { |
2759 | return false; |
2760 | } |
2761 | } |
2762 | |
2763 | String var_name = path.get_slicec('/', 2); |
2764 | |
2765 | if ("linear_limit_enabled" == var_name) { |
2766 | r_ret = axis_data[axis].linear_limit_enabled; |
2767 | } else if ("linear_limit_upper" == var_name) { |
2768 | r_ret = axis_data[axis].linear_limit_upper; |
2769 | } else if ("linear_limit_lower" == var_name) { |
2770 | r_ret = axis_data[axis].linear_limit_lower; |
2771 | } else if ("linear_limit_softness" == var_name) { |
2772 | r_ret = axis_data[axis].linear_limit_softness; |
2773 | } else if ("linear_spring_enabled" == var_name) { |
2774 | r_ret = axis_data[axis].linear_spring_enabled; |
2775 | } else if ("linear_spring_stiffness" == var_name) { |
2776 | r_ret = axis_data[axis].linear_spring_stiffness; |
2777 | } else if ("linear_spring_damping" == var_name) { |
2778 | r_ret = axis_data[axis].linear_spring_damping; |
2779 | } else if ("linear_equilibrium_point" == var_name) { |
2780 | r_ret = axis_data[axis].linear_equilibrium_point; |
2781 | } else if ("linear_restitution" == var_name) { |
2782 | r_ret = axis_data[axis].linear_restitution; |
2783 | } else if ("linear_damping" == var_name) { |
2784 | r_ret = axis_data[axis].linear_damping; |
2785 | } else if ("angular_limit_enabled" == var_name) { |
2786 | r_ret = axis_data[axis].angular_limit_enabled; |
2787 | } else if ("angular_limit_upper" == var_name) { |
2788 | r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_upper); |
2789 | } else if ("angular_limit_lower" == var_name) { |
2790 | r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_lower); |
2791 | } else if ("angular_limit_softness" == var_name) { |
2792 | r_ret = axis_data[axis].angular_limit_softness; |
2793 | } else if ("angular_restitution" == var_name) { |
2794 | r_ret = axis_data[axis].angular_restitution; |
2795 | } else if ("angular_damping" == var_name) { |
2796 | r_ret = axis_data[axis].angular_damping; |
2797 | } else if ("erp" == var_name) { |
2798 | r_ret = axis_data[axis].erp; |
2799 | } else if ("angular_spring_enabled" == var_name) { |
2800 | r_ret = axis_data[axis].angular_spring_enabled; |
2801 | } else if ("angular_spring_stiffness" == var_name) { |
2802 | r_ret = axis_data[axis].angular_spring_stiffness; |
2803 | } else if ("angular_spring_damping" == var_name) { |
2804 | r_ret = axis_data[axis].angular_spring_damping; |
2805 | } else if ("angular_equilibrium_point" == var_name) { |
2806 | r_ret = axis_data[axis].angular_equilibrium_point; |
2807 | } else { |
2808 | return false; |
2809 | } |
2810 | |
2811 | return true; |
2812 | } |
2813 | |
2814 | void PhysicalBone3D::SixDOFJointData::_get_property_list(List<PropertyInfo> *p_list) const { |
2815 | const StringName axis_names[] = { PNAME("x" ), PNAME("y" ), PNAME("z" ) }; |
2816 | for (int i = 0; i < 3; ++i) { |
2817 | const String prefix = vformat("%s/%s/" , PNAME("joint_constraints" ), axis_names[i]); |
2818 | p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_limit_enabled" ))); |
2819 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_upper" ))); |
2820 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_lower" ))); |
2821 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_softness" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2822 | p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_spring_enabled" ))); |
2823 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_stiffness" ))); |
2824 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_damping" ))); |
2825 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_equilibrium_point" ))); |
2826 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_restitution" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2827 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_damping" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2828 | p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_limit_enabled" ))); |
2829 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_upper" ), PROPERTY_HINT_RANGE, "-180,180,0.01" )); |
2830 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_lower" ), PROPERTY_HINT_RANGE, "-180,180,0.01" )); |
2831 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_softness" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2832 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_restitution" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2833 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_damping" ), PROPERTY_HINT_RANGE, "0.01,16,0.01" )); |
2834 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("erp" ))); |
2835 | p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_spring_enabled" ))); |
2836 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_stiffness" ))); |
2837 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_damping" ))); |
2838 | p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_equilibrium_point" ))); |
2839 | } |
2840 | } |
2841 | |
2842 | bool PhysicalBone3D::_set(const StringName &p_name, const Variant &p_value) { |
2843 | if (p_name == "bone_name" ) { |
2844 | set_bone_name(p_value); |
2845 | return true; |
2846 | } |
2847 | |
2848 | if (joint_data) { |
2849 | if (joint_data->_set(p_name, p_value, joint)) { |
2850 | #ifdef TOOLS_ENABLED |
2851 | update_gizmos(); |
2852 | #endif |
2853 | return true; |
2854 | } |
2855 | } |
2856 | |
2857 | return false; |
2858 | } |
2859 | |
2860 | bool PhysicalBone3D::_get(const StringName &p_name, Variant &r_ret) const { |
2861 | if (p_name == "bone_name" ) { |
2862 | r_ret = get_bone_name(); |
2863 | return true; |
2864 | } |
2865 | |
2866 | if (joint_data) { |
2867 | return joint_data->_get(p_name, r_ret); |
2868 | } |
2869 | |
2870 | return false; |
2871 | } |
2872 | |
2873 | void PhysicalBone3D::_get_property_list(List<PropertyInfo> *p_list) const { |
2874 | Skeleton3D *parent = find_skeleton_parent(get_parent()); |
2875 | |
2876 | if (parent) { |
2877 | String names; |
2878 | for (int i = 0; i < parent->get_bone_count(); i++) { |
2879 | if (i > 0) { |
2880 | names += "," ; |
2881 | } |
2882 | names += parent->get_bone_name(i); |
2883 | } |
2884 | |
2885 | p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name" ), PROPERTY_HINT_ENUM, names)); |
2886 | } else { |
2887 | p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name" ))); |
2888 | } |
2889 | |
2890 | if (joint_data) { |
2891 | joint_data->_get_property_list(p_list); |
2892 | } |
2893 | } |
2894 | |
2895 | void PhysicalBone3D::_notification(int p_what) { |
2896 | switch (p_what) { |
2897 | case NOTIFICATION_ENTER_TREE: |
2898 | parent_skeleton = find_skeleton_parent(get_parent()); |
2899 | update_bone_id(); |
2900 | reset_to_rest_position(); |
2901 | reset_physics_simulation_state(); |
2902 | if (joint_data) { |
2903 | _reload_joint(); |
2904 | } |
2905 | break; |
2906 | |
2907 | case NOTIFICATION_EXIT_TREE: { |
2908 | if (parent_skeleton) { |
2909 | if (-1 != bone_id) { |
2910 | parent_skeleton->unbind_physical_bone_from_bone(bone_id); |
2911 | bone_id = -1; |
2912 | } |
2913 | } |
2914 | parent_skeleton = nullptr; |
2915 | PhysicsServer3D::get_singleton()->joint_clear(joint); |
2916 | } break; |
2917 | |
2918 | case NOTIFICATION_TRANSFORM_CHANGED: { |
2919 | if (Engine::get_singleton()->is_editor_hint()) { |
2920 | update_offset(); |
2921 | } |
2922 | } break; |
2923 | } |
2924 | } |
2925 | |
2926 | void PhysicalBone3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) { |
2927 | set_global_transform(p_state->get_transform()); |
2928 | linear_velocity = p_state->get_linear_velocity(); |
2929 | angular_velocity = p_state->get_angular_velocity(); |
2930 | } |
2931 | |
2932 | void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) { |
2933 | if (!simulate_physics || !_internal_simulate_physics) { |
2934 | return; |
2935 | } |
2936 | |
2937 | set_ignore_transform_notification(true); |
2938 | _sync_body_state(p_state); |
2939 | |
2940 | GDVIRTUAL_CALL(_integrate_forces, p_state); |
2941 | |
2942 | _sync_body_state(p_state); |
2943 | set_ignore_transform_notification(false); |
2944 | _on_transform_changed(); |
2945 | |
2946 | Transform3D global_transform(p_state->get_transform()); |
2947 | |
2948 | // Update skeleton |
2949 | if (parent_skeleton) { |
2950 | if (-1 != bone_id) { |
2951 | parent_skeleton->set_bone_global_pose_override(bone_id, parent_skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse), 1.0, true); |
2952 | } |
2953 | } |
2954 | } |
2955 | |
2956 | void PhysicalBone3D::_bind_methods() { |
2957 | ClassDB::bind_method(D_METHOD("apply_central_impulse" , "impulse" ), &PhysicalBone3D::apply_central_impulse); |
2958 | ClassDB::bind_method(D_METHOD("apply_impulse" , "impulse" , "position" ), &PhysicalBone3D::apply_impulse, Vector3()); |
2959 | |
2960 | ClassDB::bind_method(D_METHOD("set_joint_type" , "joint_type" ), &PhysicalBone3D::set_joint_type); |
2961 | ClassDB::bind_method(D_METHOD("get_joint_type" ), &PhysicalBone3D::get_joint_type); |
2962 | |
2963 | ClassDB::bind_method(D_METHOD("set_joint_offset" , "offset" ), &PhysicalBone3D::set_joint_offset); |
2964 | ClassDB::bind_method(D_METHOD("get_joint_offset" ), &PhysicalBone3D::get_joint_offset); |
2965 | ClassDB::bind_method(D_METHOD("set_joint_rotation" , "euler" ), &PhysicalBone3D::set_joint_rotation); |
2966 | ClassDB::bind_method(D_METHOD("get_joint_rotation" ), &PhysicalBone3D::get_joint_rotation); |
2967 | |
2968 | ClassDB::bind_method(D_METHOD("set_body_offset" , "offset" ), &PhysicalBone3D::set_body_offset); |
2969 | ClassDB::bind_method(D_METHOD("get_body_offset" ), &PhysicalBone3D::get_body_offset); |
2970 | |
2971 | ClassDB::bind_method(D_METHOD("get_simulate_physics" ), &PhysicalBone3D::get_simulate_physics); |
2972 | |
2973 | ClassDB::bind_method(D_METHOD("is_simulating_physics" ), &PhysicalBone3D::is_simulating_physics); |
2974 | |
2975 | ClassDB::bind_method(D_METHOD("get_bone_id" ), &PhysicalBone3D::get_bone_id); |
2976 | |
2977 | ClassDB::bind_method(D_METHOD("set_mass" , "mass" ), &PhysicalBone3D::set_mass); |
2978 | ClassDB::bind_method(D_METHOD("get_mass" ), &PhysicalBone3D::get_mass); |
2979 | |
2980 | ClassDB::bind_method(D_METHOD("set_friction" , "friction" ), &PhysicalBone3D::set_friction); |
2981 | ClassDB::bind_method(D_METHOD("get_friction" ), &PhysicalBone3D::get_friction); |
2982 | |
2983 | ClassDB::bind_method(D_METHOD("set_bounce" , "bounce" ), &PhysicalBone3D::set_bounce); |
2984 | ClassDB::bind_method(D_METHOD("get_bounce" ), &PhysicalBone3D::get_bounce); |
2985 | |
2986 | ClassDB::bind_method(D_METHOD("set_gravity_scale" , "gravity_scale" ), &PhysicalBone3D::set_gravity_scale); |
2987 | ClassDB::bind_method(D_METHOD("get_gravity_scale" ), &PhysicalBone3D::get_gravity_scale); |
2988 | |
2989 | ClassDB::bind_method(D_METHOD("set_linear_damp_mode" , "linear_damp_mode" ), &PhysicalBone3D::set_linear_damp_mode); |
2990 | ClassDB::bind_method(D_METHOD("get_linear_damp_mode" ), &PhysicalBone3D::get_linear_damp_mode); |
2991 | |
2992 | ClassDB::bind_method(D_METHOD("set_angular_damp_mode" , "angular_damp_mode" ), &PhysicalBone3D::set_angular_damp_mode); |
2993 | ClassDB::bind_method(D_METHOD("get_angular_damp_mode" ), &PhysicalBone3D::get_angular_damp_mode); |
2994 | |
2995 | ClassDB::bind_method(D_METHOD("set_linear_damp" , "linear_damp" ), &PhysicalBone3D::set_linear_damp); |
2996 | ClassDB::bind_method(D_METHOD("get_linear_damp" ), &PhysicalBone3D::get_linear_damp); |
2997 | |
2998 | ClassDB::bind_method(D_METHOD("set_angular_damp" , "angular_damp" ), &PhysicalBone3D::set_angular_damp); |
2999 | ClassDB::bind_method(D_METHOD("get_angular_damp" ), &PhysicalBone3D::get_angular_damp); |
3000 | |
3001 | ClassDB::bind_method(D_METHOD("set_linear_velocity" , "linear_velocity" ), &PhysicalBone3D::set_linear_velocity); |
3002 | ClassDB::bind_method(D_METHOD("get_linear_velocity" ), &PhysicalBone3D::get_linear_velocity); |
3003 | |
3004 | ClassDB::bind_method(D_METHOD("set_angular_velocity" , "angular_velocity" ), &PhysicalBone3D::set_angular_velocity); |
3005 | ClassDB::bind_method(D_METHOD("get_angular_velocity" ), &PhysicalBone3D::get_angular_velocity); |
3006 | |
3007 | ClassDB::bind_method(D_METHOD("set_use_custom_integrator" , "enable" ), &PhysicalBone3D::set_use_custom_integrator); |
3008 | ClassDB::bind_method(D_METHOD("is_using_custom_integrator" ), &PhysicalBone3D::is_using_custom_integrator); |
3009 | |
3010 | ClassDB::bind_method(D_METHOD("set_can_sleep" , "able_to_sleep" ), &PhysicalBone3D::set_can_sleep); |
3011 | ClassDB::bind_method(D_METHOD("is_able_to_sleep" ), &PhysicalBone3D::is_able_to_sleep); |
3012 | |
3013 | GDVIRTUAL_BIND(_integrate_forces, "state" ); |
3014 | |
3015 | ADD_GROUP("Joint" , "joint_" ); |
3016 | ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_type" , PROPERTY_HINT_ENUM, "None,PinJoint,ConeJoint,HingeJoint,SliderJoint,6DOFJoint" ), "set_joint_type" , "get_joint_type" ); |
3017 | ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "joint_offset" , PROPERTY_HINT_NONE, "suffix:m" ), "set_joint_offset" , "get_joint_offset" ); |
3018 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "joint_rotation" , PROPERTY_HINT_RANGE, "-360,360,0.01,or_less,or_greater,radians" ), "set_joint_rotation" , "get_joint_rotation" ); |
3019 | |
3020 | ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "body_offset" , PROPERTY_HINT_NONE, "suffix:m" ), "set_body_offset" , "get_body_offset" ); |
3021 | |
3022 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass" , PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp,suffix:kg" ), "set_mass" , "get_mass" ); |
3023 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "friction" , PROPERTY_HINT_RANGE, "0,1,0.01" ), "set_friction" , "get_friction" ); |
3024 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce" , PROPERTY_HINT_RANGE, "0,1,0.01" ), "set_bounce" , "get_bounce" ); |
3025 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale" , PROPERTY_HINT_RANGE, "-10,10,0.01" ), "set_gravity_scale" , "get_gravity_scale" ); |
3026 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator" ), "set_use_custom_integrator" , "is_using_custom_integrator" ); |
3027 | ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode" , PROPERTY_HINT_ENUM, "Combine,Replace" ), "set_linear_damp_mode" , "get_linear_damp_mode" ); |
3028 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp" , PROPERTY_HINT_RANGE, "0,100,0.001,or_greater" ), "set_linear_damp" , "get_linear_damp" ); |
3029 | ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode" , PROPERTY_HINT_ENUM, "Combine,Replace" ), "set_angular_damp_mode" , "get_angular_damp_mode" ); |
3030 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp" , PROPERTY_HINT_RANGE, "0,100,0.001,or_greater" ), "set_angular_damp" , "get_angular_damp" ); |
3031 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity" , PROPERTY_HINT_NONE, "suffix:m/s" ), "set_linear_velocity" , "get_linear_velocity" ); |
3032 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity" , PROPERTY_HINT_NONE, U"radians,suffix:\u00B0/s" ), "set_angular_velocity" , "get_angular_velocity" ); |
3033 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep" ), "set_can_sleep" , "is_able_to_sleep" ); |
3034 | |
3035 | BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE); |
3036 | BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE); |
3037 | |
3038 | BIND_ENUM_CONSTANT(JOINT_TYPE_NONE); |
3039 | BIND_ENUM_CONSTANT(JOINT_TYPE_PIN); |
3040 | BIND_ENUM_CONSTANT(JOINT_TYPE_CONE); |
3041 | BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE); |
3042 | BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER); |
3043 | BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF); |
3044 | } |
3045 | |
3046 | Skeleton3D *PhysicalBone3D::find_skeleton_parent(Node *p_parent) { |
3047 | if (!p_parent) { |
3048 | return nullptr; |
3049 | } |
3050 | Skeleton3D *s = Object::cast_to<Skeleton3D>(p_parent); |
3051 | return s ? s : find_skeleton_parent(p_parent->get_parent()); |
3052 | } |
3053 | |
3054 | void PhysicalBone3D::_update_joint_offset() { |
3055 | _fix_joint_offset(); |
3056 | |
3057 | set_ignore_transform_notification(true); |
3058 | reset_to_rest_position(); |
3059 | set_ignore_transform_notification(false); |
3060 | |
3061 | #ifdef TOOLS_ENABLED |
3062 | update_gizmos(); |
3063 | #endif |
3064 | } |
3065 | |
3066 | void PhysicalBone3D::_fix_joint_offset() { |
3067 | // Clamp joint origin to bone origin |
3068 | if (parent_skeleton) { |
3069 | joint_offset.origin = body_offset.affine_inverse().origin; |
3070 | } |
3071 | } |
3072 | |
3073 | void PhysicalBone3D::_reload_joint() { |
3074 | if (!parent_skeleton) { |
3075 | PhysicsServer3D::get_singleton()->joint_clear(joint); |
3076 | return; |
3077 | } |
3078 | |
3079 | PhysicalBone3D *body_a = parent_skeleton->get_physical_bone_parent(bone_id); |
3080 | if (!body_a) { |
3081 | PhysicsServer3D::get_singleton()->joint_clear(joint); |
3082 | return; |
3083 | } |
3084 | |
3085 | Transform3D joint_transf = get_global_transform() * joint_offset; |
3086 | Transform3D local_a = body_a->get_global_transform().affine_inverse() * joint_transf; |
3087 | local_a.orthonormalize(); |
3088 | |
3089 | switch (get_joint_type()) { |
3090 | case JOINT_TYPE_PIN: { |
3091 | PhysicsServer3D::get_singleton()->joint_make_pin(joint, body_a->get_rid(), local_a.origin, get_rid(), joint_offset.origin); |
3092 | const PinJointData *pjd(static_cast<const PinJointData *>(joint_data)); |
3093 | PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_BIAS, pjd->bias); |
3094 | PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_DAMPING, pjd->damping); |
3095 | PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, pjd->impulse_clamp); |
3096 | |
3097 | } break; |
3098 | case JOINT_TYPE_CONE: { |
3099 | PhysicsServer3D::get_singleton()->joint_make_cone_twist(joint, body_a->get_rid(), local_a, get_rid(), joint_offset); |
3100 | const ConeJointData *cjd(static_cast<const ConeJointData *>(joint_data)); |
3101 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, cjd->swing_span); |
3102 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, cjd->twist_span); |
3103 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, cjd->bias); |
3104 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, cjd->softness); |
3105 | PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, cjd->relaxation); |
3106 | |
3107 | } break; |
3108 | case JOINT_TYPE_HINGE: { |
3109 | PhysicsServer3D::get_singleton()->joint_make_hinge(joint, body_a->get_rid(), local_a, get_rid(), joint_offset); |
3110 | const HingeJointData *hjd(static_cast<const HingeJointData *>(joint_data)); |
3111 | PhysicsServer3D::get_singleton()->hinge_joint_set_flag(joint, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, hjd->angular_limit_enabled); |
3112 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, hjd->angular_limit_upper); |
3113 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, hjd->angular_limit_lower); |
3114 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, hjd->angular_limit_bias); |
3115 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, hjd->angular_limit_softness); |
3116 | PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, hjd->angular_limit_relaxation); |
3117 | |
3118 | } break; |
3119 | case JOINT_TYPE_SLIDER: { |
3120 | PhysicsServer3D::get_singleton()->joint_make_slider(joint, body_a->get_rid(), local_a, get_rid(), joint_offset); |
3121 | const SliderJointData *sjd(static_cast<const SliderJointData *>(joint_data)); |
3122 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, sjd->linear_limit_upper); |
3123 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, sjd->linear_limit_lower); |
3124 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, sjd->linear_limit_softness); |
3125 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, sjd->linear_limit_restitution); |
3126 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, sjd->linear_limit_restitution); |
3127 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, sjd->angular_limit_upper); |
3128 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, sjd->angular_limit_lower); |
3129 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness); |
3130 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness); |
3131 | PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, sjd->angular_limit_damping); |
3132 | |
3133 | } break; |
3134 | case JOINT_TYPE_6DOF: { |
3135 | PhysicsServer3D::get_singleton()->joint_make_generic_6dof(joint, body_a->get_rid(), local_a, get_rid(), joint_offset); |
3136 | const SixDOFJointData *g6dofjd(static_cast<const SixDOFJointData *>(joint_data)); |
3137 | for (int axis = 0; axis < 3; ++axis) { |
3138 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, g6dofjd->axis_data[axis].linear_limit_enabled); |
3139 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, g6dofjd->axis_data[axis].linear_limit_upper); |
3140 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, g6dofjd->axis_data[axis].linear_limit_lower); |
3141 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].linear_limit_softness); |
3142 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, g6dofjd->axis_data[axis].linear_spring_enabled); |
3143 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].linear_spring_stiffness); |
3144 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, g6dofjd->axis_data[axis].linear_spring_damping); |
3145 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].linear_equilibrium_point); |
3146 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, g6dofjd->axis_data[axis].linear_restitution); |
3147 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, g6dofjd->axis_data[axis].linear_damping); |
3148 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, g6dofjd->axis_data[axis].angular_limit_enabled); |
3149 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, g6dofjd->axis_data[axis].angular_limit_upper); |
3150 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, g6dofjd->axis_data[axis].angular_limit_lower); |
3151 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].angular_limit_softness); |
3152 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, g6dofjd->axis_data[axis].angular_restitution); |
3153 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, g6dofjd->axis_data[axis].angular_damping); |
3154 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, g6dofjd->axis_data[axis].erp); |
3155 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, g6dofjd->axis_data[axis].angular_spring_enabled); |
3156 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].angular_spring_stiffness); |
3157 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, g6dofjd->axis_data[axis].angular_spring_damping); |
3158 | PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].angular_equilibrium_point); |
3159 | } |
3160 | |
3161 | } break; |
3162 | case JOINT_TYPE_NONE: { |
3163 | } break; |
3164 | } |
3165 | } |
3166 | |
3167 | void PhysicalBone3D::_on_bone_parent_changed() { |
3168 | _reload_joint(); |
3169 | } |
3170 | |
3171 | #ifdef TOOLS_ENABLED |
3172 | void PhysicalBone3D::_set_gizmo_move_joint(bool p_move_joint) { |
3173 | gizmo_move_joint = p_move_joint; |
3174 | } |
3175 | |
3176 | Transform3D PhysicalBone3D::get_global_gizmo_transform() const { |
3177 | return gizmo_move_joint ? get_global_transform() * joint_offset : get_global_transform(); |
3178 | } |
3179 | |
3180 | Transform3D PhysicalBone3D::get_local_gizmo_transform() const { |
3181 | return gizmo_move_joint ? get_transform() * joint_offset : get_transform(); |
3182 | } |
3183 | #endif |
3184 | |
3185 | const PhysicalBone3D::JointData *PhysicalBone3D::get_joint_data() const { |
3186 | return joint_data; |
3187 | } |
3188 | |
3189 | Skeleton3D *PhysicalBone3D::find_skeleton_parent() { |
3190 | return find_skeleton_parent(this); |
3191 | } |
3192 | |
3193 | void PhysicalBone3D::set_joint_type(JointType p_joint_type) { |
3194 | if (p_joint_type == get_joint_type()) { |
3195 | return; |
3196 | } |
3197 | |
3198 | if (joint_data) { |
3199 | memdelete(joint_data); |
3200 | } |
3201 | joint_data = nullptr; |
3202 | switch (p_joint_type) { |
3203 | case JOINT_TYPE_PIN: |
3204 | joint_data = memnew(PinJointData); |
3205 | break; |
3206 | case JOINT_TYPE_CONE: |
3207 | joint_data = memnew(ConeJointData); |
3208 | break; |
3209 | case JOINT_TYPE_HINGE: |
3210 | joint_data = memnew(HingeJointData); |
3211 | break; |
3212 | case JOINT_TYPE_SLIDER: |
3213 | joint_data = memnew(SliderJointData); |
3214 | break; |
3215 | case JOINT_TYPE_6DOF: |
3216 | joint_data = memnew(SixDOFJointData); |
3217 | break; |
3218 | case JOINT_TYPE_NONE: |
3219 | break; |
3220 | } |
3221 | |
3222 | _reload_joint(); |
3223 | |
3224 | #ifdef TOOLS_ENABLED |
3225 | notify_property_list_changed(); |
3226 | update_gizmos(); |
3227 | #endif |
3228 | } |
3229 | |
3230 | PhysicalBone3D::JointType PhysicalBone3D::get_joint_type() const { |
3231 | return joint_data ? joint_data->get_joint_type() : JOINT_TYPE_NONE; |
3232 | } |
3233 | |
3234 | void PhysicalBone3D::set_joint_offset(const Transform3D &p_offset) { |
3235 | joint_offset = p_offset; |
3236 | |
3237 | _update_joint_offset(); |
3238 | } |
3239 | |
3240 | const Transform3D &PhysicalBone3D::get_joint_offset() const { |
3241 | return joint_offset; |
3242 | } |
3243 | |
3244 | void PhysicalBone3D::set_joint_rotation(const Vector3 &p_euler_rad) { |
3245 | joint_offset.basis.set_euler_scale(p_euler_rad, joint_offset.basis.get_scale()); |
3246 | |
3247 | _update_joint_offset(); |
3248 | } |
3249 | |
3250 | Vector3 PhysicalBone3D::get_joint_rotation() const { |
3251 | return joint_offset.basis.get_euler_normalized(); |
3252 | } |
3253 | |
3254 | const Transform3D &PhysicalBone3D::get_body_offset() const { |
3255 | return body_offset; |
3256 | } |
3257 | |
3258 | void PhysicalBone3D::set_body_offset(const Transform3D &p_offset) { |
3259 | body_offset = p_offset; |
3260 | body_offset_inverse = body_offset.affine_inverse(); |
3261 | |
3262 | _update_joint_offset(); |
3263 | } |
3264 | |
3265 | void PhysicalBone3D::set_simulate_physics(bool p_simulate) { |
3266 | if (simulate_physics == p_simulate) { |
3267 | return; |
3268 | } |
3269 | |
3270 | simulate_physics = p_simulate; |
3271 | reset_physics_simulation_state(); |
3272 | } |
3273 | |
3274 | bool PhysicalBone3D::get_simulate_physics() { |
3275 | return simulate_physics; |
3276 | } |
3277 | |
3278 | bool PhysicalBone3D::is_simulating_physics() { |
3279 | return _internal_simulate_physics; |
3280 | } |
3281 | |
3282 | void PhysicalBone3D::set_bone_name(const String &p_name) { |
3283 | bone_name = p_name; |
3284 | bone_id = -1; |
3285 | |
3286 | update_bone_id(); |
3287 | reset_to_rest_position(); |
3288 | } |
3289 | |
3290 | const String &PhysicalBone3D::get_bone_name() const { |
3291 | return bone_name; |
3292 | } |
3293 | |
3294 | void PhysicalBone3D::set_mass(real_t p_mass) { |
3295 | ERR_FAIL_COND(p_mass <= 0); |
3296 | mass = p_mass; |
3297 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_MASS, mass); |
3298 | } |
3299 | |
3300 | real_t PhysicalBone3D::get_mass() const { |
3301 | return mass; |
3302 | } |
3303 | |
3304 | void PhysicalBone3D::set_friction(real_t p_friction) { |
3305 | ERR_FAIL_COND(p_friction < 0 || p_friction > 1); |
3306 | |
3307 | friction = p_friction; |
3308 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, friction); |
3309 | } |
3310 | |
3311 | real_t PhysicalBone3D::get_friction() const { |
3312 | return friction; |
3313 | } |
3314 | |
3315 | void PhysicalBone3D::set_bounce(real_t p_bounce) { |
3316 | ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1); |
3317 | |
3318 | bounce = p_bounce; |
3319 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, bounce); |
3320 | } |
3321 | |
3322 | real_t PhysicalBone3D::get_bounce() const { |
3323 | return bounce; |
3324 | } |
3325 | |
3326 | void PhysicalBone3D::set_gravity_scale(real_t p_gravity_scale) { |
3327 | gravity_scale = p_gravity_scale; |
3328 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, gravity_scale); |
3329 | } |
3330 | |
3331 | real_t PhysicalBone3D::get_gravity_scale() const { |
3332 | return gravity_scale; |
3333 | } |
3334 | |
3335 | void PhysicalBone3D::set_linear_damp_mode(DampMode p_mode) { |
3336 | linear_damp_mode = p_mode; |
3337 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode); |
3338 | } |
3339 | |
3340 | PhysicalBone3D::DampMode PhysicalBone3D::get_linear_damp_mode() const { |
3341 | return linear_damp_mode; |
3342 | } |
3343 | |
3344 | void PhysicalBone3D::set_angular_damp_mode(DampMode p_mode) { |
3345 | angular_damp_mode = p_mode; |
3346 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode); |
3347 | } |
3348 | |
3349 | PhysicalBone3D::DampMode PhysicalBone3D::get_angular_damp_mode() const { |
3350 | return angular_damp_mode; |
3351 | } |
3352 | |
3353 | void PhysicalBone3D::set_linear_damp(real_t p_linear_damp) { |
3354 | ERR_FAIL_COND(p_linear_damp < 0); |
3355 | |
3356 | linear_damp = p_linear_damp; |
3357 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP, linear_damp); |
3358 | } |
3359 | |
3360 | real_t PhysicalBone3D::get_linear_damp() const { |
3361 | return linear_damp; |
3362 | } |
3363 | |
3364 | void PhysicalBone3D::set_angular_damp(real_t p_angular_damp) { |
3365 | ERR_FAIL_COND(p_angular_damp < 0); |
3366 | |
3367 | angular_damp = p_angular_damp; |
3368 | PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP, angular_damp); |
3369 | } |
3370 | |
3371 | real_t PhysicalBone3D::get_angular_damp() const { |
3372 | return angular_damp; |
3373 | } |
3374 | |
3375 | void PhysicalBone3D::set_can_sleep(bool p_active) { |
3376 | can_sleep = p_active; |
3377 | PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_CAN_SLEEP, p_active); |
3378 | } |
3379 | |
3380 | bool PhysicalBone3D::is_able_to_sleep() const { |
3381 | return can_sleep; |
3382 | } |
3383 | |
3384 | PhysicalBone3D::PhysicalBone3D() : |
3385 | PhysicsBody3D(PhysicsServer3D::BODY_MODE_STATIC) { |
3386 | joint = PhysicsServer3D::get_singleton()->joint_create(); |
3387 | reset_physics_simulation_state(); |
3388 | } |
3389 | |
3390 | PhysicalBone3D::~PhysicalBone3D() { |
3391 | if (joint_data) { |
3392 | memdelete(joint_data); |
3393 | } |
3394 | ERR_FAIL_NULL(PhysicsServer3D::get_singleton()); |
3395 | PhysicsServer3D::get_singleton()->free(joint); |
3396 | } |
3397 | |
3398 | void PhysicalBone3D::update_bone_id() { |
3399 | if (!parent_skeleton) { |
3400 | return; |
3401 | } |
3402 | |
3403 | const int new_bone_id = parent_skeleton->find_bone(bone_name); |
3404 | |
3405 | if (new_bone_id != bone_id) { |
3406 | if (-1 != bone_id) { |
3407 | // Assert the unbind from old node |
3408 | parent_skeleton->unbind_physical_bone_from_bone(bone_id); |
3409 | } |
3410 | |
3411 | bone_id = new_bone_id; |
3412 | |
3413 | parent_skeleton->bind_physical_bone_to_bone(bone_id, this); |
3414 | |
3415 | _fix_joint_offset(); |
3416 | reset_physics_simulation_state(); |
3417 | } |
3418 | } |
3419 | |
3420 | void PhysicalBone3D::update_offset() { |
3421 | #ifdef TOOLS_ENABLED |
3422 | if (parent_skeleton) { |
3423 | Transform3D bone_transform(parent_skeleton->get_global_transform()); |
3424 | if (-1 != bone_id) { |
3425 | bone_transform *= parent_skeleton->get_bone_global_pose(bone_id); |
3426 | } |
3427 | |
3428 | if (gizmo_move_joint) { |
3429 | bone_transform *= body_offset; |
3430 | set_joint_offset(bone_transform.affine_inverse() * get_global_transform()); |
3431 | } else { |
3432 | set_body_offset(bone_transform.affine_inverse() * get_global_transform()); |
3433 | } |
3434 | } |
3435 | #endif |
3436 | } |
3437 | |
3438 | void PhysicalBone3D::_start_physics_simulation() { |
3439 | if (_internal_simulate_physics || !parent_skeleton) { |
3440 | return; |
3441 | } |
3442 | reset_to_rest_position(); |
3443 | set_body_mode(PhysicsServer3D::BODY_MODE_RIGID); |
3444 | PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer()); |
3445 | PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask()); |
3446 | PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority()); |
3447 | PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &PhysicalBone3D::_body_state_changed)); |
3448 | set_as_top_level(true); |
3449 | _internal_simulate_physics = true; |
3450 | } |
3451 | |
3452 | void PhysicalBone3D::_stop_physics_simulation() { |
3453 | if (!parent_skeleton) { |
3454 | return; |
3455 | } |
3456 | if (parent_skeleton->get_animate_physical_bones()) { |
3457 | set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC); |
3458 | PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer()); |
3459 | PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask()); |
3460 | PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority()); |
3461 | } else { |
3462 | set_body_mode(PhysicsServer3D::BODY_MODE_STATIC); |
3463 | PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), 0); |
3464 | PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), 0); |
3465 | PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), 1.0); |
3466 | } |
3467 | if (_internal_simulate_physics) { |
3468 | PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), Callable()); |
3469 | parent_skeleton->set_bone_global_pose_override(bone_id, Transform3D(), 0.0, false); |
3470 | set_as_top_level(false); |
3471 | _internal_simulate_physics = false; |
3472 | } |
3473 | } |
3474 | |