ray_cast_2d.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "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_type_mask(uint32_t p_mask) {
  50. type_mask = p_mask;
  51. }
  52. void RayCast2D::set_collision_mask_bit(int p_bit, bool p_value) {
  53. uint32_t mask = get_collision_mask();
  54. if (p_value)
  55. mask |= 1 << p_bit;
  56. else
  57. mask &= ~(1 << p_bit);
  58. set_collision_mask(mask);
  59. }
  60. bool RayCast2D::get_collision_mask_bit(int p_bit) const {
  61. return get_collision_mask() & (1 << p_bit);
  62. }
  63. uint32_t RayCast2D::get_type_mask() const {
  64. return type_mask;
  65. }
  66. bool RayCast2D::is_colliding() const {
  67. return collided;
  68. }
  69. Object *RayCast2D::get_collider() const {
  70. if (against == 0)
  71. return NULL;
  72. return ObjectDB::get_instance(against);
  73. }
  74. int RayCast2D::get_collider_shape() const {
  75. return against_shape;
  76. }
  77. Vector2 RayCast2D::get_collision_point() const {
  78. return collision_point;
  79. }
  80. Vector2 RayCast2D::get_collision_normal() const {
  81. return collision_normal;
  82. }
  83. void RayCast2D::set_enabled(bool p_enabled) {
  84. enabled = p_enabled;
  85. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint())
  86. set_physics_process(p_enabled);
  87. if (!p_enabled)
  88. collided = false;
  89. }
  90. bool RayCast2D::is_enabled() const {
  91. return enabled;
  92. }
  93. void RayCast2D::set_exclude_parent_body(bool p_exclude_parent_body) {
  94. if (exclude_parent_body == p_exclude_parent_body)
  95. return;
  96. exclude_parent_body = p_exclude_parent_body;
  97. if (!is_inside_tree())
  98. return;
  99. if (Object::cast_to<PhysicsBody2D>(get_parent())) {
  100. if (exclude_parent_body)
  101. exclude.insert(Object::cast_to<PhysicsBody2D>(get_parent())->get_rid());
  102. else
  103. exclude.erase(Object::cast_to<PhysicsBody2D>(get_parent())->get_rid());
  104. }
  105. }
  106. bool RayCast2D::get_exclude_parent_body() const {
  107. return exclude_parent_body;
  108. }
  109. void RayCast2D::_notification(int p_what) {
  110. switch (p_what) {
  111. case NOTIFICATION_ENTER_TREE: {
  112. if (enabled && !Engine::get_singleton()->is_editor_hint())
  113. set_physics_process(true);
  114. else
  115. set_physics_process(false);
  116. if (Object::cast_to<PhysicsBody2D>(get_parent())) {
  117. if (exclude_parent_body)
  118. exclude.insert(Object::cast_to<PhysicsBody2D>(get_parent())->get_rid());
  119. else
  120. exclude.erase(Object::cast_to<PhysicsBody2D>(get_parent())->get_rid());
  121. }
  122. } break;
  123. case NOTIFICATION_EXIT_TREE: {
  124. if (enabled)
  125. set_physics_process(false);
  126. } break;
  127. case NOTIFICATION_DRAW: {
  128. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint())
  129. break;
  130. Transform2D xf;
  131. xf.rotate(cast_to.angle());
  132. xf.translate(Vector2(cast_to.length(), 0));
  133. //Vector2 tip = Vector2(0,s->get_length());
  134. Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
  135. draw_line(Vector2(), cast_to, dcol, 3);
  136. Vector<Vector2> pts;
  137. float tsize = 4;
  138. pts.push_back(xf.xform(Vector2(tsize, 0)));
  139. pts.push_back(xf.xform(Vector2(0, 0.707 * tsize)));
  140. pts.push_back(xf.xform(Vector2(0, -0.707 * tsize)));
  141. Vector<Color> cols;
  142. for (int i = 0; i < 3; i++)
  143. cols.push_back(dcol);
  144. draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
  145. } break;
  146. case NOTIFICATION_PHYSICS_PROCESS: {
  147. if (!enabled)
  148. break;
  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. Physics2DDirectSpaceState *dss = Physics2DServer::get_singleton()->space_get_direct_state(w2d->get_space());
  157. ERR_FAIL_COND(!dss);
  158. Transform2D gt = get_global_transform();
  159. Vector2 to = cast_to;
  160. if (to == Vector2())
  161. to = Vector2(0, 0.01);
  162. Physics2DDirectSpaceState::RayResult rr;
  163. if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, collision_mask, type_mask)) {
  164. collided = true;
  165. against = rr.collider_id;
  166. collision_point = rr.position;
  167. collision_normal = rr.normal;
  168. against_shape = rr.shape;
  169. } else {
  170. collided = false;
  171. }
  172. }
  173. void RayCast2D::force_raycast_update() {
  174. _update_raycast_state();
  175. }
  176. void RayCast2D::add_exception_rid(const RID &p_rid) {
  177. exclude.insert(p_rid);
  178. }
  179. void RayCast2D::add_exception(const Object *p_object) {
  180. ERR_FAIL_NULL(p_object);
  181. const CollisionObject2D *co = Object::cast_to<CollisionObject2D>(p_object);
  182. if (!co)
  183. return;
  184. add_exception_rid(co->get_rid());
  185. }
  186. void RayCast2D::remove_exception_rid(const RID &p_rid) {
  187. exclude.erase(p_rid);
  188. }
  189. void RayCast2D::remove_exception(const Object *p_object) {
  190. ERR_FAIL_NULL(p_object);
  191. const CollisionObject2D *co = Object::cast_to<CollisionObject2D>(p_object);
  192. if (!co)
  193. return;
  194. remove_exception_rid(co->get_rid());
  195. }
  196. void RayCast2D::clear_exceptions() {
  197. exclude.clear();
  198. }
  199. void RayCast2D::_bind_methods() {
  200. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast2D::set_enabled);
  201. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast2D::is_enabled);
  202. ClassDB::bind_method(D_METHOD("set_cast_to", "local_point"), &RayCast2D::set_cast_to);
  203. ClassDB::bind_method(D_METHOD("get_cast_to"), &RayCast2D::get_cast_to);
  204. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast2D::is_colliding);
  205. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast2D::force_raycast_update);
  206. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast2D::get_collider);
  207. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast2D::get_collider_shape);
  208. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast2D::get_collision_point);
  209. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast2D::get_collision_normal);
  210. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast2D::add_exception_rid);
  211. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast2D::add_exception);
  212. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast2D::remove_exception_rid);
  213. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast2D::remove_exception);
  214. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast2D::clear_exceptions);
  215. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast2D::set_collision_mask);
  216. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast2D::get_collision_mask);
  217. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &RayCast2D::set_collision_mask_bit);
  218. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &RayCast2D::get_collision_mask_bit);
  219. ClassDB::bind_method(D_METHOD("set_type_mask", "mask"), &RayCast2D::set_type_mask);
  220. ClassDB::bind_method(D_METHOD("get_type_mask"), &RayCast2D::get_type_mask);
  221. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast2D::set_exclude_parent_body);
  222. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast2D::get_exclude_parent_body);
  223. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  224. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  225. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cast_to"), "set_cast_to", "get_cast_to");
  226. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  227. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_mask", PROPERTY_HINT_FLAGS, "Static,Kinematic,Rigid,Character,Area"), "set_type_mask", "get_type_mask");
  228. }
  229. RayCast2D::RayCast2D() {
  230. enabled = false;
  231. against = 0;
  232. collided = false;
  233. against_shape = 0;
  234. collision_mask = 1;
  235. type_mask = Physics2DDirectSpaceState::TYPE_MASK_COLLISION;
  236. cast_to = Vector2(0, 50);
  237. exclude_parent_body = true;
  238. }