shape_cast_2d.cpp 17 KB

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