ray_cast_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**************************************************************************/
  2. /* ray_cast_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "scene/2d/physics/collision_object_2d.h"
  32. #include "scene/resources/world_2d.h"
  33. void RayCast2D::set_target_position(const Vector2 &p_point) {
  34. target_position = p_point;
  35. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_collisions_hint())) {
  36. queue_redraw();
  37. }
  38. }
  39. Vector2 RayCast2D::get_target_position() const {
  40. return target_position;
  41. }
  42. void RayCast2D::set_collision_mask(uint32_t p_mask) {
  43. collision_mask = p_mask;
  44. }
  45. uint32_t RayCast2D::get_collision_mask() const {
  46. return collision_mask;
  47. }
  48. void RayCast2D::set_collision_mask_value(int p_layer_number, bool p_value) {
  49. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  50. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  51. uint32_t mask = get_collision_mask();
  52. if (p_value) {
  53. mask |= 1 << (p_layer_number - 1);
  54. } else {
  55. mask &= ~(1 << (p_layer_number - 1));
  56. }
  57. set_collision_mask(mask);
  58. }
  59. bool RayCast2D::get_collision_mask_value(int p_layer_number) const {
  60. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  61. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  62. return get_collision_mask() & (1 << (p_layer_number - 1));
  63. }
  64. bool RayCast2D::is_colliding() const {
  65. return collided;
  66. }
  67. Object *RayCast2D::get_collider() const {
  68. if (against.is_null()) {
  69. return nullptr;
  70. }
  71. return ObjectDB::get_instance(against);
  72. }
  73. RID RayCast2D::get_collider_rid() const {
  74. return against_rid;
  75. }
  76. int RayCast2D::get_collider_shape() const {
  77. return against_shape;
  78. }
  79. Vector2 RayCast2D::get_collision_point() const {
  80. return collision_point;
  81. }
  82. Vector2 RayCast2D::get_collision_normal() const {
  83. return collision_normal;
  84. }
  85. void RayCast2D::set_enabled(bool p_enabled) {
  86. enabled = p_enabled;
  87. queue_redraw();
  88. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  89. set_physics_process_internal(p_enabled);
  90. }
  91. if (!p_enabled) {
  92. collided = false;
  93. }
  94. }
  95. bool RayCast2D::is_enabled() const {
  96. return enabled;
  97. }
  98. void RayCast2D::set_exclude_parent_body(bool p_exclude_parent_body) {
  99. if (exclude_parent_body == p_exclude_parent_body) {
  100. return;
  101. }
  102. exclude_parent_body = p_exclude_parent_body;
  103. if (!is_inside_tree()) {
  104. return;
  105. }
  106. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  107. if (exclude_parent_body) {
  108. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  109. } else {
  110. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  111. }
  112. }
  113. }
  114. bool RayCast2D::get_exclude_parent_body() const {
  115. return exclude_parent_body;
  116. }
  117. void RayCast2D::_notification(int p_what) {
  118. switch (p_what) {
  119. case NOTIFICATION_ENTER_TREE: {
  120. if (enabled && !Engine::get_singleton()->is_editor_hint()) {
  121. set_physics_process_internal(true);
  122. } else {
  123. set_physics_process_internal(false);
  124. }
  125. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  126. if (exclude_parent_body) {
  127. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  128. } else {
  129. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  130. }
  131. }
  132. } break;
  133. case NOTIFICATION_EXIT_TREE: {
  134. if (enabled) {
  135. set_physics_process_internal(false);
  136. }
  137. } break;
  138. case NOTIFICATION_DRAW: {
  139. ERR_FAIL_COND(!is_inside_tree());
  140. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  141. break;
  142. }
  143. _draw_debug_shape();
  144. } break;
  145. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  146. if (!enabled) {
  147. break;
  148. }
  149. _update_raycast_state();
  150. } break;
  151. }
  152. }
  153. void RayCast2D::_update_raycast_state() {
  154. Ref<World2D> w2d = get_world_2d();
  155. ERR_FAIL_COND(w2d.is_null());
  156. PhysicsDirectSpaceState2D *dss = PhysicsServer2D::get_singleton()->space_get_direct_state(w2d->get_space());
  157. ERR_FAIL_NULL(dss);
  158. Transform2D gt = get_global_transform();
  159. Vector2 to = target_position;
  160. if (to == Vector2()) {
  161. to = Vector2(0, 0.01);
  162. }
  163. PhysicsDirectSpaceState2D::RayResult rr;
  164. bool prev_collision_state = collided;
  165. PhysicsDirectSpaceState2D::RayParameters ray_params;
  166. ray_params.from = gt.get_origin();
  167. ray_params.to = gt.xform(to);
  168. ray_params.exclude = exclude;
  169. ray_params.collision_mask = collision_mask;
  170. ray_params.collide_with_bodies = collide_with_bodies;
  171. ray_params.collide_with_areas = collide_with_areas;
  172. ray_params.hit_from_inside = hit_from_inside;
  173. if (dss->intersect_ray(ray_params, rr)) {
  174. collided = true;
  175. against = rr.collider_id;
  176. against_rid = rr.rid;
  177. collision_point = rr.position;
  178. collision_normal = rr.normal;
  179. against_shape = rr.shape;
  180. } else {
  181. collided = false;
  182. against = ObjectID();
  183. against_rid = RID();
  184. against_shape = 0;
  185. }
  186. if (prev_collision_state != collided) {
  187. queue_redraw();
  188. }
  189. }
  190. void RayCast2D::_draw_debug_shape() {
  191. Color draw_col = collided ? Color(1.0, 0.01, 0) : get_tree()->get_debug_collisions_color();
  192. if (!enabled) {
  193. const float g = draw_col.get_v();
  194. draw_col.r = g;
  195. draw_col.g = g;
  196. draw_col.b = g;
  197. }
  198. // Draw an arrow indicating where the RayCast is pointing to
  199. const real_t max_arrow_size = 6;
  200. const real_t line_width = 1.4;
  201. bool no_line = target_position.length() < line_width;
  202. real_t arrow_size = CLAMP(target_position.length() * 2 / 3, line_width, max_arrow_size);
  203. if (no_line) {
  204. arrow_size = target_position.length();
  205. } else {
  206. draw_line(Vector2(), target_position - target_position.normalized() * arrow_size, draw_col, line_width);
  207. }
  208. Transform2D xf;
  209. xf.rotate(target_position.angle());
  210. xf.translate_local(Vector2(no_line ? 0 : target_position.length() - arrow_size, 0));
  211. Vector<Vector2> pts = {
  212. xf.xform(Vector2(arrow_size, 0)),
  213. xf.xform(Vector2(0, 0.5 * arrow_size)),
  214. xf.xform(Vector2(0, -0.5 * arrow_size))
  215. };
  216. Vector<Color> cols = { draw_col, draw_col, draw_col };
  217. draw_primitive(pts, cols, Vector<Vector2>());
  218. }
  219. void RayCast2D::force_raycast_update() {
  220. _update_raycast_state();
  221. }
  222. void RayCast2D::add_exception_rid(const RID &p_rid) {
  223. exclude.insert(p_rid);
  224. }
  225. void RayCast2D::add_exception(const CollisionObject2D *p_node) {
  226. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject2D.");
  227. add_exception_rid(p_node->get_rid());
  228. }
  229. void RayCast2D::remove_exception_rid(const RID &p_rid) {
  230. exclude.erase(p_rid);
  231. }
  232. void RayCast2D::remove_exception(const CollisionObject2D *p_node) {
  233. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject2D.");
  234. remove_exception_rid(p_node->get_rid());
  235. }
  236. void RayCast2D::clear_exceptions() {
  237. exclude.clear();
  238. if (exclude_parent_body && is_inside_tree()) {
  239. CollisionObject2D *parent = Object::cast_to<CollisionObject2D>(get_parent());
  240. if (parent) {
  241. exclude.insert(parent->get_rid());
  242. }
  243. }
  244. }
  245. void RayCast2D::set_collide_with_areas(bool p_enabled) {
  246. collide_with_areas = p_enabled;
  247. }
  248. bool RayCast2D::is_collide_with_areas_enabled() const {
  249. return collide_with_areas;
  250. }
  251. void RayCast2D::set_collide_with_bodies(bool p_enabled) {
  252. collide_with_bodies = p_enabled;
  253. }
  254. bool RayCast2D::is_collide_with_bodies_enabled() const {
  255. return collide_with_bodies;
  256. }
  257. void RayCast2D::set_hit_from_inside(bool p_enabled) {
  258. hit_from_inside = p_enabled;
  259. }
  260. bool RayCast2D::is_hit_from_inside_enabled() const {
  261. return hit_from_inside;
  262. }
  263. void RayCast2D::_bind_methods() {
  264. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast2D::set_enabled);
  265. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast2D::is_enabled);
  266. ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &RayCast2D::set_target_position);
  267. ClassDB::bind_method(D_METHOD("get_target_position"), &RayCast2D::get_target_position);
  268. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast2D::is_colliding);
  269. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast2D::force_raycast_update);
  270. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast2D::get_collider);
  271. ClassDB::bind_method(D_METHOD("get_collider_rid"), &RayCast2D::get_collider_rid);
  272. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast2D::get_collider_shape);
  273. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast2D::get_collision_point);
  274. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast2D::get_collision_normal);
  275. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast2D::add_exception_rid);
  276. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast2D::add_exception);
  277. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast2D::remove_exception_rid);
  278. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast2D::remove_exception);
  279. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast2D::clear_exceptions);
  280. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast2D::set_collision_mask);
  281. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast2D::get_collision_mask);
  282. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &RayCast2D::set_collision_mask_value);
  283. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &RayCast2D::get_collision_mask_value);
  284. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast2D::set_exclude_parent_body);
  285. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast2D::get_exclude_parent_body);
  286. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast2D::set_collide_with_areas);
  287. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast2D::is_collide_with_areas_enabled);
  288. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast2D::set_collide_with_bodies);
  289. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast2D::is_collide_with_bodies_enabled);
  290. ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &RayCast2D::set_hit_from_inside);
  291. ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &RayCast2D::is_hit_from_inside_enabled);
  292. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  293. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  294. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_position", PROPERTY_HINT_NONE, "suffix:px"), "set_target_position", "get_target_position");
  295. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  296. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
  297. ADD_GROUP("Collide With", "collide_with");
  298. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  299. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  300. }
  301. RayCast2D::RayCast2D() {
  302. set_hide_clip_children(true);
  303. }