ray_cast.cpp 16 KB

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