ray_cast.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. if (!co) {
  201. return;
  202. }
  203. add_exception_rid(co->get_rid());
  204. }
  205. void RayCast::remove_exception_rid(const RID &p_rid) {
  206. exclude.erase(p_rid);
  207. }
  208. void RayCast::remove_exception(const Object *p_object) {
  209. ERR_FAIL_NULL(p_object);
  210. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  211. if (!co) {
  212. return;
  213. }
  214. remove_exception_rid(co->get_rid());
  215. }
  216. void RayCast::clear_exceptions() {
  217. exclude.clear();
  218. if (exclude_parent_body && is_inside_tree()) {
  219. CollisionObject *parent = Object::cast_to<CollisionObject>(get_parent());
  220. if (parent) {
  221. exclude.insert(parent->get_rid());
  222. }
  223. }
  224. }
  225. void RayCast::set_collide_with_areas(bool p_clip) {
  226. collide_with_areas = p_clip;
  227. }
  228. bool RayCast::is_collide_with_areas_enabled() const {
  229. return collide_with_areas;
  230. }
  231. void RayCast::set_collide_with_bodies(bool p_clip) {
  232. collide_with_bodies = p_clip;
  233. }
  234. bool RayCast::is_collide_with_bodies_enabled() const {
  235. return collide_with_bodies;
  236. }
  237. void RayCast::_bind_methods() {
  238. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast::set_enabled);
  239. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast::is_enabled);
  240. ClassDB::bind_method(D_METHOD("set_cast_to", "local_point"), &RayCast::set_cast_to);
  241. ClassDB::bind_method(D_METHOD("get_cast_to"), &RayCast::get_cast_to);
  242. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast::is_colliding);
  243. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast::force_raycast_update);
  244. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast::get_collider);
  245. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast::get_collider_shape);
  246. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast::get_collision_point);
  247. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast::get_collision_normal);
  248. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast::add_exception_rid);
  249. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast::add_exception);
  250. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast::remove_exception_rid);
  251. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast::remove_exception);
  252. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast::clear_exceptions);
  253. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast::set_collision_mask);
  254. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast::get_collision_mask);
  255. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &RayCast::set_collision_mask_bit);
  256. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &RayCast::get_collision_mask_bit);
  257. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast::set_exclude_parent_body);
  258. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast::get_exclude_parent_body);
  259. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast::set_collide_with_areas);
  260. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast::is_collide_with_areas_enabled);
  261. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast::set_collide_with_bodies);
  262. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast::is_collide_with_bodies_enabled);
  263. ClassDB::bind_method(D_METHOD("set_debug_shape_custom_color", "debug_shape_custom_color"), &RayCast::set_debug_shape_custom_color);
  264. ClassDB::bind_method(D_METHOD("get_debug_shape_custom_color"), &RayCast::get_debug_shape_custom_color);
  265. ClassDB::bind_method(D_METHOD("set_debug_shape_thickness", "debug_shape_thickness"), &RayCast::set_debug_shape_thickness);
  266. ClassDB::bind_method(D_METHOD("get_debug_shape_thickness"), &RayCast::get_debug_shape_thickness);
  267. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  268. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  269. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cast_to"), "set_cast_to", "get_cast_to");
  270. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  271. ADD_GROUP("Collide With", "collide_with");
  272. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  273. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  274. ADD_GROUP("Debug Shape", "debug_shape");
  275. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_shape_custom_color"), "set_debug_shape_custom_color", "get_debug_shape_custom_color");
  276. ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_shape_thickness", PROPERTY_HINT_RANGE, "1,5"), "set_debug_shape_thickness", "get_debug_shape_thickness");
  277. }
  278. int RayCast::get_debug_shape_thickness() const {
  279. return debug_shape_thickness;
  280. }
  281. void RayCast::_update_debug_shape_vertices() {
  282. debug_shape_vertices.clear();
  283. debug_line_vertices.clear();
  284. if (cast_to == Vector3()) {
  285. return;
  286. }
  287. debug_line_vertices.push_back(Vector3());
  288. debug_line_vertices.push_back(cast_to);
  289. if (debug_shape_thickness > 1) {
  290. float scale_factor = 100.0;
  291. Vector3 dir = Vector3(cast_to).normalized();
  292. // Draw truncated pyramid
  293. Vector3 normal = (fabs(dir.x) + fabs(dir.y) > CMP_EPSILON) ? Vector3(-dir.y, dir.x, 0).normalized() : Vector3(0, -dir.z, dir.y).normalized();
  294. normal *= debug_shape_thickness / scale_factor;
  295. int vertices_strip_order[14] = { 4, 5, 0, 1, 2, 5, 6, 4, 7, 0, 3, 2, 7, 6 };
  296. for (int v = 0; v < 14; v++) {
  297. Vector3 vertex = vertices_strip_order[v] < 4 ? normal : normal / 3.0 + cast_to;
  298. debug_shape_vertices.push_back(vertex.rotated(dir, Math_PI * (0.5 * (vertices_strip_order[v] % 4) + 0.25)));
  299. }
  300. }
  301. }
  302. void RayCast::set_debug_shape_thickness(const int p_debug_shape_thickness) {
  303. debug_shape_thickness = p_debug_shape_thickness;
  304. update_gizmo();
  305. if (Engine::get_singleton()->is_editor_hint()) {
  306. if (is_inside_tree()) {
  307. _update_debug_shape_vertices();
  308. }
  309. } else if (debug_shape) {
  310. _update_debug_shape();
  311. }
  312. }
  313. const Vector<Vector3> &RayCast::get_debug_shape_vertices() const {
  314. return debug_shape_vertices;
  315. }
  316. const Vector<Vector3> &RayCast::get_debug_line_vertices() const {
  317. return debug_line_vertices;
  318. }
  319. void RayCast::set_debug_shape_custom_color(const Color &p_color) {
  320. debug_shape_custom_color = p_color;
  321. if (debug_material.is_valid()) {
  322. _update_debug_shape_material();
  323. }
  324. }
  325. Ref<SpatialMaterial> RayCast::get_debug_material() {
  326. _update_debug_shape_material();
  327. return debug_material;
  328. }
  329. const Color &RayCast::get_debug_shape_custom_color() const {
  330. return debug_shape_custom_color;
  331. }
  332. void RayCast::_create_debug_shape() {
  333. _update_debug_shape_material();
  334. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  335. MeshInstance *mi = memnew(MeshInstance);
  336. #ifdef TOOLS_ENABLED
  337. // This enables the debug helper to show up in editor runs.
  338. // However it should not show up during export, because global mode
  339. // can slow the portal system, and this should only be used for debugging.
  340. mi->set_portal_mode(CullInstance::PORTAL_MODE_GLOBAL);
  341. #endif
  342. mi->set_mesh(mesh);
  343. add_child(mi);
  344. debug_shape = mi;
  345. }
  346. void RayCast::_update_debug_shape_material(bool p_check_collision) {
  347. if (!debug_material.is_valid()) {
  348. Ref<SpatialMaterial> material = memnew(SpatialMaterial);
  349. debug_material = material;
  350. material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  351. material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  352. // Use double-sided rendering so that the RayCast can be seen if the camera is inside.
  353. material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  354. }
  355. Color color = debug_shape_custom_color;
  356. if (color == Color(0.0, 0.0, 0.0)) {
  357. // Use the default debug shape color defined in the Project Settings.
  358. color = get_tree()->get_debug_collisions_color();
  359. }
  360. if (p_check_collision && collided) {
  361. if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) {
  362. // If base color is already quite reddish, highlight collision with green color
  363. color = Color(0.0, 1.0, 0.0, color.a);
  364. } else {
  365. // Else, highlight collision with red color
  366. color = Color(1.0, 0, 0, color.a);
  367. }
  368. }
  369. Ref<SpatialMaterial> material = static_cast<Ref<SpatialMaterial>>(debug_material);
  370. material->set_albedo(color);
  371. }
  372. void RayCast::_update_debug_shape() {
  373. if (!enabled) {
  374. return;
  375. }
  376. if (!debug_shape) {
  377. _create_debug_shape();
  378. }
  379. MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
  380. Ref<ArrayMesh> mesh = mi->get_mesh();
  381. if (!mesh.is_valid()) {
  382. return;
  383. }
  384. _update_debug_shape_vertices();
  385. mesh->clear_surfaces();
  386. Array a;
  387. a.resize(Mesh::ARRAY_MAX);
  388. uint32_t flags = 0;
  389. int surface_count = 0;
  390. if (!debug_line_vertices.empty()) {
  391. a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
  392. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), flags);
  393. mesh->surface_set_material(surface_count, debug_material);
  394. ++surface_count;
  395. }
  396. if (!debug_shape_vertices.empty()) {
  397. a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
  398. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), flags);
  399. mesh->surface_set_material(surface_count, debug_material);
  400. ++surface_count;
  401. }
  402. }
  403. void RayCast::_clear_debug_shape() {
  404. if (!debug_shape) {
  405. return;
  406. }
  407. MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
  408. if (mi->is_inside_tree()) {
  409. mi->queue_delete();
  410. } else {
  411. memdelete(mi);
  412. }
  413. debug_shape = nullptr;
  414. }
  415. RayCast::RayCast() {
  416. enabled = false;
  417. against = 0;
  418. collided = false;
  419. against_shape = 0;
  420. collision_mask = 1;
  421. cast_to = Vector3(0, -1, 0);
  422. debug_shape = nullptr;
  423. exclude_parent_body = true;
  424. collide_with_areas = false;
  425. collide_with_bodies = true;
  426. }