ray_cast.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*************************************************************************/
  2. /* ray_cast.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "servers/physics_server.h"
  33. void RayCast::set_cast_to(const Vector3 &p_point) {
  34. cast_to = p_point;
  35. if (is_inside_tree() && (get_tree()->is_editor_hint() || get_tree()->is_debugging_collisions_hint()))
  36. update_gizmo();
  37. }
  38. Vector3 RayCast::get_cast_to() const {
  39. return cast_to;
  40. }
  41. void RayCast::set_layer_mask(uint32_t p_mask) {
  42. layer_mask = p_mask;
  43. }
  44. uint32_t RayCast::get_layer_mask() const {
  45. return layer_mask;
  46. }
  47. void RayCast::set_type_mask(uint32_t p_mask) {
  48. type_mask = p_mask;
  49. }
  50. uint32_t RayCast::get_type_mask() const {
  51. return type_mask;
  52. }
  53. bool RayCast::is_colliding() const {
  54. return collided;
  55. }
  56. Object *RayCast::get_collider() const {
  57. if (against == 0)
  58. return NULL;
  59. return ObjectDB::get_instance(against);
  60. }
  61. int RayCast::get_collider_shape() const {
  62. return against_shape;
  63. }
  64. Vector3 RayCast::get_collision_point() const {
  65. return collision_point;
  66. }
  67. Vector3 RayCast::get_collision_normal() const {
  68. return collision_normal;
  69. }
  70. void RayCast::set_enabled(bool p_enabled) {
  71. enabled = p_enabled;
  72. if (is_inside_tree() && !get_tree()->is_editor_hint())
  73. set_fixed_process(p_enabled);
  74. if (!p_enabled)
  75. collided = false;
  76. }
  77. bool RayCast::is_enabled() const {
  78. return enabled;
  79. }
  80. void RayCast::_notification(int p_what) {
  81. switch (p_what) {
  82. case NOTIFICATION_ENTER_TREE: {
  83. if (enabled && !get_tree()->is_editor_hint()) {
  84. set_fixed_process(true);
  85. } else
  86. set_fixed_process(false);
  87. } break;
  88. case NOTIFICATION_EXIT_TREE: {
  89. if (enabled) {
  90. set_fixed_process(false);
  91. }
  92. } break;
  93. case NOTIFICATION_FIXED_PROCESS: {
  94. if (!enabled)
  95. break;
  96. _update_raycast_state();
  97. } break;
  98. }
  99. }
  100. void RayCast::_update_raycast_state() {
  101. Ref<World> w3d = get_world();
  102. ERR_FAIL_COND(w3d.is_null());
  103. PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(w3d->get_space());
  104. ERR_FAIL_COND(!dss);
  105. Transform gt = get_global_transform();
  106. Vector3 to = cast_to;
  107. if (to == Vector3())
  108. to = Vector3(0, 0.01, 0);
  109. PhysicsDirectSpaceState::RayResult rr;
  110. if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, layer_mask, type_mask)) {
  111. collided = true;
  112. against = rr.collider_id;
  113. collision_point = rr.position;
  114. collision_normal = rr.normal;
  115. against_shape = rr.shape;
  116. } else {
  117. collided = false;
  118. }
  119. }
  120. void RayCast::force_raycast_update() {
  121. _update_raycast_state();
  122. }
  123. void RayCast::add_exception_rid(const RID &p_rid) {
  124. exclude.insert(p_rid);
  125. }
  126. void RayCast::add_exception(const Object *p_object) {
  127. ERR_FAIL_NULL(p_object);
  128. CollisionObject *co = ((Object *)p_object)->cast_to<CollisionObject>();
  129. if (!co)
  130. return;
  131. add_exception_rid(co->get_rid());
  132. }
  133. void RayCast::remove_exception_rid(const RID &p_rid) {
  134. exclude.erase(p_rid);
  135. }
  136. void RayCast::remove_exception(const Object *p_object) {
  137. ERR_FAIL_NULL(p_object);
  138. CollisionObject *co = ((Object *)p_object)->cast_to<CollisionObject>();
  139. if (!co)
  140. return;
  141. remove_exception_rid(co->get_rid());
  142. }
  143. void RayCast::clear_exceptions() {
  144. exclude.clear();
  145. }
  146. void RayCast::_bind_methods() {
  147. ObjectTypeDB::bind_method(_MD("set_enabled", "enabled"), &RayCast::set_enabled);
  148. ObjectTypeDB::bind_method(_MD("is_enabled"), &RayCast::is_enabled);
  149. ObjectTypeDB::bind_method(_MD("set_cast_to", "local_point"), &RayCast::set_cast_to);
  150. ObjectTypeDB::bind_method(_MD("get_cast_to"), &RayCast::get_cast_to);
  151. ObjectTypeDB::bind_method(_MD("is_colliding"), &RayCast::is_colliding);
  152. ObjectTypeDB::bind_method(_MD("force_raycast_update"), &RayCast::force_raycast_update);
  153. ObjectTypeDB::bind_method(_MD("get_collider"), &RayCast::get_collider);
  154. ObjectTypeDB::bind_method(_MD("get_collider_shape"), &RayCast::get_collider_shape);
  155. ObjectTypeDB::bind_method(_MD("get_collision_point"), &RayCast::get_collision_point);
  156. ObjectTypeDB::bind_method(_MD("get_collision_normal"), &RayCast::get_collision_normal);
  157. ObjectTypeDB::bind_method(_MD("add_exception_rid", "rid"), &RayCast::add_exception_rid);
  158. ObjectTypeDB::bind_method(_MD("add_exception", "node"), &RayCast::add_exception);
  159. ObjectTypeDB::bind_method(_MD("remove_exception_rid", "rid"), &RayCast::remove_exception_rid);
  160. ObjectTypeDB::bind_method(_MD("remove_exception", "node"), &RayCast::remove_exception);
  161. ObjectTypeDB::bind_method(_MD("clear_exceptions"), &RayCast::clear_exceptions);
  162. ObjectTypeDB::bind_method(_MD("set_layer_mask", "mask"), &RayCast::set_layer_mask);
  163. ObjectTypeDB::bind_method(_MD("get_layer_mask"), &RayCast::get_layer_mask);
  164. ObjectTypeDB::bind_method(_MD("set_type_mask", "mask"), &RayCast::set_type_mask);
  165. ObjectTypeDB::bind_method(_MD("get_type_mask"), &RayCast::get_type_mask);
  166. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), _SCS("set_enabled"), _SCS("is_enabled"));
  167. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cast_to"), _SCS("set_cast_to"), _SCS("get_cast_to"));
  168. ADD_PROPERTY(PropertyInfo(Variant::INT, "layer_mask", PROPERTY_HINT_ALL_FLAGS), _SCS("set_layer_mask"), _SCS("get_layer_mask"));
  169. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_mask", PROPERTY_HINT_FLAGS, "Static,Kinematic,Rigid,Character,Area"), _SCS("set_type_mask"), _SCS("get_type_mask"));
  170. }
  171. RayCast::RayCast() {
  172. enabled = false;
  173. against = 0;
  174. collided = false;
  175. against_shape = 0;
  176. layer_mask = 1;
  177. type_mask = PhysicsDirectSpaceState::TYPE_MASK_COLLISION;
  178. cast_to = Vector3(0, -1, 0);
  179. }