Debian Codegraph @

scene/3d/physics/ray_cast_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* ray_cast_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 "ray_cast_3d.h"
32
33 #include "scene/3d/physics/collision_object_3d.h"
34 #include "scene/resources/mesh.h"
35
36 void RayCast3D::set_target_position(const Vector3 &p_point) {
37 target_position = p_point;
38 update_gizmos();
39
40 if (Engine::get_singleton()->is_editor_hint()) {
41 if (is_inside_tree()) {
42 _update_debug_shape_vertices();
43 }
44 } else if (debug_instance.is_valid()) {
45 _update_debug_shape();
46 }
47 }
48
49 Vector3 RayCast3D::get_target_position() const {
50 return target_position;
51 }
52
53 void RayCast3D::set_collision_mask(uint32_t p_mask) {
54 collision_mask = p_mask;
55 }
56
57 uint32_t RayCast3D::get_collision_mask() const {
58 return collision_mask;
59 }
60
61 void RayCast3D::set_collision_mask_value(int p_layer_number, bool p_value) {
62 ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
63 ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
64 uint32_t mask = get_collision_mask();
65 if (p_value) {
66 mask |= 1 << (p_layer_number - 1);
67 } else {
68 mask &= ~(1 << (p_layer_number - 1));
69 }
70 set_collision_mask(mask);
71 }
72
73 bool RayCast3D::get_collision_mask_value(int p_layer_number) const {
74 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
75 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
76 return get_collision_mask() & (1 << (p_layer_number - 1));
77 }
78
79 bool RayCast3D::is_colliding() const {
80 return collided;
81 }
82
83 Object *RayCast3D::get_collider() const {
84 if (against.is_null()) {
85 return nullptr;
86 }
87
88 return ObjectDB::get_instance(against);
89 }
90
91 RID RayCast3D::get_collider_rid() const {
92 return against_rid;
93 }
94
95 int RayCast3D::get_collider_shape() const {
96 return against_shape;
97 }
98
99 Vector3 RayCast3D::get_collision_point() const {
100 return collision_point;
101 }
102
103 Vector3 RayCast3D::get_collision_normal() const {
104 return collision_normal;
105 }
106
107 int RayCast3D::get_collision_face_index() const {
108 return collision_face_index;
109 }
110
111 void RayCast3D::set_enabled(bool p_enabled) {
112 enabled = p_enabled;
113 update_gizmos();
114
115 if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
116 set_physics_process_internal(p_enabled);
117 }
118 if (!p_enabled) {
119 collided = false;
120 }
121
122 if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) {
123 if (p_enabled) {
124 _update_debug_shape();
125 } else {
126 _clear_debug_shape();
127 }
128 }
129 }
130
131 bool RayCast3D::is_enabled() const {
132 return enabled;
133 }
134
135 void RayCast3D::set_exclude_parent_body(bool p_exclude_parent_body) {
136 if (exclude_parent_body == p_exclude_parent_body) {
137 return;
138 }
139
140 exclude_parent_body = p_exclude_parent_body;
141
142 if (!is_inside_tree()) {
143 return;
144 }
145
146 if (Object::cast_to<CollisionObject3D>(get_parent())) {
147 if (exclude_parent_body) {
148 exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
149 } else {
150 exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
151 }
152 }
153 }
154
155 bool RayCast3D::get_exclude_parent_body() const {
156 return exclude_parent_body;
157 }
158
159 void RayCast3D::_notification(int p_what) {
160 switch (p_what) {
161 case NOTIFICATION_ENTER_TREE: {
162 if (Engine::get_singleton()->is_editor_hint()) {
163 _update_debug_shape_vertices();
164 }
165 if (enabled && !Engine::get_singleton()->is_editor_hint()) {
166 set_physics_process_internal(true);
167 } else {
168 set_physics_process_internal(false);
169 }
170
171 if (get_tree()->is_debugging_collisions_hint()) {
172 _update_debug_shape();
173 }
174
175 if (Object::cast_to<CollisionObject3D>(get_parent())) {
176 if (exclude_parent_body) {
177 exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
178 } else {
179 exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
180 }
181 }
182 } break;
183
184 case NOTIFICATION_EXIT_TREE: {
185 if (enabled) {
186 set_physics_process_internal(false);
187 }
188
189 if (debug_instance.is_valid()) {
190 _clear_debug_shape();
191 }
192 } break;
193
194 case NOTIFICATION_VISIBILITY_CHANGED: {
195 if (is_inside_tree() && debug_instance.is_valid()) {
196 RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
197 }
198 } break;
199
200 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
201 if (!enabled) {
202 break;
203 }
204
205 bool prev_collision_state = collided;
206 _update_raycast_state();
207 if (get_tree()->is_debugging_collisions_hint()) {
208 if (prev_collision_state != collided) {
209 _update_debug_shape_material(true);
210 }
211 if (is_inside_tree() && debug_instance.is_valid()) {
212 RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
213 }
214 }
215 } break;
216 }
217 }
218
219 void RayCast3D::_update_raycast_state() {
220 Ref<World3D> w3d = get_world_3d();
221 ERR_FAIL_COND(w3d.is_null());
222
223 PhysicsDirectSpaceState3D *dss = PhysicsServer3D::get_singleton()->space_get_direct_state(w3d->get_space());
224 ERR_FAIL_NULL(dss);
225
226 Transform3D gt = get_global_transform();
227
228 Vector3 to = target_position;
229 if (to == Vector3()) {
230 to = Vector3(0, 0.01, 0);
231 }
232
233 PhysicsDirectSpaceState3D::RayParameters ray_params;
234 ray_params.from = gt.get_origin();
235 ray_params.to = gt.xform(to);
236 ray_params.exclude = exclude;
237 ray_params.collision_mask = collision_mask;
238 ray_params.collide_with_bodies = collide_with_bodies;
239 ray_params.collide_with_areas = collide_with_areas;
240 ray_params.hit_from_inside = hit_from_inside;
241 ray_params.hit_back_faces = hit_back_faces;
242
243 PhysicsDirectSpaceState3D::RayResult rr;
244 if (dss->intersect_ray(ray_params, rr)) {
245 collided = true;
246 against = rr.collider_id;
247 against_rid = rr.rid;
248 collision_point = rr.position;
249 collision_normal = rr.normal;
250 collision_face_index = rr.face_index;
251 against_shape = rr.shape;
252 } else {
253 collided = false;
254 against = ObjectID();
255 against_rid = RID();
256 against_shape = 0;
257 }
258 }
259
260 void RayCast3D::force_raycast_update() {
261 _update_raycast_state();
262 }
263
264 void RayCast3D::add_exception_rid(const RID &p_rid) {
265 exclude.insert(p_rid);
266 }
267
268 void RayCast3D::add_exception(RequiredParam<const CollisionObject3D> rp_node) {
269 EXTRACT_PARAM_OR_FAIL_MSG(p_node, rp_node, "The passed Node must be an instance of CollisionObject3D.");
270 add_exception_rid(p_node->get_rid());
271 }
272
273 void RayCast3D::remove_exception_rid(const RID &p_rid) {
274 exclude.erase(p_rid);
275 }
276
277 void RayCast3D::remove_exception(RequiredParam<const CollisionObject3D> rp_node) {
278 EXTRACT_PARAM_OR_FAIL_MSG(p_node, rp_node, "The passed Node must be an instance of CollisionObject3D.");
279 remove_exception_rid(p_node->get_rid());
280 }
281
282 void RayCast3D::clear_exceptions() {
283 exclude.clear();
284
285 if (exclude_parent_body && is_inside_tree()) {
286 CollisionObject3D *parent = Object::cast_to<CollisionObject3D>(get_parent());
287 if (parent) {
288 exclude.insert(parent->get_rid());
289 }
290 }
291 }
292
293 void RayCast3D::set_collide_with_areas(bool p_enabled) {
294 collide_with_areas = p_enabled;
295 }
296
297 bool RayCast3D::is_collide_with_areas_enabled() const {
298 return collide_with_areas;
299 }
300
301 void RayCast3D::set_collide_with_bodies(bool p_enabled) {
302 collide_with_bodies = p_enabled;
303 }
304
305 bool RayCast3D::is_collide_with_bodies_enabled() const {
306 return collide_with_bodies;
307 }
308
309 void RayCast3D::set_hit_from_inside(bool p_enabled) {
310 hit_from_inside = p_enabled;
311 }
312
313 bool RayCast3D::is_hit_from_inside_enabled() const {
314 return hit_from_inside;
315 }
316
317 void RayCast3D::set_hit_back_faces(bool p_enabled) {
318 hit_back_faces = p_enabled;
319 }
320
321 bool RayCast3D::is_hit_back_faces_enabled() const {
322 return hit_back_faces;
323 }
324
325 void RayCast3D::_bind_methods() {
326 ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast3D::set_enabled);
327 ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast3D::is_enabled);
328
329 ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &RayCast3D::set_target_position);
330 ClassDB::bind_method(D_METHOD("get_target_position"), &RayCast3D::get_target_position);
331
332 ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast3D::is_colliding);
333 ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast3D::force_raycast_update);
334
335 ClassDB::bind_method(D_METHOD("get_collider"), &RayCast3D::get_collider);
336 ClassDB::bind_method(D_METHOD("get_collider_rid"), &RayCast3D::get_collider_rid);
337 ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast3D::get_collider_shape);
338 ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast3D::get_collision_point);
339 ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast3D::get_collision_normal);
340 ClassDB::bind_method(D_METHOD("get_collision_face_index"), &RayCast3D::get_collision_face_index);
341
342 ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast3D::add_exception_rid);
343 ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast3D::add_exception);
344
345 ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast3D::remove_exception_rid);
346 ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast3D::remove_exception);
347
348 ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast3D::clear_exceptions);
349
350 ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast3D::set_collision_mask);
351 ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast3D::get_collision_mask);
352
353 ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &RayCast3D::set_collision_mask_value);
354 ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &RayCast3D::get_collision_mask_value);
355
356 ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast3D::set_exclude_parent_body);
357 ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast3D::get_exclude_parent_body);
358
359 ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast3D::set_collide_with_areas);
360 ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast3D::is_collide_with_areas_enabled);
361
362 ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast3D::set_collide_with_bodies);
363 ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast3D::is_collide_with_bodies_enabled);
364
365 ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &RayCast3D::set_hit_from_inside);
366 ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &RayCast3D::is_hit_from_inside_enabled);
367
368 ClassDB::bind_method(D_METHOD("set_hit_back_faces", "enable"), &RayCast3D::set_hit_back_faces);
369 ClassDB::bind_method(D_METHOD("is_hit_back_faces_enabled"), &RayCast3D::is_hit_back_faces_enabled);
370
371 ClassDB::bind_method(D_METHOD("set_debug_shape_custom_color", "debug_shape_custom_color"), &RayCast3D::set_debug_shape_custom_color);
372 ClassDB::bind_method(D_METHOD("get_debug_shape_custom_color"), &RayCast3D::get_debug_shape_custom_color);
373
374 ClassDB::bind_method(D_METHOD("set_debug_shape_thickness", "debug_shape_thickness"), &RayCast3D::set_debug_shape_thickness);
375 ClassDB::bind_method(D_METHOD("get_debug_shape_thickness"), &RayCast3D::get_debug_shape_thickness);
376
377 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
378 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
379 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "suffix:m"), "set_target_position", "get_target_position");
380 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
381 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
382 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_back_faces"), "set_hit_back_faces", "is_hit_back_faces_enabled");
383
384 ADD_GROUP("Collide With", "collide_with");
385 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
386 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
387
388 ADD_GROUP("Debug Shape", "debug_shape");
389 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_shape_custom_color"), "set_debug_shape_custom_color", "get_debug_shape_custom_color");
390 ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_shape_thickness", PROPERTY_HINT_RANGE, "1,5"), "set_debug_shape_thickness", "get_debug_shape_thickness");
391 }
392
393 int RayCast3D::get_debug_shape_thickness() const {
394 return debug_shape_thickness;
395 }
396
397 void RayCast3D::_update_debug_shape_vertices() {
398 debug_shape_vertices.clear();
399 debug_line_vertices.clear();
400
401 if (target_position == Vector3()) {
402 return;
403 }
404
405 debug_line_vertices.push_back(Vector3());
406 debug_line_vertices.push_back(target_position);
407
408 if (debug_shape_thickness > 1) {
409 float scale_factor = 100.0;
410 Vector3 dir = Vector3(target_position).normalized();
411 // Draw truncated pyramid
412 Vector3 normal = (std::abs(dir.x) + std::abs(dir.y) > CMP_EPSILON) ? Vector3(-dir.y, dir.x, 0).normalized() : Vector3(0, -dir.z, dir.y).normalized();
413 normal *= debug_shape_thickness / scale_factor;
414 int vertices_strip_order[14] = { 4, 5, 0, 1, 2, 5, 6, 4, 7, 0, 3, 2, 7, 6 };
415 for (int v = 0; v < 14; v++) {
416 Vector3 vertex = vertices_strip_order[v] < 4 ? normal : normal / 3.0 + target_position;
417 debug_shape_vertices.push_back(vertex.rotated(dir, Math::PI * (0.5 * (vertices_strip_order[v] % 4) + 0.25)));
418 }
419 }
420 }
421
422 void RayCast3D::set_debug_shape_thickness(const int p_debug_shape_thickness) {
423 debug_shape_thickness = p_debug_shape_thickness;
424 update_gizmos();
425
426 if (Engine::get_singleton()->is_editor_hint()) {
427 if (is_inside_tree()) {
428 _update_debug_shape_vertices();
429 }
430 } else if (debug_instance.is_valid()) {
431 _update_debug_shape();
432 }
433 }
434
435 const Vector<Vector3> &RayCast3D::get_debug_shape_vertices() const {
436 return debug_shape_vertices;
437 }
438
439 const Vector<Vector3> &RayCast3D::get_debug_line_vertices() const {
440 return debug_line_vertices;
441 }
442
443 void RayCast3D::set_debug_shape_custom_color(const Color &p_color) {
444 debug_shape_custom_color = p_color;
445 if (debug_material.is_valid()) {
446 _update_debug_shape_material();
447 }
448 }
449
450 Ref<StandardMaterial3D> RayCast3D::get_debug_material() {
451 _update_debug_shape_material();
452 return debug_material;
453 }
454
455 const Color &RayCast3D::get_debug_shape_custom_color() const {
456 return debug_shape_custom_color;
457 }
458
459 void RayCast3D::_create_debug_shape() {
460 _update_debug_shape_material();
461
462 if (!debug_instance.is_valid()) {
463 debug_instance = RenderingServer::get_singleton()->instance_create();
464 }
465
466 if (debug_mesh.is_null()) {
467 debug_mesh.instantiate();
468 }
469 }
470
471 void RayCast3D::_update_debug_shape_material(bool p_check_collision) {
472 if (debug_material.is_null()) {
473 Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
474 debug_material = material;
475
476 material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
477 material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
478 // Use double-sided rendering so that the RayCast can be seen if the camera is inside.
479 material->set_cull_mode(BaseMaterial3D::CULL_DISABLED);
480 material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA);
481 }
482
483 Color color = debug_shape_custom_color;
484 if (color == Color(0.0, 0.0, 0.0)) {
485 // Use the default debug shape color defined in the Project Settings.
486 color = get_tree()->get_debug_collisions_color();
487 }
488
489 if (p_check_collision && collided) {
490 if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) {
491 // If base color is already quite reddish, highlight collision with green color
492 color = Color(0.0, 1.0, 0.0, color.a);
493 } else {
494 // Else, highlight collision with red color
495 color = Color(1.0, 0, 0, color.a);
496 }
497 }
498
499 Ref<StandardMaterial3D> material = static_cast<Ref<StandardMaterial3D>>(debug_material);
500 material->set_albedo(color);
501 }
502
503 void RayCast3D::_update_debug_shape() {
504 if (!enabled) {
505 return;
506 }
507
508 if (!debug_instance.is_valid()) {
509 _create_debug_shape();
510 }
511
512 if (!debug_instance.is_valid() || debug_mesh.is_null()) {
513 return;
514 }
515
516 _update_debug_shape_vertices();
517
518 debug_mesh->clear_surfaces();
519
520 Array a;
521 a.resize(Mesh::ARRAY_MAX);
522
523 uint32_t flags = 0;
524 int surface_count = 0;
525
526 if (!debug_line_vertices.is_empty()) {
527 a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
528 debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags);
529 debug_mesh->surface_set_material(surface_count, debug_material);
530 ++surface_count;
531 }
532
533 if (!debug_shape_vertices.is_empty()) {
534 a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
535 debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), Dictionary(), flags);
536 debug_mesh->surface_set_material(surface_count, debug_material);
537 ++surface_count;
538 }
539
540 RenderingServer::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
541 if (is_inside_tree()) {
542 RenderingServer::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
543 RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
544 RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
545 }
546 }
547
548 void RayCast3D::_clear_debug_shape() {
549 ERR_FAIL_NULL(RenderingServer::get_singleton());
550 if (debug_instance.is_valid()) {
551 RenderingServer::get_singleton()->free_rid(debug_instance);
552 debug_instance = RID();
553 }
554 if (debug_mesh.is_valid()) {
555 RenderingServer::get_singleton()->free_rid(debug_mesh->get_rid());
556 debug_mesh = Ref<ArrayMesh>();
557 }
558 }
559
560 RayCast3D::RayCast3D() {
561 }