Debian Codegraph @

scene/3d/physics/animatable_body_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* animatable_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 "animatable_body_3d.h"
32
33 Vector3 AnimatableBody3D::get_linear_velocity() const {
34 return linear_velocity;
35 }
36
37 Vector3 AnimatableBody3D::get_angular_velocity() const {
38 return angular_velocity;
39 }
40
41 void AnimatableBody3D::set_sync_to_physics(bool p_enable) {
42 if (sync_to_physics == p_enable) {
43 return;
44 }
45
46 sync_to_physics = p_enable;
47
48 _update_kinematic_motion();
49 }
50
51 bool AnimatableBody3D::is_sync_to_physics_enabled() const {
52 return sync_to_physics;
53 }
54
55 void AnimatableBody3D::_update_kinematic_motion() {
56 #ifdef TOOLS_ENABLED
57 if (Engine::get_singleton()->is_editor_hint()) {
58 return;
59 }
60 #endif
61
62 if (sync_to_physics) {
63 set_only_update_transform_changes(true);
64 set_notify_local_transform(true);
65 } else {
66 set_only_update_transform_changes(false);
67 set_notify_local_transform(false);
68 }
69 }
70
71 void AnimatableBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
72 linear_velocity = p_state->get_linear_velocity();
73 angular_velocity = p_state->get_angular_velocity();
74
75 if (!sync_to_physics) {
76 return;
77 }
78
79 last_valid_transform = p_state->get_transform();
80 set_notify_local_transform(false);
81 set_global_transform(last_valid_transform);
82 set_notify_local_transform(true);
83 _on_transform_changed();
84 }
85
86 void AnimatableBody3D::_notification(int p_what) {
87 #ifdef TOOLS_ENABLED
88 if (Engine::get_singleton()->is_editor_hint()) {
89 return;
90 }
91 #endif
92 switch (p_what) {
93 case NOTIFICATION_ENTER_TREE: {
94 last_valid_transform = get_global_transform();
95 _update_kinematic_motion();
96 } break;
97
98 case NOTIFICATION_EXIT_TREE: {
99 set_only_update_transform_changes(false);
100 set_notify_local_transform(false);
101 } break;
102
103 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
104 // Used by sync to physics, send the new transform to the physics...
105 Transform3D new_transform = get_global_transform();
106
107 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_TRANSFORM, new_transform);
108
109 // ... but then revert changes.
110 set_notify_local_transform(false);
111 set_global_transform(last_valid_transform);
112 set_notify_local_transform(true);
113 _on_transform_changed();
114 } break;
115 }
116 }
117
118 void AnimatableBody3D::_bind_methods() {
119 ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &AnimatableBody3D::set_sync_to_physics);
120 ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &AnimatableBody3D::is_sync_to_physics_enabled);
121
122 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
123 }
124
125 AnimatableBody3D::AnimatableBody3D() :
126 StaticBody3D(PhysicsServer3D::BODY_MODE_KINEMATIC) {
127 PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &AnimatableBody3D::_body_state_changed));
128 }