ray_cast_2d.cpp 11 KB

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