shape_cast_2d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**************************************************************************/
  2. /* shape_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 "shape_cast_2d.h"
  31. #include "core/config/engine.h"
  32. #include "scene/2d/collision_object_2d.h"
  33. #include "scene/2d/physics_body_2d.h"
  34. #include "scene/resources/circle_shape_2d.h"
  35. #include "servers/physics_2d/godot_physics_server_2d.h"
  36. void ShapeCast2D::set_target_position(const Vector2 &p_point) {
  37. target_position = p_point;
  38. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_collisions_hint())) {
  39. queue_redraw();
  40. }
  41. }
  42. Vector2 ShapeCast2D::get_target_position() const {
  43. return target_position;
  44. }
  45. void ShapeCast2D::set_margin(real_t p_margin) {
  46. margin = p_margin;
  47. }
  48. real_t ShapeCast2D::get_margin() const {
  49. return margin;
  50. }
  51. void ShapeCast2D::set_max_results(int p_max_results) {
  52. max_results = p_max_results;
  53. }
  54. int ShapeCast2D::get_max_results() const {
  55. return max_results;
  56. }
  57. void ShapeCast2D::set_collision_mask(uint32_t p_mask) {
  58. collision_mask = p_mask;
  59. }
  60. uint32_t ShapeCast2D::get_collision_mask() const {
  61. return collision_mask;
  62. }
  63. void ShapeCast2D::set_collision_mask_value(int p_layer_number, bool p_value) {
  64. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  65. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  66. uint32_t mask = get_collision_mask();
  67. if (p_value) {
  68. mask |= 1 << (p_layer_number - 1);
  69. } else {
  70. mask &= ~(1 << (p_layer_number - 1));
  71. }
  72. set_collision_mask(mask);
  73. }
  74. bool ShapeCast2D::get_collision_mask_value(int p_layer_number) const {
  75. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  76. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  77. return get_collision_mask() & (1 << (p_layer_number - 1));
  78. }
  79. int ShapeCast2D::get_collision_count() const {
  80. return result.size();
  81. }
  82. bool ShapeCast2D::is_colliding() const {
  83. return collided;
  84. }
  85. Object *ShapeCast2D::get_collider(int p_idx) const {
  86. ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), nullptr, "No collider found.");
  87. if (result[p_idx].collider_id.is_null()) {
  88. return nullptr;
  89. }
  90. return ObjectDB::get_instance(result[p_idx].collider_id);
  91. }
  92. RID ShapeCast2D::get_collider_rid(int p_idx) const {
  93. ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), RID(), "No collider RID found.");
  94. return result[p_idx].rid;
  95. }
  96. int ShapeCast2D::get_collider_shape(int p_idx) const {
  97. ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), -1, "No collider shape found.");
  98. return result[p_idx].shape;
  99. }
  100. Vector2 ShapeCast2D::get_collision_point(int p_idx) const {
  101. ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), Vector2(), "No collision point found.");
  102. return result[p_idx].point;
  103. }
  104. Vector2 ShapeCast2D::get_collision_normal(int p_idx) const {
  105. ERR_FAIL_INDEX_V_MSG(p_idx, result.size(), Vector2(), "No collision normal found.");
  106. return result[p_idx].normal;
  107. }
  108. real_t ShapeCast2D::get_closest_collision_safe_fraction() const {
  109. return collision_safe_fraction;
  110. }
  111. real_t ShapeCast2D::get_closest_collision_unsafe_fraction() const {
  112. return collision_unsafe_fraction;
  113. }
  114. void ShapeCast2D::set_enabled(bool p_enabled) {
  115. enabled = p_enabled;
  116. queue_redraw();
  117. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  118. set_physics_process_internal(p_enabled);
  119. }
  120. if (!p_enabled) {
  121. collided = false;
  122. }
  123. }
  124. bool ShapeCast2D::is_enabled() const {
  125. return enabled;
  126. }
  127. void ShapeCast2D::set_shape(const Ref<Shape2D> &p_shape) {
  128. if (p_shape == shape) {
  129. return;
  130. }
  131. if (shape.is_valid()) {
  132. shape->disconnect_changed(callable_mp(this, &ShapeCast2D::_shape_changed));
  133. }
  134. shape = p_shape;
  135. if (shape.is_valid()) {
  136. shape->connect_changed(callable_mp(this, &ShapeCast2D::_shape_changed));
  137. shape_rid = shape->get_rid();
  138. }
  139. update_configuration_warnings();
  140. queue_redraw();
  141. }
  142. Ref<Shape2D> ShapeCast2D::get_shape() const {
  143. return shape;
  144. }
  145. void ShapeCast2D::set_exclude_parent_body(bool p_exclude_parent_body) {
  146. if (exclude_parent_body == p_exclude_parent_body) {
  147. return;
  148. }
  149. exclude_parent_body = p_exclude_parent_body;
  150. if (!is_inside_tree()) {
  151. return;
  152. }
  153. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  154. if (exclude_parent_body) {
  155. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  156. } else {
  157. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  158. }
  159. }
  160. }
  161. bool ShapeCast2D::get_exclude_parent_body() const {
  162. return exclude_parent_body;
  163. }
  164. void ShapeCast2D::_shape_changed() {
  165. queue_redraw();
  166. }
  167. void ShapeCast2D::_notification(int p_what) {
  168. switch (p_what) {
  169. case NOTIFICATION_ENTER_TREE: {
  170. if (enabled && !Engine::get_singleton()->is_editor_hint()) {
  171. set_physics_process_internal(true);
  172. } else {
  173. set_physics_process_internal(false);
  174. }
  175. if (Object::cast_to<CollisionObject2D>(get_parent())) {
  176. if (exclude_parent_body) {
  177. exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  178. } else {
  179. exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
  180. }
  181. }
  182. } break;
  183. case NOTIFICATION_EXIT_TREE: {
  184. if (enabled) {
  185. set_physics_process_internal(false);
  186. }
  187. } break;
  188. case NOTIFICATION_DRAW: {
  189. #ifdef TOOLS_ENABLED
  190. ERR_FAIL_COND(!is_inside_tree());
  191. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  192. break;
  193. }
  194. if (shape.is_null()) {
  195. break;
  196. }
  197. Color draw_col = collided ? Color(1.0, 0.01, 0) : get_tree()->get_debug_collisions_color();
  198. if (!enabled) {
  199. float g = draw_col.get_v();
  200. draw_col.r = g;
  201. draw_col.g = g;
  202. draw_col.b = g;
  203. }
  204. // Draw continuous chain of shapes along the cast.
  205. const int steps = MAX(2, target_position.length() / shape->get_rect().get_size().length() * 4);
  206. for (int i = 0; i <= steps; ++i) {
  207. Vector2 t = (real_t(i) / steps) * target_position;
  208. draw_set_transform(t, 0.0, Size2(1, 1));
  209. shape->draw(get_canvas_item(), draw_col);
  210. }
  211. draw_set_transform(Vector2(), 0.0, Size2(1, 1));
  212. // Draw an arrow indicating where the ShapeCast is pointing to.
  213. if (target_position != Vector2()) {
  214. const real_t max_arrow_size = 6;
  215. const real_t line_width = 1.4;
  216. bool no_line = target_position.length() < line_width;
  217. real_t arrow_size = CLAMP(target_position.length() * 2 / 3, line_width, max_arrow_size);
  218. if (no_line) {
  219. arrow_size = target_position.length();
  220. } else {
  221. draw_line(Vector2(), target_position - target_position.normalized() * arrow_size, draw_col, line_width);
  222. }
  223. Transform2D xf;
  224. xf.rotate(target_position.angle());
  225. xf.translate_local(Vector2(no_line ? 0 : target_position.length() - arrow_size, 0));
  226. Vector<Vector2> pts = {
  227. xf.xform(Vector2(arrow_size, 0)),
  228. xf.xform(Vector2(0, 0.5 * arrow_size)),
  229. xf.xform(Vector2(0, -0.5 * arrow_size))
  230. };
  231. Vector<Color> cols = { draw_col, draw_col, draw_col };
  232. draw_primitive(pts, cols, Vector<Vector2>());
  233. }
  234. #endif
  235. } break;
  236. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  237. if (!enabled) {
  238. break;
  239. }
  240. _update_shapecast_state();
  241. } break;
  242. }
  243. }
  244. void ShapeCast2D::_update_shapecast_state() {
  245. result.clear();
  246. ERR_FAIL_COND_MSG(shape.is_null(), "Invalid shape.");
  247. Ref<World2D> w2d = get_world_2d();
  248. ERR_FAIL_COND(w2d.is_null());
  249. PhysicsDirectSpaceState2D *dss = PhysicsServer2D::get_singleton()->space_get_direct_state(w2d->get_space());
  250. ERR_FAIL_NULL(dss);
  251. Transform2D gt = get_global_transform();
  252. PhysicsDirectSpaceState2D::ShapeParameters params;
  253. params.shape_rid = shape_rid;
  254. params.transform = gt;
  255. params.motion = gt.basis_xform(target_position);
  256. params.margin = margin;
  257. params.exclude = exclude;
  258. params.collision_mask = collision_mask;
  259. params.collide_with_bodies = collide_with_bodies;
  260. params.collide_with_areas = collide_with_areas;
  261. collision_safe_fraction = 0.0;
  262. collision_unsafe_fraction = 0.0;
  263. bool prev_collision_state = collided;
  264. if (target_position != Vector2()) {
  265. dss->cast_motion(params, collision_safe_fraction, collision_unsafe_fraction);
  266. if (collision_unsafe_fraction < 1.0) {
  267. // Move shape transform to the point of impact,
  268. // so we can collect contact info at that point.
  269. gt.set_origin(gt.get_origin() + params.motion * (collision_unsafe_fraction + CMP_EPSILON));
  270. params.transform = gt;
  271. }
  272. }
  273. // Regardless of whether the shape is stuck or it's moved along
  274. // the motion vector, we'll only consider static collisions from now on.
  275. params.motion = Vector2();
  276. bool intersected = true;
  277. while (intersected && result.size() < max_results) {
  278. PhysicsDirectSpaceState2D::ShapeRestInfo info;
  279. intersected = dss->rest_info(params, &info);
  280. if (intersected) {
  281. result.push_back(info);
  282. params.exclude.insert(info.rid);
  283. }
  284. }
  285. collided = !result.is_empty();
  286. if (prev_collision_state != collided) {
  287. queue_redraw();
  288. }
  289. }
  290. void ShapeCast2D::force_shapecast_update() {
  291. _update_shapecast_state();
  292. }
  293. void ShapeCast2D::add_exception_rid(const RID &p_rid) {
  294. exclude.insert(p_rid);
  295. }
  296. void ShapeCast2D::add_exception(const CollisionObject2D *p_node) {
  297. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject2D.");
  298. add_exception_rid(p_node->get_rid());
  299. }
  300. void ShapeCast2D::remove_exception_rid(const RID &p_rid) {
  301. exclude.erase(p_rid);
  302. }
  303. void ShapeCast2D::remove_exception(const CollisionObject2D *p_node) {
  304. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject2D.");
  305. remove_exception_rid(p_node->get_rid());
  306. }
  307. void ShapeCast2D::clear_exceptions() {
  308. exclude.clear();
  309. }
  310. void ShapeCast2D::set_collide_with_areas(bool p_clip) {
  311. collide_with_areas = p_clip;
  312. }
  313. bool ShapeCast2D::is_collide_with_areas_enabled() const {
  314. return collide_with_areas;
  315. }
  316. void ShapeCast2D::set_collide_with_bodies(bool p_clip) {
  317. collide_with_bodies = p_clip;
  318. }
  319. bool ShapeCast2D::is_collide_with_bodies_enabled() const {
  320. return collide_with_bodies;
  321. }
  322. Array ShapeCast2D::_get_collision_result() const {
  323. Array ret;
  324. for (int i = 0; i < result.size(); ++i) {
  325. const PhysicsDirectSpaceState2D::ShapeRestInfo &sri = result[i];
  326. Dictionary col;
  327. col["point"] = sri.point;
  328. col["normal"] = sri.normal;
  329. col["rid"] = sri.rid;
  330. col["collider"] = ObjectDB::get_instance(sri.collider_id);
  331. col["collider_id"] = sri.collider_id;
  332. col["shape"] = sri.shape;
  333. col["linear_velocity"] = sri.linear_velocity;
  334. ret.push_back(col);
  335. }
  336. return ret;
  337. }
  338. PackedStringArray ShapeCast2D::get_configuration_warnings() const {
  339. PackedStringArray warnings = Node2D::get_configuration_warnings();
  340. if (shape.is_null()) {
  341. warnings.push_back(RTR("This node cannot interact with other objects unless a Shape2D is assigned."));
  342. }
  343. return warnings;
  344. }
  345. void ShapeCast2D::_bind_methods() {
  346. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &ShapeCast2D::set_enabled);
  347. ClassDB::bind_method(D_METHOD("is_enabled"), &ShapeCast2D::is_enabled);
  348. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &ShapeCast2D::set_shape);
  349. ClassDB::bind_method(D_METHOD("get_shape"), &ShapeCast2D::get_shape);
  350. ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &ShapeCast2D::set_target_position);
  351. ClassDB::bind_method(D_METHOD("get_target_position"), &ShapeCast2D::get_target_position);
  352. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &ShapeCast2D::set_margin);
  353. ClassDB::bind_method(D_METHOD("get_margin"), &ShapeCast2D::get_margin);
  354. ClassDB::bind_method(D_METHOD("set_max_results", "max_results"), &ShapeCast2D::set_max_results);
  355. ClassDB::bind_method(D_METHOD("get_max_results"), &ShapeCast2D::get_max_results);
  356. ClassDB::bind_method(D_METHOD("is_colliding"), &ShapeCast2D::is_colliding);
  357. ClassDB::bind_method(D_METHOD("get_collision_count"), &ShapeCast2D::get_collision_count);
  358. ClassDB::bind_method(D_METHOD("force_shapecast_update"), &ShapeCast2D::force_shapecast_update);
  359. ClassDB::bind_method(D_METHOD("get_collider", "index"), &ShapeCast2D::get_collider);
  360. ClassDB::bind_method(D_METHOD("get_collider_rid", "index"), &ShapeCast2D::get_collider_rid);
  361. ClassDB::bind_method(D_METHOD("get_collider_shape", "index"), &ShapeCast2D::get_collider_shape);
  362. ClassDB::bind_method(D_METHOD("get_collision_point", "index"), &ShapeCast2D::get_collision_point);
  363. ClassDB::bind_method(D_METHOD("get_collision_normal", "index"), &ShapeCast2D::get_collision_normal);
  364. ClassDB::bind_method(D_METHOD("get_closest_collision_safe_fraction"), &ShapeCast2D::get_closest_collision_safe_fraction);
  365. ClassDB::bind_method(D_METHOD("get_closest_collision_unsafe_fraction"), &ShapeCast2D::get_closest_collision_unsafe_fraction);
  366. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &ShapeCast2D::add_exception_rid);
  367. ClassDB::bind_method(D_METHOD("add_exception", "node"), &ShapeCast2D::add_exception);
  368. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &ShapeCast2D::remove_exception_rid);
  369. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &ShapeCast2D::remove_exception);
  370. ClassDB::bind_method(D_METHOD("clear_exceptions"), &ShapeCast2D::clear_exceptions);
  371. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &ShapeCast2D::set_collision_mask);
  372. ClassDB::bind_method(D_METHOD("get_collision_mask"), &ShapeCast2D::get_collision_mask);
  373. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &ShapeCast2D::set_collision_mask_value);
  374. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &ShapeCast2D::get_collision_mask_value);
  375. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &ShapeCast2D::set_exclude_parent_body);
  376. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &ShapeCast2D::get_exclude_parent_body);
  377. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &ShapeCast2D::set_collide_with_areas);
  378. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &ShapeCast2D::is_collide_with_areas_enabled);
  379. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &ShapeCast2D::set_collide_with_bodies);
  380. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &ShapeCast2D::is_collide_with_bodies_enabled);
  381. ClassDB::bind_method(D_METHOD("_get_collision_result"), &ShapeCast2D::_get_collision_result);
  382. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  383. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  384. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  385. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_position", PROPERTY_HINT_NONE, "suffix:px"), "set_target_position", "get_target_position");
  386. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01,suffix:px"), "set_margin", "get_margin");
  387. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_results"), "set_max_results", "get_max_results");
  388. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  389. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "collision_result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "", "_get_collision_result");
  390. ADD_GROUP("Collide With", "collide_with");
  391. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  392. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  393. }
  394. ShapeCast2D::ShapeCast2D() {
  395. set_hide_clip_children(true);
  396. }