Debian Codegraph @

scene/3d/physics/joints/joint_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* joint_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 "joint_3d.h"
32
33 void Joint3D::_disconnect_signals() {
34 Node *node_a = get_node_or_null(a);
35 PhysicsBody3D *body_a = Object::cast_to<PhysicsBody3D>(node_a);
36 if (body_a) {
37 body_a->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint3D::_body_exit_tree));
38 }
39
40 Node *node_b = get_node_or_null(b);
41 PhysicsBody3D *body_b = Object::cast_to<PhysicsBody3D>(node_b);
42 if (body_b) {
43 body_b->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint3D::_body_exit_tree));
44 }
45 }
46
47 void Joint3D::_body_exit_tree() {
48 _disconnect_signals();
49 _update_joint(true);
50 update_configuration_warnings();
51 }
52
53 void Joint3D::_update_joint(bool p_only_free) {
54 if (ba.is_valid() && bb.is_valid()) {
55 PhysicsServer3D::get_singleton()->body_remove_collision_exception(ba, bb);
56 PhysicsServer3D::get_singleton()->body_remove_collision_exception(bb, ba);
57 }
58
59 ba = RID();
60 bb = RID();
61
62 configured = false;
63
64 if (p_only_free || !is_inside_tree()) {
65 PhysicsServer3D::get_singleton()->joint_clear(joint);
66 warning = String();
67 return;
68 }
69
70 Node *node_a = get_node_or_null(a);
71 Node *node_b = get_node_or_null(b);
72
73 PhysicsBody3D *body_a = Object::cast_to<PhysicsBody3D>(node_a);
74 PhysicsBody3D *body_b = Object::cast_to<PhysicsBody3D>(node_b);
75
76 if (node_a && !body_a && node_b && !body_b) {
77 warning = RTR("Node A and Node B must be PhysicsBody3Ds");
78 } else if (node_a && !body_a) {
79 warning = RTR("Node A must be a PhysicsBody3D");
80 } else if (node_b && !body_b) {
81 warning = RTR("Node B must be a PhysicsBody3D");
82 } else if (!body_a && !body_b) {
83 warning = RTR("Joint is not connected to any PhysicsBody3Ds");
84 } else if (body_a == body_b) {
85 warning = RTR("Node A and Node B must be different PhysicsBody3Ds");
86 } else {
87 warning = String();
88 }
89
90 update_configuration_warnings();
91
92 if (!warning.is_empty()) {
93 PhysicsServer3D::get_singleton()->joint_clear(joint);
94 return;
95 }
96
97 configured = true;
98
99 if (body_a) {
100 _configure_joint(joint, body_a, body_b);
101 } else if (body_b) {
102 _configure_joint(joint, body_b, nullptr);
103 }
104
105 PhysicsServer3D::get_singleton()->joint_set_solver_priority(joint, solver_priority);
106
107 if (body_a) {
108 ba = body_a->get_rid();
109 if (!body_a->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint3D::_body_exit_tree))) {
110 body_a->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint3D::_body_exit_tree));
111 }
112 }
113
114 if (body_b) {
115 bb = body_b->get_rid();
116 if (!body_b->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint3D::_body_exit_tree))) {
117 body_b->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint3D::_body_exit_tree));
118 }
119 }
120
121 PhysicsServer3D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);
122 }
123
124 void Joint3D::set_node_a(const NodePath &p_node_a) {
125 if (a == p_node_a) {
126 return;
127 }
128
129 if (is_configured()) {
130 _disconnect_signals();
131 }
132
133 a = p_node_a;
134 _update_joint();
135 }
136
137 NodePath Joint3D::get_node_a() const {
138 return a;
139 }
140
141 void Joint3D::set_node_b(const NodePath &p_node_b) {
142 if (b == p_node_b) {
143 return;
144 }
145
146 if (is_configured()) {
147 _disconnect_signals();
148 }
149
150 b = p_node_b;
151 _update_joint();
152 }
153
154 NodePath Joint3D::get_node_b() const {
155 return b;
156 }
157
158 void Joint3D::set_solver_priority(int p_priority) {
159 solver_priority = p_priority;
160 if (joint.is_valid()) {
161 PhysicsServer3D::get_singleton()->joint_set_solver_priority(joint, solver_priority);
162 }
163 }
164
165 int Joint3D::get_solver_priority() const {
166 return solver_priority;
167 }
168
169 void Joint3D::_notification(int p_what) {
170 switch (p_what) {
171 case NOTIFICATION_POST_ENTER_TREE: {
172 if (is_configured()) {
173 _disconnect_signals();
174 }
175 _update_joint();
176 } break;
177
178 case NOTIFICATION_EXIT_TREE: {
179 if (is_configured()) {
180 _disconnect_signals();
181 }
182 _update_joint(true);
183 } break;
184 }
185 }
186
187 void Joint3D::set_exclude_nodes_from_collision(bool p_enable) {
188 if (exclude_from_collision == p_enable) {
189 return;
190 }
191 if (is_configured()) {
192 _disconnect_signals();
193 }
194 _update_joint(true);
195 exclude_from_collision = p_enable;
196 _update_joint();
197 }
198
199 bool Joint3D::get_exclude_nodes_from_collision() const {
200 return exclude_from_collision;
201 }
202
203 PackedStringArray Joint3D::get_configuration_warnings() const {
204 PackedStringArray warnings = Node3D::get_configuration_warnings();
205
206 if (!warning.is_empty()) {
207 warnings.push_back(warning);
208 }
209
210 return warnings;
211 }
212
213 void Joint3D::_bind_methods() {
214 ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint3D::set_node_a);
215 ClassDB::bind_method(D_METHOD("get_node_a"), &Joint3D::get_node_a);
216
217 ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint3D::set_node_b);
218 ClassDB::bind_method(D_METHOD("get_node_b"), &Joint3D::get_node_b);
219
220 ClassDB::bind_method(D_METHOD("set_solver_priority", "priority"), &Joint3D::set_solver_priority);
221 ClassDB::bind_method(D_METHOD("get_solver_priority"), &Joint3D::get_solver_priority);
222
223 ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint3D::set_exclude_nodes_from_collision);
224 ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint3D::get_exclude_nodes_from_collision);
225
226 ClassDB::bind_method(D_METHOD("get_rid"), &Joint3D::get_rid);
227
228 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody3D"), "set_node_a", "get_node_a");
229 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody3D"), "set_node_b", "get_node_b");
230 ADD_PROPERTY(PropertyInfo(Variant::INT, "solver_priority", PROPERTY_HINT_RANGE, "1,8,1"), "set_solver_priority", "get_solver_priority");
231
232 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_nodes_from_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
233 }
234
235 Joint3D::Joint3D() {
236 set_notify_transform(true);
237 joint = PhysicsServer3D::get_singleton()->joint_create();
238 }
239
240 Joint3D::~Joint3D() {
241 ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
242 PhysicsServer3D::get_singleton()->free_rid(joint);
243 }