ray_cast.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*************************************************************************/
  2. /* ray_cast.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_collisions_hint()))
  38. update_gizmo();
  39. if (is_inside_tree() && get_tree()->is_debugging_collisions_hint())
  40. _update_debug_shape();
  41. }
  42. Vector3 RayCast::get_cast_to() const {
  43. return cast_to;
  44. }
  45. void RayCast::set_collision_mask(uint32_t p_mask) {
  46. collision_mask = p_mask;
  47. }
  48. uint32_t RayCast::get_collision_mask() const {
  49. return collision_mask;
  50. }
  51. void RayCast::set_collision_mask_bit(int p_bit, bool p_value) {
  52. uint32_t mask = get_collision_mask();
  53. if (p_value)
  54. mask |= 1 << p_bit;
  55. else
  56. mask &= ~(1 << p_bit);
  57. set_collision_mask(mask);
  58. }
  59. bool RayCast::get_collision_mask_bit(int p_bit) const {
  60. return get_collision_mask() & (1 << p_bit);
  61. }
  62. bool RayCast::is_colliding() const {
  63. return collided;
  64. }
  65. Object *RayCast::get_collider() const {
  66. if (against == 0)
  67. return NULL;
  68. return ObjectDB::get_instance(against);
  69. }
  70. int RayCast::get_collider_shape() const {
  71. return against_shape;
  72. }
  73. Vector3 RayCast::get_collision_point() const {
  74. return collision_point;
  75. }
  76. Vector3 RayCast::get_collision_normal() const {
  77. return collision_normal;
  78. }
  79. void RayCast::set_enabled(bool p_enabled) {
  80. enabled = p_enabled;
  81. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint())
  82. set_physics_process_internal(p_enabled);
  83. if (!p_enabled)
  84. collided = false;
  85. if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) {
  86. if (p_enabled)
  87. _update_debug_shape();
  88. else
  89. _clear_debug_shape();
  90. }
  91. }
  92. bool RayCast::is_enabled() const {
  93. return enabled;
  94. }
  95. void RayCast::set_exclude_parent_body(bool p_exclude_parent_body) {
  96. if (exclude_parent_body == p_exclude_parent_body)
  97. return;
  98. exclude_parent_body = p_exclude_parent_body;
  99. if (!is_inside_tree())
  100. return;
  101. if (Object::cast_to<CollisionObject>(get_parent())) {
  102. if (exclude_parent_body)
  103. exclude.insert(Object::cast_to<CollisionObject>(get_parent())->get_rid());
  104. else
  105. exclude.erase(Object::cast_to<CollisionObject>(get_parent())->get_rid());
  106. }
  107. }
  108. bool RayCast::get_exclude_parent_body() const {
  109. return exclude_parent_body;
  110. }
  111. void RayCast::_notification(int p_what) {
  112. switch (p_what) {
  113. case NOTIFICATION_ENTER_TREE: {
  114. if (enabled && !Engine::get_singleton()->is_editor_hint()) {
  115. set_physics_process_internal(true);
  116. if (get_tree()->is_debugging_collisions_hint())
  117. _update_debug_shape();
  118. } else
  119. set_physics_process_internal(false);
  120. if (Object::cast_to<CollisionObject>(get_parent())) {
  121. if (exclude_parent_body)
  122. exclude.insert(Object::cast_to<CollisionObject>(get_parent())->get_rid());
  123. else
  124. exclude.erase(Object::cast_to<CollisionObject>(get_parent())->get_rid());
  125. }
  126. } break;
  127. case NOTIFICATION_EXIT_TREE: {
  128. if (enabled) {
  129. set_physics_process_internal(false);
  130. }
  131. if (debug_shape)
  132. _clear_debug_shape();
  133. } break;
  134. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  135. if (!enabled)
  136. break;
  137. bool prev_collision_state = collided;
  138. _update_raycast_state();
  139. if (prev_collision_state != collided && get_tree()->is_debugging_collisions_hint()) {
  140. if (debug_material.is_valid()) {
  141. Ref<SpatialMaterial> line_material = static_cast<Ref<SpatialMaterial> >(debug_material);
  142. line_material->set_albedo(collided ? Color(1.0, 0, 0) : Color(1.0, 0.8, 0.6));
  143. }
  144. }
  145. } break;
  146. }
  147. }
  148. void RayCast::_update_raycast_state() {
  149. Ref<World> w3d = get_world();
  150. ERR_FAIL_COND(w3d.is_null());
  151. PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(w3d->get_space());
  152. ERR_FAIL_COND(!dss);
  153. Transform gt = get_global_transform();
  154. Vector3 to = cast_to;
  155. if (to == Vector3())
  156. to = Vector3(0, 0.01, 0);
  157. PhysicsDirectSpaceState::RayResult rr;
  158. if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, collision_mask, collide_with_bodies, collide_with_areas)) {
  159. collided = true;
  160. against = rr.collider_id;
  161. collision_point = rr.position;
  162. collision_normal = rr.normal;
  163. against_shape = rr.shape;
  164. } else {
  165. collided = false;
  166. against = 0;
  167. against_shape = 0;
  168. }
  169. }
  170. void RayCast::force_raycast_update() {
  171. _update_raycast_state();
  172. }
  173. void RayCast::add_exception_rid(const RID &p_rid) {
  174. exclude.insert(p_rid);
  175. }
  176. void RayCast::add_exception(const Object *p_object) {
  177. ERR_FAIL_NULL(p_object);
  178. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  179. if (!co)
  180. return;
  181. add_exception_rid(co->get_rid());
  182. }
  183. void RayCast::remove_exception_rid(const RID &p_rid) {
  184. exclude.erase(p_rid);
  185. }
  186. void RayCast::remove_exception(const Object *p_object) {
  187. ERR_FAIL_NULL(p_object);
  188. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  189. if (!co)
  190. return;
  191. remove_exception_rid(co->get_rid());
  192. }
  193. void RayCast::clear_exceptions() {
  194. exclude.clear();
  195. }
  196. void RayCast::set_collide_with_areas(bool p_clip) {
  197. collide_with_areas = p_clip;
  198. }
  199. bool RayCast::is_collide_with_areas_enabled() const {
  200. return collide_with_areas;
  201. }
  202. void RayCast::set_collide_with_bodies(bool p_clip) {
  203. collide_with_bodies = p_clip;
  204. }
  205. bool RayCast::is_collide_with_bodies_enabled() const {
  206. return collide_with_bodies;
  207. }
  208. void RayCast::_bind_methods() {
  209. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast::set_enabled);
  210. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast::is_enabled);
  211. ClassDB::bind_method(D_METHOD("set_cast_to", "local_point"), &RayCast::set_cast_to);
  212. ClassDB::bind_method(D_METHOD("get_cast_to"), &RayCast::get_cast_to);
  213. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast::is_colliding);
  214. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast::force_raycast_update);
  215. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast::get_collider);
  216. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast::get_collider_shape);
  217. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast::get_collision_point);
  218. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast::get_collision_normal);
  219. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast::add_exception_rid);
  220. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast::add_exception);
  221. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast::remove_exception_rid);
  222. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast::remove_exception);
  223. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast::clear_exceptions);
  224. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast::set_collision_mask);
  225. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast::get_collision_mask);
  226. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &RayCast::set_collision_mask_bit);
  227. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &RayCast::get_collision_mask_bit);
  228. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast::set_exclude_parent_body);
  229. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast::get_exclude_parent_body);
  230. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast::set_collide_with_areas);
  231. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast::is_collide_with_areas_enabled);
  232. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast::set_collide_with_bodies);
  233. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast::is_collide_with_bodies_enabled);
  234. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  235. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  236. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cast_to"), "set_cast_to", "get_cast_to");
  237. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  238. ADD_GROUP("Collide With", "collide_with");
  239. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  240. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  241. }
  242. void RayCast::_create_debug_shape() {
  243. if (!debug_material.is_valid()) {
  244. debug_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  245. Ref<SpatialMaterial> line_material = static_cast<Ref<SpatialMaterial> >(debug_material);
  246. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  247. line_material->set_line_width(3.0);
  248. line_material->set_albedo(Color(1.0, 0.8, 0.6));
  249. }
  250. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  251. MeshInstance *mi = memnew(MeshInstance);
  252. mi->set_mesh(mesh);
  253. add_child(mi);
  254. debug_shape = mi;
  255. }
  256. void RayCast::_update_debug_shape() {
  257. if (!enabled)
  258. return;
  259. if (!debug_shape)
  260. _create_debug_shape();
  261. MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
  262. if (!mi->get_mesh().is_valid())
  263. return;
  264. Ref<ArrayMesh> mesh = mi->get_mesh();
  265. if (mesh->get_surface_count() > 0)
  266. mesh->surface_remove(0);
  267. Array a;
  268. a.resize(Mesh::ARRAY_MAX);
  269. Vector<Vector3> verts;
  270. verts.push_back(Vector3());
  271. verts.push_back(cast_to);
  272. a[Mesh::ARRAY_VERTEX] = verts;
  273. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
  274. mesh->surface_set_material(0, debug_material);
  275. }
  276. void RayCast::_clear_debug_shape() {
  277. if (!debug_shape)
  278. return;
  279. MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
  280. if (mi->is_inside_tree())
  281. mi->queue_delete();
  282. else
  283. memdelete(mi);
  284. debug_shape = NULL;
  285. }
  286. RayCast::RayCast() {
  287. enabled = false;
  288. against = 0;
  289. collided = false;
  290. against_shape = 0;
  291. collision_mask = 1;
  292. cast_to = Vector3(0, -1, 0);
  293. debug_shape = NULL;
  294. exclude_parent_body = true;
  295. collide_with_areas = false;
  296. collide_with_bodies = true;
  297. }