Debian Codegraph @

scene/3d/physics/collision_shape_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* collision_shape_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 "collision_shape_3d.h"
32
33 #include "scene/3d/mesh_instance_3d.h"
34 #include "scene/3d/physics/character_body_3d.h"
35 #include "scene/3d/physics/vehicle_body_3d.h"
36 #include "scene/resources/3d/concave_polygon_shape_3d.h"
37 #include "scene/resources/3d/convex_polygon_shape_3d.h"
38 #include "scene/resources/3d/world_boundary_shape_3d.h"
39
40 void CollisionShape3D::make_convex_from_siblings() {
41 Node *p = get_parent();
42 if (!p) {
43 return;
44 }
45
46 Vector<Vector3> vertices;
47
48 for (int i = 0; i < p->get_child_count(); i++) {
49 Node *n = p->get_child(i);
50 MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(n);
51 if (mi) {
52 Ref<Mesh> m = mi->get_mesh();
53 if (m.is_valid()) {
54 for (int j = 0; j < m->get_surface_count(); j++) {
55 Array a = m->surface_get_arrays(j);
56 if (!a.is_empty()) {
57 Vector<Vector3> v = a[RenderingServer::ARRAY_VERTEX];
58 for (int k = 0; k < v.size(); k++) {
59 vertices.append(mi->get_transform().xform(v[k]));
60 }
61 }
62 }
63 }
64 }
65 }
66
67 Ref<ConvexPolygonShape3D> shape_new = memnew(ConvexPolygonShape3D);
68 shape_new->set_points(vertices);
69 set_shape(shape_new);
70 }
71
72 void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
73 collision_object->shape_owner_set_transform(owner_id, get_transform());
74 if (p_xform_only) {
75 return;
76 }
77 collision_object->shape_owner_set_disabled(owner_id, disabled);
78 }
79
80 void CollisionShape3D::_notification(int p_what) {
81 switch (p_what) {
82 case NOTIFICATION_PARENTED: {
83 collision_object = Object::cast_to<CollisionObject3D>(get_parent());
84 if (collision_object) {
85 owner_id = collision_object->create_shape_owner(this);
86 if (shape.is_valid()) {
87 collision_object->shape_owner_add_shape(owner_id, shape);
88 }
89
90 _update_in_shape_owner();
91 }
92 } break;
93
94 case NOTIFICATION_ENTER_TREE: {
95 if (collision_object) {
96 _update_in_shape_owner();
97 }
98 } break;
99
100 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
101 if (collision_object) {
102 _update_in_shape_owner(true);
103 }
104 update_configuration_warnings();
105 } break;
106
107 case NOTIFICATION_UNPARENTED: {
108 if (collision_object) {
109 collision_object->remove_shape_owner(owner_id);
110 }
111 owner_id = 0;
112 collision_object = nullptr;
113 } break;
114 }
115 }
116
117 #ifndef DISABLE_DEPRECATED
118 void CollisionShape3D::resource_changed(Ref<Resource> res) {
119 }
120 #endif
121
122 PackedStringArray CollisionShape3D::get_configuration_warnings() const {
123 PackedStringArray warnings = Node3D::get_configuration_warnings();
124
125 CollisionObject3D *col_object = Object::cast_to<CollisionObject3D>(get_parent());
126 if (col_object == nullptr) {
127 warnings.push_back(RTR("CollisionShape3D only serves to provide a collision shape to a CollisionObject3D derived node.\nPlease only use it as a child of Area3D, StaticBody3D, RigidBody3D, CharacterBody3D, etc. to give them a shape."));
128 }
129
130 if (shape.is_null()) {
131 warnings.push_back(RTR("A shape must be provided for CollisionShape3D to function. Please create a shape resource for it."));
132 }
133
134 if (shape.is_valid() && Object::cast_to<RigidBody3D>(col_object)) {
135 String body_type = "RigidBody3D";
136 if (Object::cast_to<VehicleBody3D>(col_object)) {
137 body_type = "VehicleBody3D";
138 }
139
140 if (Object::cast_to<ConcavePolygonShape3D>(*shape)) {
141 warnings.push_back(vformat(RTR("When used for collision, ConcavePolygonShape3D is intended to work with static CollisionObject3D nodes like StaticBody3D.\nIt will likely not behave well for %ss (except when frozen and freeze_mode set to FREEZE_MODE_STATIC)."), body_type));
142 } else if (Object::cast_to<WorldBoundaryShape3D>(*shape)) {
143 warnings.push_back(RTR("WorldBoundaryShape3D doesn't support RigidBody3D in another mode than static."));
144 }
145 }
146
147 if (shape.is_valid() && Object::cast_to<CharacterBody3D>(col_object)) {
148 if (Object::cast_to<ConcavePolygonShape3D>(*shape)) {
149 warnings.push_back(RTR("When used for collision, ConcavePolygonShape3D is intended to work with static CollisionObject3D nodes like StaticBody3D.\nIt will likely not behave well for CharacterBody3Ds."));
150 }
151 }
152
153 Vector3 scale = get_transform().get_basis().get_scale();
154 if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
155 warnings.push_back(RTR("A non-uniformly scaled CollisionShape3D node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change the size of its shape resource instead."));
156 }
157
158 return warnings;
159 }
160
161 void CollisionShape3D::_bind_methods() {
162 #ifndef DISABLE_DEPRECATED
163 ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape3D::resource_changed);
164 #endif
165 ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape3D::set_shape);
166 ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape3D::get_shape);
167 ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape3D::set_disabled);
168 ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape3D::is_disabled);
169
170 ClassDB::bind_method(D_METHOD("make_convex_from_siblings"), &CollisionShape3D::make_convex_from_siblings);
171 ClassDB::set_method_flags("CollisionShape3D", "make_convex_from_siblings", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
172
173 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
174 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
175
176 ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape3D::set_debug_color);
177 ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape3D::get_debug_color);
178
179 ClassDB::bind_method(D_METHOD("set_enable_debug_fill", "enable"), &CollisionShape3D::set_debug_fill_enabled);
180 ClassDB::bind_method(D_METHOD("get_enable_debug_fill"), &CollisionShape3D::get_debug_fill_enabled);
181
182 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
183 // Default value depends on a project setting, override for doc generation purposes.
184 ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
185
186 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_fill"), "set_enable_debug_fill", "get_enable_debug_fill");
187 }
188
189 void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
190 if (p_shape == shape) {
191 return;
192 }
193 if (shape.is_valid()) {
194 #ifdef DEBUG_ENABLED
195 shape->disconnect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
196 #endif // DEBUG_ENABLED
197 shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
198 }
199 shape = p_shape;
200 if (shape.is_valid()) {
201 #ifdef DEBUG_ENABLED
202 if (shape->are_debug_properties_edited()) {
203 set_debug_color(shape->get_debug_color());
204 set_debug_fill_enabled(shape->get_debug_fill());
205 } else {
206 shape->set_debug_color(debug_color);
207 shape->set_debug_fill(debug_fill);
208 }
209 #endif // DEBUG_ENABLED
210
211 shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
212 #ifdef DEBUG_ENABLED
213 shape->connect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
214 #endif // DEBUG_ENABLED
215 }
216 update_gizmos();
217 if (collision_object) {
218 collision_object->shape_owner_clear_shapes(owner_id);
219 if (shape.is_valid()) {
220 collision_object->shape_owner_add_shape(owner_id, shape);
221 }
222 }
223
224 if (is_inside_tree() && collision_object) {
225 // If this is a heightfield shape our center may have changed
226 _update_in_shape_owner(true);
227 }
228 update_configuration_warnings();
229 }
230
231 Ref<Shape3D> CollisionShape3D::get_shape() const {
232 return shape;
233 }
234
235 void CollisionShape3D::set_disabled(bool p_disabled) {
236 disabled = p_disabled;
237 update_gizmos();
238 if (collision_object) {
239 collision_object->shape_owner_set_disabled(owner_id, p_disabled);
240 }
241 }
242
243 bool CollisionShape3D::is_disabled() const {
244 return disabled;
245 }
246
247 Color CollisionShape3D::_get_default_debug_color() const {
248 const SceneTree *st = SceneTree::get_singleton();
249 return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
250 }
251
252 void CollisionShape3D::set_debug_color(const Color &p_color) {
253 if (debug_color == p_color) {
254 return;
255 }
256
257 debug_color = p_color;
258
259 if (shape.is_valid()) {
260 shape->set_debug_color(p_color);
261 }
262 }
263
264 Color CollisionShape3D::get_debug_color() const {
265 return debug_color;
266 }
267
268 void CollisionShape3D::set_debug_fill_enabled(bool p_enable) {
269 if (debug_fill == p_enable) {
270 return;
271 }
272
273 debug_fill = p_enable;
274
275 if (shape.is_valid()) {
276 shape->set_debug_fill(p_enable);
277 }
278 }
279
280 bool CollisionShape3D::get_debug_fill_enabled() const {
281 return debug_fill;
282 }
283
284 #ifdef DEBUG_ENABLED
285
286 bool CollisionShape3D::_property_can_revert(const StringName &p_name) const {
287 if (p_name == "debug_color") {
288 return true;
289 }
290 return false;
291 }
292
293 bool CollisionShape3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
294 if (p_name == "debug_color") {
295 r_property = _get_default_debug_color();
296 return true;
297 }
298 return false;
299 }
300
301 void CollisionShape3D::_validate_property(PropertyInfo &p_property) const {
302 if (p_property.name == "debug_color") {
303 if (debug_color == _get_default_debug_color()) {
304 p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;
305 } else {
306 p_property.usage = PROPERTY_USAGE_DEFAULT;
307 }
308 }
309 }
310
311 void CollisionShape3D::_shape_changed() {
312 if (shape->get_debug_color() != debug_color) {
313 set_debug_color(shape->get_debug_color());
314 }
315 if (shape->get_debug_fill() != debug_fill) {
316 set_debug_fill_enabled(shape->get_debug_fill());
317 }
318 }
319
320 #endif // DEBUG_ENABLED
321
322 CollisionShape3D::CollisionShape3D() {
323 //indicator = RenderingServer::get_singleton()->mesh_create();
324 set_notify_local_transform(true);
325 debug_color = _get_default_debug_color();
326 }
327
328 CollisionShape3D::~CollisionShape3D() {
329 //RenderingServer::get_singleton()->free(indicator);
330 }