ray_cast_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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-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_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. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint())
  80. set_physics_process_internal(p_enabled);
  81. if (!p_enabled)
  82. collided = false;
  83. }
  84. bool RayCast2D::is_enabled() const {
  85. return enabled;
  86. }
  87. void RayCast2D::set_exclude_parent_body(bool p_exclude_parent_body) {
  88. if (exclude_parent_body == p_exclude_parent_body)
  89. return;
  90. exclude_parent_body = p_exclude_parent_body;
  91. if (!is_inside_tree())
  92. return;
  93. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  94. if (exclude_parent_body)
  95. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  96. else
  97. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  98. }
  99. }
  100. bool RayCast2D::get_exclude_parent_body() const {
  101. return exclude_parent_body;
  102. }
  103. void RayCast2D::_notification(int p_what) {
  104. switch (p_what) {
  105. case NOTIFICATION_ENTER_TREE: {
  106. if (enabled && !Engine::get_singleton()->is_editor_hint())
  107. set_physics_process_internal(true);
  108. else
  109. set_physics_process_internal(false);
  110. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  111. if (exclude_parent_body)
  112. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  113. else
  114. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  115. }
  116. } break;
  117. case NOTIFICATION_EXIT_TREE: {
  118. if (enabled)
  119. set_physics_process_internal(false);
  120. } break;
  121. case NOTIFICATION_DRAW: {
  122. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint())
  123. break;
  124. Transform2D xf;
  125. xf.rotate(cast_to.angle());
  126. xf.translate(Vector2(cast_to.length(), 0));
  127. //Vector2 tip = Vector2(0,s->get_length());
  128. Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
  129. draw_line(Vector2(), cast_to, dcol, 3);
  130. Vector<Vector2> pts;
  131. float tsize = 4;
  132. pts.push_back(xf.xform(Vector2(tsize, 0)));
  133. pts.push_back(xf.xform(Vector2(0, 0.707 * tsize)));
  134. pts.push_back(xf.xform(Vector2(0, -0.707 * tsize)));
  135. Vector<Color> cols;
  136. for (int i = 0; i < 3; i++)
  137. cols.push_back(dcol);
  138. draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
  139. } break;
  140. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  141. if (!enabled)
  142. break;
  143. _update_raycast_state();
  144. } break;
  145. }
  146. }
  147. void RayCast2D::_update_raycast_state() {
  148. Ref<World2D> w2d = get_world_2d();
  149. ERR_FAIL_COND(w2d.is_null());
  150. Physics2DDirectSpaceState *dss = Physics2DServer::get_singleton()->space_get_direct_state(w2d->get_space());
  151. ERR_FAIL_COND(!dss);
  152. Transform2D gt = get_global_transform();
  153. Vector2 to = cast_to;
  154. if (to == Vector2())
  155. to = Vector2(0, 0.01);
  156. Physics2DDirectSpaceState::RayResult rr;
  157. if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, collision_mask, collide_with_bodies, collide_with_areas)) {
  158. collided = true;
  159. against = rr.collider_id;
  160. collision_point = rr.position;
  161. collision_normal = rr.normal;
  162. against_shape = rr.shape;
  163. } else {
  164. collided = false;
  165. against = 0;
  166. against_shape = 0;
  167. }
  168. }
  169. void RayCast2D::force_raycast_update() {
  170. _update_raycast_state();
  171. }
  172. void RayCast2D::add_exception_rid(const RID &p_rid) {
  173. exclude.insert(p_rid);
  174. }
  175. void RayCast2D::add_exception(const Object *p_object) {
  176. ERR_FAIL_NULL(p_object);
  177. const CollisionObject2D *co = Object::cast_to<CollisionObject2D>(p_object);
  178. if (!co)
  179. return;
  180. add_exception_rid(co->get_rid());
  181. }
  182. void RayCast2D::remove_exception_rid(const RID &p_rid) {
  183. exclude.erase(p_rid);
  184. }
  185. void RayCast2D::remove_exception(const Object *p_object) {
  186. ERR_FAIL_NULL(p_object);
  187. const CollisionObject2D *co = Object::cast_to<CollisionObject2D>(p_object);
  188. if (!co)
  189. return;
  190. remove_exception_rid(co->get_rid());
  191. }
  192. void RayCast2D::clear_exceptions() {
  193. exclude.clear();
  194. }
  195. void RayCast2D::set_collide_with_areas(bool p_clip) {
  196. collide_with_areas = p_clip;
  197. }
  198. bool RayCast2D::is_collide_with_areas_enabled() const {
  199. return collide_with_areas;
  200. }
  201. void RayCast2D::set_collide_with_bodies(bool p_clip) {
  202. collide_with_bodies = p_clip;
  203. }
  204. bool RayCast2D::is_collide_with_bodies_enabled() const {
  205. return collide_with_bodies;
  206. }
  207. void RayCast2D::_bind_methods() {
  208. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast2D::set_enabled);
  209. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast2D::is_enabled);
  210. ClassDB::bind_method(D_METHOD("set_cast_to", "local_point"), &RayCast2D::set_cast_to);
  211. ClassDB::bind_method(D_METHOD("get_cast_to"), &RayCast2D::get_cast_to);
  212. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast2D::is_colliding);
  213. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast2D::force_raycast_update);
  214. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast2D::get_collider);
  215. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast2D::get_collider_shape);
  216. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast2D::get_collision_point);
  217. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast2D::get_collision_normal);
  218. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast2D::add_exception_rid);
  219. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast2D::add_exception);
  220. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast2D::remove_exception_rid);
  221. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast2D::remove_exception);
  222. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast2D::clear_exceptions);
  223. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast2D::set_collision_mask);
  224. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast2D::get_collision_mask);
  225. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &RayCast2D::set_collision_mask_bit);
  226. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &RayCast2D::get_collision_mask_bit);
  227. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast2D::set_exclude_parent_body);
  228. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast2D::get_exclude_parent_body);
  229. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast2D::set_collide_with_areas);
  230. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast2D::is_collide_with_areas_enabled);
  231. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast2D::set_collide_with_bodies);
  232. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast2D::is_collide_with_bodies_enabled);
  233. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  234. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  235. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cast_to"), "set_cast_to", "get_cast_to");
  236. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  237. ADD_GROUP("Collide With", "collide_with");
  238. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  239. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  240. }
  241. RayCast2D::RayCast2D() {
  242. enabled = false;
  243. against = 0;
  244. collided = false;
  245. against_shape = 0;
  246. collision_mask = 1;
  247. cast_to = Vector2(0, 50);
  248. exclude_parent_body = true;
  249. collide_with_bodies = true;
  250. collide_with_areas = false;
  251. }