ray_cast.cpp 10 KB

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