collision_shape_2d.cpp 11 KB

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