ray_cast_3d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #include "ray_cast_3d.h"
  31. #include "collision_object_3d.h"
  32. #include "mesh_instance_3d.h"
  33. void RayCast3D::set_target_position(const Vector3 &p_point) {
  34. target_position = p_point;
  35. update_gizmos();
  36. if (Engine::get_singleton()->is_editor_hint()) {
  37. if (is_inside_tree()) {
  38. _update_debug_shape_vertices();
  39. }
  40. } else if (debug_shape) {
  41. _update_debug_shape();
  42. }
  43. }
  44. Vector3 RayCast3D::get_target_position() const {
  45. return target_position;
  46. }
  47. void RayCast3D::set_collision_mask(uint32_t p_mask) {
  48. collision_mask = p_mask;
  49. }
  50. uint32_t RayCast3D::get_collision_mask() const {
  51. return collision_mask;
  52. }
  53. void RayCast3D::set_collision_mask_value(int p_layer_number, bool p_value) {
  54. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  55. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  56. uint32_t mask = get_collision_mask();
  57. if (p_value) {
  58. mask |= 1 << (p_layer_number - 1);
  59. } else {
  60. mask &= ~(1 << (p_layer_number - 1));
  61. }
  62. set_collision_mask(mask);
  63. }
  64. bool RayCast3D::get_collision_mask_value(int p_layer_number) const {
  65. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  66. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  67. return get_collision_mask() & (1 << (p_layer_number - 1));
  68. }
  69. bool RayCast3D::is_colliding() const {
  70. return collided;
  71. }
  72. Object *RayCast3D::get_collider() const {
  73. if (against.is_null()) {
  74. return nullptr;
  75. }
  76. return ObjectDB::get_instance(against);
  77. }
  78. RID RayCast3D::get_collider_rid() const {
  79. return against_rid;
  80. }
  81. int RayCast3D::get_collider_shape() const {
  82. return against_shape;
  83. }
  84. Vector3 RayCast3D::get_collision_point() const {
  85. return collision_point;
  86. }
  87. Vector3 RayCast3D::get_collision_normal() const {
  88. return collision_normal;
  89. }
  90. void RayCast3D::set_enabled(bool p_enabled) {
  91. enabled = p_enabled;
  92. update_gizmos();
  93. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  94. set_physics_process_internal(p_enabled);
  95. }
  96. if (!p_enabled) {
  97. collided = false;
  98. }
  99. if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) {
  100. if (p_enabled) {
  101. _update_debug_shape();
  102. } else {
  103. _clear_debug_shape();
  104. }
  105. }
  106. }
  107. bool RayCast3D::is_enabled() const {
  108. return enabled;
  109. }
  110. void RayCast3D::set_exclude_parent_body(bool p_exclude_parent_body) {
  111. if (exclude_parent_body == p_exclude_parent_body) {
  112. return;
  113. }
  114. exclude_parent_body = p_exclude_parent_body;
  115. if (!is_inside_tree()) {
  116. return;
  117. }
  118. if (Object::cast_to<CollisionObject3D>(get_parent())) {
  119. if (exclude_parent_body) {
  120. exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  121. } else {
  122. exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  123. }
  124. }
  125. }
  126. bool RayCast3D::get_exclude_parent_body() const {
  127. return exclude_parent_body;
  128. }
  129. void RayCast3D::_notification(int p_what) {
  130. switch (p_what) {
  131. case NOTIFICATION_ENTER_TREE: {
  132. if (Engine::get_singleton()->is_editor_hint()) {
  133. _update_debug_shape_vertices();
  134. }
  135. if (enabled && !Engine::get_singleton()->is_editor_hint()) {
  136. set_physics_process_internal(true);
  137. } else {
  138. set_physics_process_internal(false);
  139. }
  140. if (get_tree()->is_debugging_collisions_hint()) {
  141. _update_debug_shape();
  142. }
  143. if (Object::cast_to<CollisionObject3D>(get_parent())) {
  144. if (exclude_parent_body) {
  145. exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  146. } else {
  147. exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  148. }
  149. }
  150. } break;
  151. case NOTIFICATION_EXIT_TREE: {
  152. if (enabled) {
  153. set_physics_process_internal(false);
  154. }
  155. if (debug_shape) {
  156. _clear_debug_shape();
  157. }
  158. } break;
  159. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  160. if (!enabled) {
  161. break;
  162. }
  163. bool prev_collision_state = collided;
  164. _update_raycast_state();
  165. if (prev_collision_state != collided && get_tree()->is_debugging_collisions_hint()) {
  166. _update_debug_shape_material(true);
  167. }
  168. } break;
  169. }
  170. }
  171. void RayCast3D::_update_raycast_state() {
  172. Ref<World3D> w3d = get_world_3d();
  173. ERR_FAIL_COND(w3d.is_null());
  174. PhysicsDirectSpaceState3D *dss = PhysicsServer3D::get_singleton()->space_get_direct_state(w3d->get_space());
  175. ERR_FAIL_COND(!dss);
  176. Transform3D gt = get_global_transform();
  177. Vector3 to = target_position;
  178. if (to == Vector3()) {
  179. to = Vector3(0, 0.01, 0);
  180. }
  181. PhysicsDirectSpaceState3D::RayParameters ray_params;
  182. ray_params.from = gt.get_origin();
  183. ray_params.to = gt.xform(to);
  184. ray_params.exclude = exclude;
  185. ray_params.collision_mask = collision_mask;
  186. ray_params.collide_with_bodies = collide_with_bodies;
  187. ray_params.collide_with_areas = collide_with_areas;
  188. ray_params.hit_from_inside = hit_from_inside;
  189. PhysicsDirectSpaceState3D::RayResult rr;
  190. if (dss->intersect_ray(ray_params, rr)) {
  191. collided = true;
  192. against = rr.collider_id;
  193. against_rid = rr.rid;
  194. collision_point = rr.position;
  195. collision_normal = rr.normal;
  196. against_shape = rr.shape;
  197. } else {
  198. collided = false;
  199. against = ObjectID();
  200. against_rid = RID();
  201. against_shape = 0;
  202. }
  203. }
  204. void RayCast3D::force_raycast_update() {
  205. _update_raycast_state();
  206. }
  207. void RayCast3D::add_exception_rid(const RID &p_rid) {
  208. exclude.insert(p_rid);
  209. }
  210. void RayCast3D::add_exception(const CollisionObject3D *p_node) {
  211. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject3D.");
  212. add_exception_rid(p_node->get_rid());
  213. }
  214. void RayCast3D::remove_exception_rid(const RID &p_rid) {
  215. exclude.erase(p_rid);
  216. }
  217. void RayCast3D::remove_exception(const CollisionObject3D *p_node) {
  218. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject3D.");
  219. remove_exception_rid(p_node->get_rid());
  220. }
  221. void RayCast3D::clear_exceptions() {
  222. exclude.clear();
  223. if (exclude_parent_body && is_inside_tree()) {
  224. CollisionObject3D *parent = Object::cast_to<CollisionObject3D>(get_parent());
  225. if (parent) {
  226. exclude.insert(parent->get_rid());
  227. }
  228. }
  229. }
  230. void RayCast3D::set_collide_with_areas(bool p_enabled) {
  231. collide_with_areas = p_enabled;
  232. }
  233. bool RayCast3D::is_collide_with_areas_enabled() const {
  234. return collide_with_areas;
  235. }
  236. void RayCast3D::set_collide_with_bodies(bool p_enabled) {
  237. collide_with_bodies = p_enabled;
  238. }
  239. bool RayCast3D::is_collide_with_bodies_enabled() const {
  240. return collide_with_bodies;
  241. }
  242. void RayCast3D::set_hit_from_inside(bool p_enabled) {
  243. hit_from_inside = p_enabled;
  244. }
  245. bool RayCast3D::is_hit_from_inside_enabled() const {
  246. return hit_from_inside;
  247. }
  248. void RayCast3D::_bind_methods() {
  249. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast3D::set_enabled);
  250. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast3D::is_enabled);
  251. ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &RayCast3D::set_target_position);
  252. ClassDB::bind_method(D_METHOD("get_target_position"), &RayCast3D::get_target_position);
  253. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast3D::is_colliding);
  254. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast3D::force_raycast_update);
  255. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast3D::get_collider);
  256. ClassDB::bind_method(D_METHOD("get_collider_rid"), &RayCast3D::get_collider_rid);
  257. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast3D::get_collider_shape);
  258. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast3D::get_collision_point);
  259. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast3D::get_collision_normal);
  260. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast3D::add_exception_rid);
  261. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast3D::add_exception);
  262. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast3D::remove_exception_rid);
  263. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast3D::remove_exception);
  264. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast3D::clear_exceptions);
  265. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast3D::set_collision_mask);
  266. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast3D::get_collision_mask);
  267. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &RayCast3D::set_collision_mask_value);
  268. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &RayCast3D::get_collision_mask_value);
  269. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast3D::set_exclude_parent_body);
  270. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast3D::get_exclude_parent_body);
  271. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast3D::set_collide_with_areas);
  272. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast3D::is_collide_with_areas_enabled);
  273. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast3D::set_collide_with_bodies);
  274. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast3D::is_collide_with_bodies_enabled);
  275. ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &RayCast3D::set_hit_from_inside);
  276. ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &RayCast3D::is_hit_from_inside_enabled);
  277. ClassDB::bind_method(D_METHOD("set_debug_shape_custom_color", "debug_shape_custom_color"), &RayCast3D::set_debug_shape_custom_color);
  278. ClassDB::bind_method(D_METHOD("get_debug_shape_custom_color"), &RayCast3D::get_debug_shape_custom_color);
  279. ClassDB::bind_method(D_METHOD("set_debug_shape_thickness", "debug_shape_thickness"), &RayCast3D::set_debug_shape_thickness);
  280. ClassDB::bind_method(D_METHOD("get_debug_shape_thickness"), &RayCast3D::get_debug_shape_thickness);
  281. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  282. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  283. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "suffix:m"), "set_target_position", "get_target_position");
  284. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  285. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
  286. ADD_GROUP("Collide With", "collide_with");
  287. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  288. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  289. ADD_GROUP("Debug Shape", "debug_shape");
  290. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_shape_custom_color"), "set_debug_shape_custom_color", "get_debug_shape_custom_color");
  291. ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_shape_thickness", PROPERTY_HINT_RANGE, "1,5"), "set_debug_shape_thickness", "get_debug_shape_thickness");
  292. }
  293. int RayCast3D::get_debug_shape_thickness() const {
  294. return debug_shape_thickness;
  295. }
  296. void RayCast3D::_update_debug_shape_vertices() {
  297. debug_shape_vertices.clear();
  298. debug_line_vertices.clear();
  299. if (target_position == Vector3()) {
  300. return;
  301. }
  302. debug_line_vertices.push_back(Vector3());
  303. debug_line_vertices.push_back(target_position);
  304. if (debug_shape_thickness > 1) {
  305. float scale_factor = 100.0;
  306. Vector3 dir = Vector3(target_position).normalized();
  307. // Draw truncated pyramid
  308. Vector3 normal = (fabs(dir.x) + fabs(dir.y) > CMP_EPSILON) ? Vector3(-dir.y, dir.x, 0).normalized() : Vector3(0, -dir.z, dir.y).normalized();
  309. normal *= debug_shape_thickness / scale_factor;
  310. int vertices_strip_order[14] = { 4, 5, 0, 1, 2, 5, 6, 4, 7, 0, 3, 2, 7, 6 };
  311. for (int v = 0; v < 14; v++) {
  312. Vector3 vertex = vertices_strip_order[v] < 4 ? normal : normal / 3.0 + target_position;
  313. debug_shape_vertices.push_back(vertex.rotated(dir, Math_PI * (0.5 * (vertices_strip_order[v] % 4) + 0.25)));
  314. }
  315. }
  316. }
  317. void RayCast3D::set_debug_shape_thickness(const int p_debug_shape_thickness) {
  318. debug_shape_thickness = p_debug_shape_thickness;
  319. update_gizmos();
  320. if (Engine::get_singleton()->is_editor_hint()) {
  321. if (is_inside_tree()) {
  322. _update_debug_shape_vertices();
  323. }
  324. } else if (debug_shape) {
  325. _update_debug_shape();
  326. }
  327. }
  328. const Vector<Vector3> &RayCast3D::get_debug_shape_vertices() const {
  329. return debug_shape_vertices;
  330. }
  331. const Vector<Vector3> &RayCast3D::get_debug_line_vertices() const {
  332. return debug_line_vertices;
  333. }
  334. void RayCast3D::set_debug_shape_custom_color(const Color &p_color) {
  335. debug_shape_custom_color = p_color;
  336. if (debug_material.is_valid()) {
  337. _update_debug_shape_material();
  338. }
  339. }
  340. Ref<StandardMaterial3D> RayCast3D::get_debug_material() {
  341. _update_debug_shape_material();
  342. return debug_material;
  343. }
  344. const Color &RayCast3D::get_debug_shape_custom_color() const {
  345. return debug_shape_custom_color;
  346. }
  347. void RayCast3D::_create_debug_shape() {
  348. _update_debug_shape_material();
  349. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  350. MeshInstance3D *mi = memnew(MeshInstance3D);
  351. mi->set_mesh(mesh);
  352. add_child(mi);
  353. debug_shape = mi;
  354. }
  355. void RayCast3D::_update_debug_shape_material(bool p_check_collision) {
  356. if (!debug_material.is_valid()) {
  357. Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
  358. debug_material = material;
  359. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  360. // Use double-sided rendering so that the RayCast can be seen if the camera is inside.
  361. material->set_cull_mode(BaseMaterial3D::CULL_DISABLED);
  362. material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA);
  363. }
  364. Color color = debug_shape_custom_color;
  365. if (color == Color(0.0, 0.0, 0.0)) {
  366. // Use the default debug shape color defined in the Project Settings.
  367. color = get_tree()->get_debug_collisions_color();
  368. }
  369. if (p_check_collision && collided) {
  370. if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) {
  371. // If base color is already quite reddish, highlight collision with green color
  372. color = Color(0.0, 1.0, 0.0, color.a);
  373. } else {
  374. // Else, highlight collision with red color
  375. color = Color(1.0, 0, 0, color.a);
  376. }
  377. }
  378. Ref<StandardMaterial3D> material = static_cast<Ref<StandardMaterial3D>>(debug_material);
  379. material->set_albedo(color);
  380. }
  381. void RayCast3D::_update_debug_shape() {
  382. if (!enabled) {
  383. return;
  384. }
  385. if (!debug_shape) {
  386. _create_debug_shape();
  387. }
  388. MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape);
  389. Ref<ArrayMesh> mesh = mi->get_mesh();
  390. if (!mesh.is_valid()) {
  391. return;
  392. }
  393. _update_debug_shape_vertices();
  394. mesh->clear_surfaces();
  395. Array a;
  396. a.resize(Mesh::ARRAY_MAX);
  397. uint32_t flags = 0;
  398. int surface_count = 0;
  399. if (!debug_line_vertices.is_empty()) {
  400. a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
  401. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags);
  402. mesh->surface_set_material(surface_count, debug_material);
  403. ++surface_count;
  404. }
  405. if (!debug_shape_vertices.is_empty()) {
  406. a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
  407. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), Dictionary(), flags);
  408. mesh->surface_set_material(surface_count, debug_material);
  409. ++surface_count;
  410. }
  411. }
  412. void RayCast3D::_clear_debug_shape() {
  413. if (!debug_shape) {
  414. return;
  415. }
  416. MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape);
  417. if (mi->is_inside_tree()) {
  418. mi->queue_free();
  419. } else {
  420. memdelete(mi);
  421. }
  422. debug_shape = nullptr;
  423. }
  424. RayCast3D::RayCast3D() {
  425. }