collision_shape_2d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**************************************************************************/
  2. /* collision_shape_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 "collision_shape_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "scene/2d/area_2d.h"
  33. #include "scene/resources/concave_polygon_shape_2d.h"
  34. #include "scene/resources/convex_polygon_shape_2d.h"
  35. void CollisionShape2D::_shape_changed() {
  36. queue_redraw();
  37. }
  38. void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) {
  39. parent->shape_owner_set_transform(owner_id, get_transform());
  40. if (p_xform_only) {
  41. return;
  42. }
  43. parent->shape_owner_set_disabled(owner_id, disabled);
  44. parent->shape_owner_set_one_way_collision(owner_id, one_way_collision);
  45. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  46. }
  47. Color CollisionShape2D::_get_default_debug_color() const {
  48. SceneTree *st = SceneTree::get_singleton();
  49. return st ? st->get_debug_collisions_color() : Color();
  50. }
  51. void CollisionShape2D::_notification(int p_what) {
  52. switch (p_what) {
  53. case NOTIFICATION_PARENTED: {
  54. parent = Object::cast_to<CollisionObject2D>(get_parent());
  55. if (parent) {
  56. owner_id = parent->create_shape_owner(this);
  57. if (shape.is_valid()) {
  58. parent->shape_owner_add_shape(owner_id, shape);
  59. }
  60. _update_in_shape_owner();
  61. }
  62. } break;
  63. case NOTIFICATION_ENTER_TREE: {
  64. if (parent) {
  65. _update_in_shape_owner();
  66. }
  67. } break;
  68. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  69. if (parent) {
  70. _update_in_shape_owner(true);
  71. }
  72. } break;
  73. case NOTIFICATION_UNPARENTED: {
  74. if (parent) {
  75. parent->remove_shape_owner(owner_id);
  76. }
  77. owner_id = 0;
  78. parent = nullptr;
  79. } break;
  80. case NOTIFICATION_DRAW: {
  81. ERR_FAIL_COND(!is_inside_tree());
  82. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  83. break;
  84. }
  85. if (!shape.is_valid()) {
  86. break;
  87. }
  88. rect = Rect2();
  89. Color draw_col = debug_color;
  90. if (disabled) {
  91. float g = draw_col.get_v();
  92. draw_col.r = g;
  93. draw_col.g = g;
  94. draw_col.b = g;
  95. draw_col.a *= 0.5;
  96. }
  97. shape->draw(get_canvas_item(), draw_col);
  98. rect = shape->get_rect();
  99. rect = rect.grow(3);
  100. if (one_way_collision) {
  101. // Draw an arrow indicating the one-way collision direction
  102. draw_col = debug_color.inverted();
  103. if (disabled) {
  104. draw_col = draw_col.darkened(0.25);
  105. }
  106. Vector2 line_to(0, 20);
  107. draw_line(Vector2(), line_to, draw_col, 2);
  108. real_t tsize = 8;
  109. Vector<Vector2> pts{
  110. line_to + Vector2(0, tsize),
  111. line_to + Vector2(Math_SQRT12 * tsize, 0),
  112. line_to + Vector2(-Math_SQRT12 * tsize, 0)
  113. };
  114. Vector<Color> cols{ draw_col, draw_col, draw_col };
  115. draw_primitive(pts, cols, Vector<Vector2>());
  116. }
  117. } break;
  118. }
  119. }
  120. void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) {
  121. if (p_shape == shape) {
  122. return;
  123. }
  124. if (shape.is_valid()) {
  125. shape->disconnect("changed", callable_mp(this, &CollisionShape2D::_shape_changed));
  126. }
  127. shape = p_shape;
  128. queue_redraw();
  129. if (parent) {
  130. parent->shape_owner_clear_shapes(owner_id);
  131. if (shape.is_valid()) {
  132. parent->shape_owner_add_shape(owner_id, shape);
  133. }
  134. _update_in_shape_owner();
  135. }
  136. if (shape.is_valid()) {
  137. shape->connect("changed", callable_mp(this, &CollisionShape2D::_shape_changed));
  138. }
  139. update_configuration_warnings();
  140. }
  141. Ref<Shape2D> CollisionShape2D::get_shape() const {
  142. return shape;
  143. }
  144. bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  145. if (!shape.is_valid()) {
  146. return false;
  147. }
  148. return shape->_edit_is_selected_on_click(p_point, p_tolerance);
  149. }
  150. PackedStringArray CollisionShape2D::get_configuration_warnings() const {
  151. PackedStringArray warnings = Node::get_configuration_warnings();
  152. if (!Object::cast_to<CollisionObject2D>(get_parent())) {
  153. warnings.push_back(RTR("CollisionShape2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, CharacterBody2D, etc. to give them a shape."));
  154. }
  155. if (!shape.is_valid()) {
  156. warnings.push_back(RTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!"));
  157. }
  158. if (one_way_collision && Object::cast_to<Area2D>(get_parent())) {
  159. warnings.push_back(RTR("The One Way Collision property will be ignored when the parent is an Area2D."));
  160. }
  161. Ref<ConvexPolygonShape2D> convex = shape;
  162. Ref<ConcavePolygonShape2D> concave = shape;
  163. if (convex.is_valid() || concave.is_valid()) {
  164. warnings.push_back(RTR("Polygon-based shapes are not meant be used nor edited directly through the CollisionShape2D node. Please use the CollisionPolygon2D node instead."));
  165. }
  166. return warnings;
  167. }
  168. void CollisionShape2D::set_disabled(bool p_disabled) {
  169. disabled = p_disabled;
  170. queue_redraw();
  171. if (parent) {
  172. parent->shape_owner_set_disabled(owner_id, p_disabled);
  173. }
  174. }
  175. bool CollisionShape2D::is_disabled() const {
  176. return disabled;
  177. }
  178. void CollisionShape2D::set_one_way_collision(bool p_enable) {
  179. one_way_collision = p_enable;
  180. queue_redraw();
  181. if (parent) {
  182. parent->shape_owner_set_one_way_collision(owner_id, p_enable);
  183. }
  184. update_configuration_warnings();
  185. }
  186. bool CollisionShape2D::is_one_way_collision_enabled() const {
  187. return one_way_collision;
  188. }
  189. void CollisionShape2D::set_one_way_collision_margin(real_t p_margin) {
  190. one_way_collision_margin = p_margin;
  191. if (parent) {
  192. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  193. }
  194. }
  195. real_t CollisionShape2D::get_one_way_collision_margin() const {
  196. return one_way_collision_margin;
  197. }
  198. void CollisionShape2D::set_debug_color(const Color &p_color) {
  199. debug_color = p_color;
  200. queue_redraw();
  201. }
  202. Color CollisionShape2D::get_debug_color() const {
  203. return debug_color;
  204. }
  205. bool CollisionShape2D::_property_can_revert(const StringName &p_name) const {
  206. if (p_name == "debug_color") {
  207. return true;
  208. }
  209. return false;
  210. }
  211. bool CollisionShape2D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  212. if (p_name == "debug_color") {
  213. r_property = _get_default_debug_color();
  214. return true;
  215. }
  216. return false;
  217. }
  218. void CollisionShape2D::_validate_property(PropertyInfo &p_property) const {
  219. if (p_property.name == "debug_color") {
  220. if (debug_color == _get_default_debug_color()) {
  221. p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;
  222. } else {
  223. p_property.usage = PROPERTY_USAGE_DEFAULT;
  224. }
  225. }
  226. }
  227. void CollisionShape2D::_bind_methods() {
  228. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
  229. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
  230. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionShape2D::set_disabled);
  231. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape2D::is_disabled);
  232. ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionShape2D::set_one_way_collision);
  233. ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
  234. ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);
  235. ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);
  236. ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);
  237. ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);
  238. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  239. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  240. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
  241. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1,suffix:px"), "set_one_way_collision_margin", "get_one_way_collision_margin");
  242. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
  243. // Default value depends on a project setting, override for doc generation purposes.
  244. ADD_PROPERTY_DEFAULT("debug_color", Color());
  245. }
  246. CollisionShape2D::CollisionShape2D() {
  247. set_notify_local_transform(true);
  248. set_hide_clip_children(true);
  249. debug_color = _get_default_debug_color();
  250. }