| 1 | /**************************************************************************/ |
| 2 | /* shape_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 "shape_cast_3d.h" |
| 32 | |
| 33 | #include "scene/3d/physics/collision_object_3d.h" |
| 34 | #include "scene/resources/3d/concave_polygon_shape_3d.h" |
| 35 | #include "scene/resources/mesh.h" |
| 36 | |
| 37 | void ShapeCast3D::_notification(int p_what) { |
| 38 | switch (p_what) { |
| 39 | case NOTIFICATION_ENTER_TREE: { |
| 40 | if (Engine::get_singleton()->is_editor_hint()) { |
| 41 | _update_debug_shape_vertices(); |
| 42 | } |
| 43 | if (enabled && !Engine::get_singleton()->is_editor_hint()) { |
| 44 | set_physics_process_internal(true); |
| 45 | } else { |
| 46 | set_physics_process_internal(false); |
| 47 | } |
| 48 | |
| 49 | if (get_tree()->is_debugging_collisions_hint()) { |
| 50 | _update_debug_shape(); |
| 51 | } |
| 52 | |
| 53 | if (Object::cast_to<CollisionObject3D>(get_parent())) { |
| 54 | if (exclude_parent_body) { |
| 55 | exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid()); |
| 56 | } else { |
| 57 | exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid()); |
| 58 | } |
| 59 | } |
| 60 | } break; |
| 61 | |
| 62 | case NOTIFICATION_EXIT_TREE: { |
| 63 | if (enabled) { |
| 64 | set_physics_process_internal(false); |
| 65 | } |
| 66 | |
| 67 | if (debug_instance.is_valid()) { |
| 68 | _clear_debug_shape(); |
| 69 | } |
| 70 | } break; |
| 71 | |
| 72 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 73 | if (is_inside_tree() && debug_instance.is_valid()) { |
| 74 | RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree()); |
| 75 | } |
| 76 | } break; |
| 77 | |
| 78 | case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { |
| 79 | if (!enabled) { |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | bool prev_collision_state = collided; |
| 84 | _update_shapecast_state(); |
| 85 | if (get_tree()->is_debugging_collisions_hint()) { |
| 86 | if (prev_collision_state != collided) { |
| 87 | _update_debug_shape_material(true); |
| 88 | } |
| 89 | if (collided) { |
| 90 | _update_debug_shape(); |
| 91 | } |
| 92 | if (prev_collision_state == collided && !collided) { |
| 93 | _update_debug_shape(); |
| 94 | } |
| 95 | if (is_inside_tree() && debug_instance.is_valid()) { |
| 96 | RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform()); |
| 97 | } |
| 98 | } |
| 99 | } break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void ShapeCast3D::_bind_methods() { |
| 104 | #ifndef DISABLE_DEPRECATED |
| 105 | ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &ShapeCast3D::resource_changed); |
| 106 | #endif |
| 107 | |
| 108 | ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &ShapeCast3D::set_enabled); |
| 109 | ClassDB::bind_method(D_METHOD("is_enabled"), &ShapeCast3D::is_enabled); |
| 110 | |
| 111 | ClassDB::bind_method(D_METHOD("set_shape", "shape"), &ShapeCast3D::set_shape); |
| 112 | ClassDB::bind_method(D_METHOD("get_shape"), &ShapeCast3D::get_shape); |
| 113 | |
| 114 | ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &ShapeCast3D::set_target_position); |
| 115 | ClassDB::bind_method(D_METHOD("get_target_position"), &ShapeCast3D::get_target_position); |
| 116 | |
| 117 | ClassDB::bind_method(D_METHOD("set_margin", "margin"), &ShapeCast3D::set_margin); |
| 118 | ClassDB::bind_method(D_METHOD("get_margin"), &ShapeCast3D::get_margin); |
| 119 | |
| 120 | ClassDB::bind_method(D_METHOD("set_max_results", "max_results"), &ShapeCast3D::set_max_results); |
| 121 | ClassDB::bind_method(D_METHOD("get_max_results"), &ShapeCast3D::get_max_results); |
| 122 | |
| 123 | ClassDB::bind_method(D_METHOD("is_colliding"), &ShapeCast3D::is_colliding); |
| 124 | ClassDB::bind_method(D_METHOD("get_collision_count"), &ShapeCast3D::get_collision_count); |
| 125 | |
| 126 | ClassDB::bind_method(D_METHOD("force_shapecast_update"), &ShapeCast3D::force_shapecast_update); |
| 127 | |
| 128 | ClassDB::bind_method(D_METHOD("get_collider", "index"), &ShapeCast3D::get_collider); |
| 129 | ClassDB::bind_method(D_METHOD("get_collider_rid", "index"), &ShapeCast3D::get_collider_rid); |
| 130 | ClassDB::bind_method(D_METHOD("get_collider_shape", "index"), &ShapeCast3D::get_collider_shape); |
| 131 | ClassDB::bind_method(D_METHOD("get_collision_point", "index"), &ShapeCast3D::get_collision_point); |
| 132 | ClassDB::bind_method(D_METHOD("get_collision_normal", "index"), &ShapeCast3D::get_collision_normal); |
| 133 | |
| 134 | ClassDB::bind_method(D_METHOD("get_closest_collision_safe_fraction"), &ShapeCast3D::get_closest_collision_safe_fraction); |
| 135 | ClassDB::bind_method(D_METHOD("get_closest_collision_unsafe_fraction"), &ShapeCast3D::get_closest_collision_unsafe_fraction); |
| 136 | |
| 137 | ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &ShapeCast3D::add_exception_rid); |
| 138 | ClassDB::bind_method(D_METHOD("add_exception", "node"), &ShapeCast3D::add_exception); |
| 139 | |
| 140 | ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &ShapeCast3D::remove_exception_rid); |
| 141 | ClassDB::bind_method(D_METHOD("remove_exception", "node"), &ShapeCast3D::remove_exception); |
| 142 | |
| 143 | ClassDB::bind_method(D_METHOD("clear_exceptions"), &ShapeCast3D::clear_exceptions); |
| 144 | |
| 145 | ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &ShapeCast3D::set_collision_mask); |
| 146 | ClassDB::bind_method(D_METHOD("get_collision_mask"), &ShapeCast3D::get_collision_mask); |
| 147 | |
| 148 | ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &ShapeCast3D::set_collision_mask_value); |
| 149 | ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &ShapeCast3D::get_collision_mask_value); |
| 150 | |
| 151 | ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &ShapeCast3D::set_exclude_parent_body); |
| 152 | ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &ShapeCast3D::get_exclude_parent_body); |
| 153 | |
| 154 | ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &ShapeCast3D::set_collide_with_areas); |
| 155 | ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &ShapeCast3D::is_collide_with_areas_enabled); |
| 156 | |
| 157 | ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &ShapeCast3D::set_collide_with_bodies); |
| 158 | ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &ShapeCast3D::is_collide_with_bodies_enabled); |
| 159 | |
| 160 | ClassDB::bind_method(D_METHOD("get_collision_result"), &ShapeCast3D::get_collision_result); |
| 161 | |
| 162 | ClassDB::bind_method(D_METHOD("set_debug_shape_custom_color", "debug_shape_custom_color"), &ShapeCast3D::set_debug_shape_custom_color); |
| 163 | ClassDB::bind_method(D_METHOD("get_debug_shape_custom_color"), &ShapeCast3D::get_debug_shape_custom_color); |
| 164 | |
| 165 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled"); |
| 166 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape"); |
| 167 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body"); |
| 168 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "suffix:m"), "set_target_position", "get_target_position"); |
| 169 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01,suffix:m"), "set_margin", "get_margin"); |
| 170 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_results"), "set_max_results", "get_max_results"); |
| 171 | ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask"); |
| 172 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "collision_result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "", "get_collision_result"); |
| 173 | |
| 174 | ADD_GROUP("Collide With", "collide_with"); |
| 175 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled"); |
| 176 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled"); |
| 177 | |
| 178 | ADD_GROUP("Debug Shape", "debug_shape"); |
| 179 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_shape_custom_color"), "set_debug_shape_custom_color", "get_debug_shape_custom_color"); |
| 180 | } |
| 181 | |
| 182 | PackedStringArray ShapeCast3D::get_configuration_warnings() const { |
| 183 | PackedStringArray warnings = Node3D::get_configuration_warnings(); |
| 184 | |
| 185 | if (shape.is_null()) { |
| 186 | warnings.push_back(RTR("This node cannot interact with other objects unless a Shape3D is assigned.")); |
| 187 | } |
| 188 | if (shape.is_valid() && Object::cast_to<ConcavePolygonShape3D>(*shape)) { |
| 189 | warnings.push_back(RTR("ShapeCast3D does not support ConcavePolygonShape3Ds. Collisions will not be reported.")); |
| 190 | } |
| 191 | return warnings; |
| 192 | } |
| 193 | |
| 194 | void ShapeCast3D::set_enabled(bool p_enabled) { |
| 195 | enabled = p_enabled; |
| 196 | update_gizmos(); |
| 197 | |
| 198 | if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) { |
| 199 | set_physics_process_internal(p_enabled); |
| 200 | } |
| 201 | if (!p_enabled) { |
| 202 | collided = false; |
| 203 | } |
| 204 | |
| 205 | if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) { |
| 206 | if (p_enabled) { |
| 207 | _update_debug_shape(); |
| 208 | } else { |
| 209 | _clear_debug_shape(); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | bool ShapeCast3D::is_enabled() const { |
| 215 | return enabled; |
| 216 | } |
| 217 | |
| 218 | void ShapeCast3D::set_target_position(const Vector3 &p_point) { |
| 219 | target_position = p_point; |
| 220 | if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) { |
| 221 | _update_debug_shape(); |
| 222 | } |
| 223 | update_gizmos(); |
| 224 | |
| 225 | if (Engine::get_singleton()->is_editor_hint()) { |
| 226 | if (is_inside_tree()) { |
| 227 | _update_debug_shape_vertices(); |
| 228 | } |
| 229 | } else if (debug_instance.is_valid()) { |
| 230 | _update_debug_shape(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | Vector3 ShapeCast3D::get_target_position() const { |
| 235 | return target_position; |
| 236 | } |
| 237 | |
| 238 | void ShapeCast3D::set_margin(real_t p_margin) { |
| 239 | margin = p_margin; |
| 240 | } |
| 241 | |
| 242 | real_t ShapeCast3D::get_margin() const { |
| 243 | return margin; |
| 244 | } |
| 245 | |
| 246 | void ShapeCast3D::set_max_results(int p_max_results) { |
| 247 | max_results = p_max_results; |
| 248 | } |
| 249 | |
| 250 | int ShapeCast3D::get_max_results() const { |
| 251 | return max_results; |
| 252 | } |
| 253 | |
| 254 | void ShapeCast3D::set_collision_mask(uint32_t p_mask) { |
| 255 | collision_mask = p_mask; |
| 256 | } |
| 257 | |
| 258 | uint32_t ShapeCast3D::get_collision_mask() const { |
| 259 | return collision_mask; |
| 260 | } |
| 261 | |
| 262 | void ShapeCast3D::set_collision_mask_value(int p_layer_number, bool p_value) { |
| 263 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive."); |
| 264 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive."); |
| 265 | uint32_t mask = get_collision_mask(); |
| 266 | if (p_value) { |
| 267 | mask |= 1 << (p_layer_number - 1); |
| 268 | } else { |
| 269 | mask &= ~(1 << (p_layer_number - 1)); |
| 270 | } |
| 271 | set_collision_mask(mask); |
| 272 | } |
| 273 | |
| 274 | bool ShapeCast3D::get_collision_mask_value(int p_layer_number) const { |
| 275 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 276 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 277 | return get_collision_mask() & (1 << (p_layer_number - 1)); |
| 278 | } |
| 279 | |
| 280 | int ShapeCast3D::get_collision_count() const { |
| 281 | return result.size(); |
| 282 | } |
| 283 | |
| 284 | bool ShapeCast3D::is_colliding() const { |
| 285 | return collided; |
| 286 | } |
| 287 | |
| 288 | Object *ShapeCast3D::get_collider(int p_idx) const { |
| 289 | ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), nullptr, "No collider found."); |
| 290 | |
| 291 | if (result[p_idx].collider_id.is_null()) { |
| 292 | return nullptr; |
| 293 | } |
| 294 | return ObjectDB::get_instance(result[p_idx].collider_id); |
| 295 | } |
| 296 | |
| 297 | RID ShapeCast3D::get_collider_rid(int p_idx) const { |
| 298 | ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), RID(), "No collider RID found."); |
| 299 | return result[p_idx].rid; |
| 300 | } |
| 301 | |
| 302 | int ShapeCast3D::get_collider_shape(int p_idx) const { |
| 303 | ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), -1, "No collider shape found."); |
| 304 | return result[p_idx].shape; |
| 305 | } |
| 306 | |
| 307 | Vector3 ShapeCast3D::get_collision_point(int p_idx) const { |
| 308 | ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), Vector3(), "No collision point found."); |
| 309 | return result[p_idx].point; |
| 310 | } |
| 311 | |
| 312 | Vector3 ShapeCast3D::get_collision_normal(int p_idx) const { |
| 313 | ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), Vector3(), "No collision normal found."); |
| 314 | return result[p_idx].normal; |
| 315 | } |
| 316 | |
| 317 | real_t ShapeCast3D::get_closest_collision_safe_fraction() const { |
| 318 | return collision_safe_fraction; |
| 319 | } |
| 320 | |
| 321 | real_t ShapeCast3D::get_closest_collision_unsafe_fraction() const { |
| 322 | return collision_unsafe_fraction; |
| 323 | } |
| 324 | |
| 325 | #ifndef DISABLE_DEPRECATED |
| 326 | void ShapeCast3D::resource_changed(Ref<Resource> p_res) { |
| 327 | } |
| 328 | #endif |
| 329 | |
| 330 | void ShapeCast3D::_shape_changed() { |
| 331 | update_gizmos(); |
| 332 | bool is_editor = Engine::get_singleton()->is_editor_hint(); |
| 333 | if (is_inside_tree() && (is_editor || get_tree()->is_debugging_collisions_hint())) { |
| 334 | _update_debug_shape(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void ShapeCast3D::set_shape(const Ref<Shape3D> &p_shape) { |
| 339 | if (p_shape == shape) { |
| 340 | return; |
| 341 | } |
| 342 | if (shape.is_valid()) { |
| 343 | shape->disconnect_changed(callable_mp(this, &ShapeCast3D::_shape_changed)); |
| 344 | } |
| 345 | shape = p_shape; |
| 346 | if (shape.is_valid()) { |
| 347 | shape->connect_changed(callable_mp(this, &ShapeCast3D::_shape_changed)); |
| 348 | shape_rid = shape->get_rid(); |
| 349 | } |
| 350 | |
| 351 | bool is_editor = Engine::get_singleton()->is_editor_hint(); |
| 352 | if (is_inside_tree() && (is_editor || get_tree()->is_debugging_collisions_hint())) { |
| 353 | _update_debug_shape(); |
| 354 | } |
| 355 | update_gizmos(); |
| 356 | update_configuration_warnings(); |
| 357 | } |
| 358 | |
| 359 | Ref<Shape3D> ShapeCast3D::get_shape() const { |
| 360 | return shape; |
| 361 | } |
| 362 | |
| 363 | void ShapeCast3D::set_exclude_parent_body(bool p_exclude_parent_body) { |
| 364 | if (exclude_parent_body == p_exclude_parent_body) { |
| 365 | return; |
| 366 | } |
| 367 | exclude_parent_body = p_exclude_parent_body; |
| 368 | |
| 369 | if (!is_inside_tree()) { |
| 370 | return; |
| 371 | } |
| 372 | if (Object::cast_to<CollisionObject3D>(get_parent())) { |
| 373 | if (exclude_parent_body) { |
| 374 | exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid()); |
| 375 | } else { |
| 376 | exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid()); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | bool ShapeCast3D::get_exclude_parent_body() const { |
| 382 | return exclude_parent_body; |
| 383 | } |
| 384 | |
| 385 | void ShapeCast3D::_update_shapecast_state() { |
| 386 | result.clear(); |
| 387 | |
| 388 | ERR_FAIL_COND_MSG(shape.is_null(), "Null reference to shape. ShapeCast3D requires a Shape3D to sweep for collisions."); |
| 389 | |
| 390 | Ref<World3D> w3d = get_world_3d(); |
| 391 | ERR_FAIL_COND(w3d.is_null()); |
| 392 | |
| 393 | PhysicsDirectSpaceState3D *dss = PhysicsServer3D::get_singleton()->space_get_direct_state(w3d->get_space()); |
| 394 | ERR_FAIL_NULL(dss); |
| 395 | |
| 396 | Transform3D gt = get_global_transform(); |
| 397 | |
| 398 | PhysicsDirectSpaceState3D::ShapeParameters params; |
| 399 | params.shape_rid = shape_rid; |
| 400 | params.transform = gt; |
| 401 | params.motion = gt.basis.xform(target_position); |
| 402 | params.margin = margin; |
| 403 | params.exclude = exclude; |
| 404 | params.collision_mask = collision_mask; |
| 405 | params.collide_with_bodies = collide_with_bodies; |
| 406 | params.collide_with_areas = collide_with_areas; |
| 407 | |
| 408 | collision_safe_fraction = 0.0; |
| 409 | collision_unsafe_fraction = 0.0; |
| 410 | |
| 411 | if (target_position != Vector3()) { |
| 412 | dss->cast_motion(params, collision_safe_fraction, collision_unsafe_fraction); |
| 413 | if (collision_unsafe_fraction < 1.0) { |
| 414 | // Move shape transform to the point of impact, |
| 415 | // so we can collect contact info at that point. |
| 416 | gt.set_origin(gt.get_origin() + params.motion * (collision_unsafe_fraction + CMP_EPSILON)); |
| 417 | params.transform = gt; |
| 418 | } |
| 419 | } |
| 420 | // Regardless of whether the shape is stuck or it's moved along |
| 421 | // the motion vector, we'll only consider static collisions from now on. |
| 422 | params.motion = Vector3(); |
| 423 | |
| 424 | bool intersected = true; |
| 425 | while (intersected && result.size() < max_results) { |
| 426 | PhysicsDirectSpaceState3D::ShapeRestInfo info; |
| 427 | intersected = dss->rest_info(params, &info); |
| 428 | if (intersected) { |
| 429 | result.push_back(info); |
| 430 | params.exclude.insert(info.rid); |
| 431 | } |
| 432 | } |
| 433 | collided = !result.is_empty(); |
| 434 | } |
| 435 | |
| 436 | void ShapeCast3D::force_shapecast_update() { |
| 437 | _update_shapecast_state(); |
| 438 | } |
| 439 | |
| 440 | void ShapeCast3D::add_exception_rid(const RID &p_rid) { |
| 441 | exclude.insert(p_rid); |
| 442 | } |
| 443 | |
| 444 | void ShapeCast3D::add_exception(RequiredParam<const CollisionObject3D> rp_node) { |
| 445 | EXTRACT_PARAM_OR_FAIL_MSG(p_node, rp_node, "The passed Node must be an instance of CollisionObject3D."); |
| 446 | add_exception_rid(p_node->get_rid()); |
| 447 | } |
| 448 | |
| 449 | void ShapeCast3D::remove_exception_rid(const RID &p_rid) { |
| 450 | exclude.erase(p_rid); |
| 451 | } |
| 452 | |
| 453 | void ShapeCast3D::remove_exception(RequiredParam<const CollisionObject3D> rp_node) { |
| 454 | EXTRACT_PARAM_OR_FAIL_MSG(p_node, rp_node, "The passed Node must be an instance of CollisionObject3D."); |
| 455 | remove_exception_rid(p_node->get_rid()); |
| 456 | } |
| 457 | |
| 458 | void ShapeCast3D::clear_exceptions() { |
| 459 | exclude.clear(); |
| 460 | } |
| 461 | |
| 462 | void ShapeCast3D::set_collide_with_areas(bool p_clip) { |
| 463 | collide_with_areas = p_clip; |
| 464 | } |
| 465 | |
| 466 | bool ShapeCast3D::is_collide_with_areas_enabled() const { |
| 467 | return collide_with_areas; |
| 468 | } |
| 469 | |
| 470 | void ShapeCast3D::set_collide_with_bodies(bool p_clip) { |
| 471 | collide_with_bodies = p_clip; |
| 472 | } |
| 473 | |
| 474 | bool ShapeCast3D::is_collide_with_bodies_enabled() const { |
| 475 | return collide_with_bodies; |
| 476 | } |
| 477 | |
| 478 | Array ShapeCast3D::get_collision_result() const { |
| 479 | Array ret; |
| 480 | |
| 481 | for (int i = 0; i < result.size(); ++i) { |
| 482 | const PhysicsDirectSpaceState3D::ShapeRestInfo &sri = result[i]; |
| 483 | |
| 484 | Dictionary col; |
| 485 | col["point"] = sri.point; |
| 486 | col["normal"] = sri.normal; |
| 487 | col["rid"] = sri.rid; |
| 488 | col["collider"] = ObjectDB::get_instance(sri.collider_id); |
| 489 | col["collider_id"] = sri.collider_id; |
| 490 | col["shape"] = sri.shape; |
| 491 | col["linear_velocity"] = sri.linear_velocity; |
| 492 | |
| 493 | ret.push_back(col); |
| 494 | } |
| 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | void ShapeCast3D::_update_debug_shape_vertices() { |
| 499 | debug_shape_vertices.clear(); |
| 500 | debug_line_vertices.clear(); |
| 501 | |
| 502 | if (shape.is_valid()) { |
| 503 | debug_shape_vertices.append_array(shape->get_debug_mesh_lines()); |
| 504 | for (int i = 0; i < debug_shape_vertices.size(); i++) { |
| 505 | debug_shape_vertices.set(i, debug_shape_vertices[i] + Vector3(target_position * get_closest_collision_safe_fraction())); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (target_position == Vector3()) { |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | debug_line_vertices.push_back(Vector3()); |
| 514 | debug_line_vertices.push_back(target_position); |
| 515 | } |
| 516 | |
| 517 | const Vector<Vector3> &ShapeCast3D::get_debug_shape_vertices() const { |
| 518 | return debug_shape_vertices; |
| 519 | } |
| 520 | |
| 521 | const Vector<Vector3> &ShapeCast3D::get_debug_line_vertices() const { |
| 522 | return debug_line_vertices; |
| 523 | } |
| 524 | |
| 525 | void ShapeCast3D::set_debug_shape_custom_color(const Color &p_color) { |
| 526 | debug_shape_custom_color = p_color; |
| 527 | if (debug_material.is_valid()) { |
| 528 | _update_debug_shape_material(); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | Ref<StandardMaterial3D> ShapeCast3D::get_debug_material() { |
| 533 | _update_debug_shape_material(); |
| 534 | return debug_material; |
| 535 | } |
| 536 | |
| 537 | const Color &ShapeCast3D::get_debug_shape_custom_color() const { |
| 538 | return debug_shape_custom_color; |
| 539 | } |
| 540 | |
| 541 | void ShapeCast3D::_create_debug_shape() { |
| 542 | _update_debug_shape_material(); |
| 543 | |
| 544 | if (!debug_instance.is_valid()) { |
| 545 | debug_instance = RenderingServer::get_singleton()->instance_create(); |
| 546 | } |
| 547 | |
| 548 | if (debug_mesh.is_null()) { |
| 549 | debug_mesh.instantiate(); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void ShapeCast3D::_update_debug_shape_material(bool p_check_collision) { |
| 554 | if (debug_material.is_null()) { |
| 555 | Ref<StandardMaterial3D> material = memnew(StandardMaterial3D); |
| 556 | debug_material = material; |
| 557 | |
| 558 | material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 559 | material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true); |
| 560 | // Use double-sided rendering so that the RayCast can be seen if the camera is inside. |
| 561 | material->set_cull_mode(BaseMaterial3D::CULL_DISABLED); |
| 562 | material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA); |
| 563 | } |
| 564 | |
| 565 | Color color = debug_shape_custom_color; |
| 566 | if (color == Color(0.0, 0.0, 0.0)) { |
| 567 | // Use the default debug shape color defined in the Project Settings. |
| 568 | color = get_tree()->get_debug_collisions_color(); |
| 569 | } |
| 570 | |
| 571 | if (p_check_collision && collided) { |
| 572 | if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) { |
| 573 | // If base color is already quite reddish, highlight collision with green color |
| 574 | color = Color(0.0, 1.0, 0.0, color.a); |
| 575 | } else { |
| 576 | // Else, highlight collision with red color |
| 577 | color = Color(1.0, 0, 0, color.a); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | Ref<StandardMaterial3D> material = static_cast<Ref<StandardMaterial3D>>(debug_material); |
| 582 | material->set_albedo(color); |
| 583 | } |
| 584 | |
| 585 | void ShapeCast3D::_update_debug_shape() { |
| 586 | if (!enabled) { |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | if (!debug_instance.is_valid()) { |
| 591 | _create_debug_shape(); |
| 592 | } |
| 593 | |
| 594 | _update_debug_shape_vertices(); |
| 595 | |
| 596 | if (Engine::get_singleton()->is_editor_hint()) { |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | if (!debug_instance.is_valid() || debug_mesh.is_null()) { |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | debug_mesh->clear_surfaces(); |
| 605 | |
| 606 | Array a; |
| 607 | a.resize(Mesh::ARRAY_MAX); |
| 608 | |
| 609 | uint32_t flags = 0; |
| 610 | int surface_count = 0; |
| 611 | |
| 612 | if (!debug_shape_vertices.is_empty()) { |
| 613 | a[Mesh::ARRAY_VERTEX] = debug_shape_vertices; |
| 614 | debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags); |
| 615 | debug_mesh->surface_set_material(surface_count, debug_material); |
| 616 | ++surface_count; |
| 617 | } |
| 618 | |
| 619 | if (!debug_line_vertices.is_empty()) { |
| 620 | a[Mesh::ARRAY_VERTEX] = debug_line_vertices; |
| 621 | debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags); |
| 622 | debug_mesh->surface_set_material(surface_count, debug_material); |
| 623 | ++surface_count; |
| 624 | } |
| 625 | |
| 626 | RenderingServer::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid()); |
| 627 | if (is_inside_tree()) { |
| 628 | RenderingServer::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario()); |
| 629 | RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree()); |
| 630 | RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform()); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | void ShapeCast3D::_clear_debug_shape() { |
| 635 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 636 | if (debug_instance.is_valid()) { |
| 637 | RenderingServer::get_singleton()->free_rid(debug_instance); |
| 638 | debug_instance = RID(); |
| 639 | } |
| 640 | if (debug_mesh.is_valid()) { |
| 641 | RenderingServer::get_singleton()->free_rid(debug_mesh->get_rid()); |
| 642 | debug_mesh = Ref<ArrayMesh>(); |
| 643 | } |
| 644 | } |