ray_cast_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*************************************************************************/
  2. /* ray_cast_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "core/engine.h"
  33. #include "physics_body_2d.h"
  34. #include "servers/physics_2d_server.h"
  35. void RayCast2D::set_cast_to(const Vector2 &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();
  39. }
  40. Vector2 RayCast2D::get_cast_to() const {
  41. return cast_to;
  42. }
  43. void RayCast2D::set_collision_mask(uint32_t p_mask) {
  44. collision_mask = p_mask;
  45. }
  46. uint32_t RayCast2D::get_collision_mask() const {
  47. return collision_mask;
  48. }
  49. void RayCast2D::set_collision_mask_bit(int p_bit, bool p_value) {
  50. uint32_t mask = get_collision_mask();
  51. if (p_value)
  52. mask |= 1 << p_bit;
  53. else
  54. mask &= ~(1 << p_bit);
  55. set_collision_mask(mask);
  56. }
  57. bool RayCast2D::get_collision_mask_bit(int p_bit) const {
  58. return get_collision_mask() & (1 << p_bit);
  59. }
  60. bool RayCast2D::is_colliding() const {
  61. return collided;
  62. }
  63. Object *RayCast2D::get_collider() const {
  64. if (against == 0)
  65. return NULL;
  66. return ObjectDB::get_instance(against);
  67. }
  68. int RayCast2D::get_collider_shape() const {
  69. return against_shape;
  70. }
  71. Vector2 RayCast2D::get_collision_point() const {
  72. return collision_point;
  73. }
  74. Vector2 RayCast2D::get_collision_normal() const {
  75. return collision_normal;
  76. }
  77. void RayCast2D::set_enabled(bool p_enabled) {
  78. enabled = p_enabled;
  79. update();
  80. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint())
  81. set_physics_process_internal(p_enabled);
  82. if (!p_enabled)
  83. collided = false;
  84. }
  85. bool RayCast2D::is_enabled() const {
  86. return enabled;
  87. }
  88. void RayCast2D::set_exclude_parent_body(bool p_exclude_parent_body) {
  89. if (exclude_parent_body == p_exclude_parent_body)
  90. return;
  91. exclude_parent_body = p_exclude_parent_body;
  92. if (!is_inside_tree())
  93. return;
  94. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  95. if (exclude_parent_body)
  96. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  97. else
  98. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  99. }
  100. }
  101. bool RayCast2D::get_exclude_parent_body() const {
  102. return exclude_parent_body;
  103. }
  104. void RayCast2D::_notification(int p_what) {
  105. switch (p_what) {
  106. case NOTIFICATION_ENTER_TREE: {
  107. if (enabled && !Engine::get_singleton()->is_editor_hint())
  108. set_physics_process_internal(true);
  109. else
  110. set_physics_process_internal(false);
  111. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  112. if (exclude_parent_body)
  113. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  114. else
  115. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  116. }
  117. } break;
  118. case NOTIFICATION_EXIT_TREE: {
  119. if (enabled)
  120. set_physics_process_internal(false);
  121. } break;
  122. case NOTIFICATION_DRAW: {
  123. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint())
  124. break;
  125. Transform2D xf;
  126. xf.rotate(cast_to.angle());
  127. xf.translate(Vector2(cast_to.length(), 0));
  128. // Draw an arrow indicating where the RayCast is pointing to
  129. Color draw_col = get_tree()->get_debug_collisions_color();
  130. if (!enabled) {
  131. float g = draw_col.get_v();
  132. draw_col.r = g;
  133. draw_col.g = g;
  134. draw_col.b = g;
  135. }
  136. draw_line(Vector2(), cast_to, draw_col, 2, true);
  137. Vector<Vector2> pts;
  138. float tsize = 8;
  139. pts.push_back(xf.xform(Vector2(tsize, 0)));
  140. pts.push_back(xf.xform(Vector2(0, Math_SQRT12 * tsize)));
  141. pts.push_back(xf.xform(Vector2(0, -Math_SQRT12 * tsize)));
  142. Vector<Color> cols;
  143. for (int i = 0; i < 3; i++)
  144. cols.push_back(draw_col);
  145. draw_primitive(pts, cols, Vector<Vector2>());
  146. } break;
  147. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  148. if (!enabled)
  149. break;
  150. _update_raycast_state();
  151. } break;
  152. }
  153. }
  154. void RayCast2D::_update_raycast_state() {
  155. Ref<World2D> w2d = get_world_2d();
  156. ERR_FAIL_COND(w2d.is_null());
  157. Physics2DDirectSpaceState *dss = Physics2DServer::get_singleton()->space_get_direct_state(w2d->get_space());
  158. ERR_FAIL_COND(!dss);
  159. Transform2D gt = get_global_transform();
  160. Vector2 to = cast_to;
  161. if (to == Vector2())
  162. to = Vector2(0, 0.01);
  163. Physics2DDirectSpaceState::RayResult rr;
  164. if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, collision_mask, collide_with_bodies, collide_with_areas)) {
  165. collided = true;
  166. against = rr.collider_id;
  167. collision_point = rr.position;
  168. collision_normal = rr.normal;
  169. against_shape = rr.shape;
  170. } else {
  171. collided = false;
  172. against = 0;
  173. against_shape = 0;
  174. }
  175. }
  176. void RayCast2D::force_raycast_update() {
  177. _update_raycast_state();
  178. }
  179. void RayCast2D::add_exception_rid(const RID &p_rid) {
  180. exclude.insert(p_rid);
  181. }
  182. void RayCast2D::add_exception(const Object *p_object) {
  183. ERR_FAIL_NULL(p_object);
  184. const CollisionObject2D *co = Object::cast_to<CollisionObject2D>(p_object);
  185. if (!co)
  186. return;
  187. add_exception_rid(co->get_rid());
  188. }
  189. void RayCast2D::remove_exception_rid(const RID &p_rid) {
  190. exclude.erase(p_rid);
  191. }
  192. void RayCast2D::remove_exception(const Object *p_object) {
  193. ERR_FAIL_NULL(p_object);
  194. const CollisionObject2D *co = Object::cast_to<CollisionObject2D>(p_object);
  195. if (!co)
  196. return;
  197. remove_exception_rid(co->get_rid());
  198. }
  199. void RayCast2D::clear_exceptions() {
  200. exclude.clear();
  201. }
  202. void RayCast2D::set_collide_with_areas(bool p_clip) {
  203. collide_with_areas = p_clip;
  204. }
  205. bool RayCast2D::is_collide_with_areas_enabled() const {
  206. return collide_with_areas;
  207. }
  208. void RayCast2D::set_collide_with_bodies(bool p_clip) {
  209. collide_with_bodies = p_clip;
  210. }
  211. bool RayCast2D::is_collide_with_bodies_enabled() const {
  212. return collide_with_bodies;
  213. }
  214. void RayCast2D::_bind_methods() {
  215. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast2D::set_enabled);
  216. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast2D::is_enabled);
  217. ClassDB::bind_method(D_METHOD("set_cast_to", "local_point"), &RayCast2D::set_cast_to);
  218. ClassDB::bind_method(D_METHOD("get_cast_to"), &RayCast2D::get_cast_to);
  219. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast2D::is_colliding);
  220. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast2D::force_raycast_update);
  221. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast2D::get_collider);
  222. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast2D::get_collider_shape);
  223. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast2D::get_collision_point);
  224. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast2D::get_collision_normal);
  225. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast2D::add_exception_rid);
  226. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast2D::add_exception);
  227. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast2D::remove_exception_rid);
  228. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast2D::remove_exception);
  229. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast2D::clear_exceptions);
  230. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast2D::set_collision_mask);
  231. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast2D::get_collision_mask);
  232. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &RayCast2D::set_collision_mask_bit);
  233. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &RayCast2D::get_collision_mask_bit);
  234. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast2D::set_exclude_parent_body);
  235. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast2D::get_exclude_parent_body);
  236. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast2D::set_collide_with_areas);
  237. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast2D::is_collide_with_areas_enabled);
  238. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast2D::set_collide_with_bodies);
  239. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast2D::is_collide_with_bodies_enabled);
  240. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  241. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  242. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cast_to"), "set_cast_to", "get_cast_to");
  243. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  244. ADD_GROUP("Collide With", "collide_with");
  245. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  246. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  247. }
  248. RayCast2D::RayCast2D() {
  249. enabled = false;
  250. against = 0;
  251. collided = false;
  252. against_shape = 0;
  253. collision_mask = 1;
  254. cast_to = Vector2(0, 50);
  255. exclude_parent_body = true;
  256. collide_with_bodies = true;
  257. collide_with_areas = false;
  258. }