collision_shape_2d.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*************************************************************************/
  2. /* collision_shape_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "collision_shape_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "scene/resources/concave_polygon_shape_2d.h"
  33. #include "scene/resources/convex_polygon_shape_2d.h"
  34. void CollisionShape2D::_shape_changed() {
  35. update();
  36. }
  37. void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) {
  38. parent->shape_owner_set_transform(owner_id, get_transform());
  39. if (p_xform_only) {
  40. return;
  41. }
  42. parent->shape_owner_set_disabled(owner_id, disabled);
  43. parent->shape_owner_set_one_way_collision(owner_id, one_way_collision);
  44. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  45. }
  46. void CollisionShape2D::_notification(int p_what) {
  47. switch (p_what) {
  48. case NOTIFICATION_PARENTED: {
  49. parent = Object::cast_to<CollisionObject2D>(get_parent());
  50. if (parent) {
  51. owner_id = parent->create_shape_owner(this);
  52. if (shape.is_valid()) {
  53. parent->shape_owner_add_shape(owner_id, shape);
  54. }
  55. _update_in_shape_owner();
  56. }
  57. /*if (Engine::get_singleton()->is_editor_hint()) {
  58. //display above all else
  59. set_z_as_relative(false);
  60. set_z_index(RS::CANVAS_ITEM_Z_MAX - 1);
  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 = get_tree()->get_debug_collisions_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 = get_tree()->get_debug_collisions_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. Vector<Vector2> pts;
  109. real_t tsize = 8;
  110. pts.push_back(line_to + (Vector2(0, tsize)));
  111. pts.push_back(line_to + (Vector2(Math_SQRT12 * tsize, 0)));
  112. pts.push_back(line_to + (Vector2(-Math_SQRT12 * tsize, 0)));
  113. Vector<Color> cols;
  114. for (int i = 0; i < 3; i++) {
  115. cols.push_back(draw_col);
  116. }
  117. draw_primitive(pts, cols, Vector<Vector2>());
  118. }
  119. } break;
  120. }
  121. }
  122. void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) {
  123. if (p_shape == shape) {
  124. return;
  125. }
  126. if (shape.is_valid()) {
  127. shape->disconnect("changed", callable_mp(this, &CollisionShape2D::_shape_changed));
  128. }
  129. shape = p_shape;
  130. update();
  131. if (parent) {
  132. parent->shape_owner_clear_shapes(owner_id);
  133. if (shape.is_valid()) {
  134. parent->shape_owner_add_shape(owner_id, shape);
  135. }
  136. _update_in_shape_owner();
  137. }
  138. if (shape.is_valid()) {
  139. shape->connect("changed", callable_mp(this, &CollisionShape2D::_shape_changed));
  140. }
  141. update_configuration_warnings();
  142. }
  143. Ref<Shape2D> CollisionShape2D::get_shape() const {
  144. return shape;
  145. }
  146. bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  147. if (!shape.is_valid()) {
  148. return false;
  149. }
  150. return shape->_edit_is_selected_on_click(p_point, p_tolerance);
  151. }
  152. TypedArray<String> CollisionShape2D::get_configuration_warnings() const {
  153. TypedArray<String> warnings = Node::get_configuration_warnings();
  154. if (!Object::cast_to<CollisionObject2D>(get_parent())) {
  155. warnings.push_back(TTR("CollisionShape2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidDynamicBody2D, CharacterBody2D, etc. to give them a shape."));
  156. }
  157. if (!shape.is_valid()) {
  158. warnings.push_back(TTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!"));
  159. }
  160. Ref<ConvexPolygonShape2D> convex = shape;
  161. Ref<ConcavePolygonShape2D> concave = shape;
  162. if (convex.is_valid() || concave.is_valid()) {
  163. warnings.push_back(TTR("Polygon-based shapes are not meant be used nor edited directly through the CollisionShape2D node. Please use the CollisionPolygon2D node instead."));
  164. }
  165. return warnings;
  166. }
  167. void CollisionShape2D::set_disabled(bool p_disabled) {
  168. disabled = p_disabled;
  169. update();
  170. if (parent) {
  171. parent->shape_owner_set_disabled(owner_id, p_disabled);
  172. }
  173. }
  174. bool CollisionShape2D::is_disabled() const {
  175. return disabled;
  176. }
  177. void CollisionShape2D::set_one_way_collision(bool p_enable) {
  178. one_way_collision = p_enable;
  179. update();
  180. if (parent) {
  181. parent->shape_owner_set_one_way_collision(owner_id, p_enable);
  182. }
  183. }
  184. bool CollisionShape2D::is_one_way_collision_enabled() const {
  185. return one_way_collision;
  186. }
  187. void CollisionShape2D::set_one_way_collision_margin(real_t p_margin) {
  188. one_way_collision_margin = p_margin;
  189. if (parent) {
  190. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  191. }
  192. }
  193. real_t CollisionShape2D::get_one_way_collision_margin() const {
  194. return one_way_collision_margin;
  195. }
  196. void CollisionShape2D::_bind_methods() {
  197. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
  198. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
  199. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionShape2D::set_disabled);
  200. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape2D::is_disabled);
  201. ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionShape2D::set_one_way_collision);
  202. ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
  203. ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);
  204. ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);
  205. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  206. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  207. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
  208. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1"), "set_one_way_collision_margin", "get_one_way_collision_margin");
  209. }
  210. CollisionShape2D::CollisionShape2D() {
  211. set_notify_local_transform(true);
  212. }