collision_polygon.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**************************************************************************/
  2. /* collision_polygon.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_polygon.h"
  31. #include "collision_object.h"
  32. #include "scene/resources/concave_polygon_shape.h"
  33. #include "scene/resources/convex_polygon_shape.h"
  34. void CollisionPolygon::_build_polygon() {
  35. if (!parent) {
  36. return;
  37. }
  38. parent->shape_owner_clear_shapes(owner_id);
  39. if (polygon.size() == 0) {
  40. return;
  41. }
  42. Vector<Vector<Vector2>> decomp = Geometry::decompose_polygon_in_convex(polygon);
  43. if (decomp.size() == 0) {
  44. return;
  45. }
  46. //here comes the sun, lalalala
  47. //decompose concave into multiple convex polygons and add them
  48. for (int i = 0; i < decomp.size(); i++) {
  49. Ref<ConvexPolygonShape> convex = memnew(ConvexPolygonShape);
  50. PoolVector<Vector3> cp;
  51. int cs = decomp[i].size();
  52. cp.resize(cs * 2);
  53. {
  54. PoolVector<Vector3>::Write w = cp.write();
  55. int idx = 0;
  56. for (int j = 0; j < cs; j++) {
  57. Vector2 d = decomp[i][j];
  58. w[idx++] = Vector3(d.x, d.y, depth * 0.5);
  59. w[idx++] = Vector3(d.x, d.y, -depth * 0.5);
  60. }
  61. }
  62. convex->set_points(cp);
  63. convex->set_margin(margin);
  64. parent->shape_owner_add_shape(owner_id, convex);
  65. parent->shape_owner_set_disabled(owner_id, disabled);
  66. }
  67. }
  68. void CollisionPolygon::_update_in_shape_owner(bool p_xform_only) {
  69. parent->shape_owner_set_transform(owner_id, get_transform());
  70. if (p_xform_only) {
  71. return;
  72. }
  73. parent->shape_owner_set_disabled(owner_id, disabled);
  74. }
  75. void CollisionPolygon::_notification(int p_what) {
  76. switch (p_what) {
  77. case NOTIFICATION_PARENTED: {
  78. parent = Object::cast_to<CollisionObject>(get_parent());
  79. if (parent) {
  80. owner_id = parent->create_shape_owner(this);
  81. _build_polygon();
  82. _update_in_shape_owner();
  83. }
  84. } break;
  85. case NOTIFICATION_ENTER_TREE: {
  86. if (parent) {
  87. _update_in_shape_owner();
  88. }
  89. } break;
  90. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  91. if (parent) {
  92. _update_in_shape_owner(true);
  93. }
  94. } break;
  95. case NOTIFICATION_UNPARENTED: {
  96. if (parent) {
  97. parent->remove_shape_owner(owner_id);
  98. }
  99. owner_id = 0;
  100. parent = nullptr;
  101. } break;
  102. }
  103. }
  104. void CollisionPolygon::set_polygon(const Vector<Point2> &p_polygon) {
  105. polygon = p_polygon;
  106. if (parent) {
  107. _build_polygon();
  108. }
  109. update_configuration_warning();
  110. update_gizmo();
  111. }
  112. Vector<Point2> CollisionPolygon::get_polygon() const {
  113. return polygon;
  114. }
  115. AABB CollisionPolygon::get_item_rect() const {
  116. return aabb;
  117. }
  118. void CollisionPolygon::set_depth(float p_depth) {
  119. depth = p_depth;
  120. _build_polygon();
  121. update_gizmo();
  122. }
  123. float CollisionPolygon::get_depth() const {
  124. return depth;
  125. }
  126. void CollisionPolygon::set_disabled(bool p_disabled) {
  127. disabled = p_disabled;
  128. update_gizmo();
  129. if (parent) {
  130. parent->shape_owner_set_disabled(owner_id, p_disabled);
  131. }
  132. }
  133. bool CollisionPolygon::is_disabled() const {
  134. return disabled;
  135. }
  136. real_t CollisionPolygon::get_margin() const {
  137. return margin;
  138. }
  139. void CollisionPolygon::set_margin(real_t p_margin) {
  140. margin = p_margin;
  141. if (parent) {
  142. _build_polygon();
  143. }
  144. }
  145. String CollisionPolygon::get_configuration_warning() const {
  146. String warning = Spatial::get_configuration_warning();
  147. if (!Object::cast_to<CollisionObject>(get_parent())) {
  148. if (warning != String()) {
  149. warning += "\n\n";
  150. }
  151. warning += TTR("CollisionPolygon only serves to provide a collision shape to a CollisionObject derived node. Please only use it as a child of Area, StaticBody, RigidBody, KinematicBody, etc. to give them a shape.");
  152. }
  153. if (polygon.empty()) {
  154. if (warning != String()) {
  155. warning += "\n\n";
  156. }
  157. warning += TTR("An empty CollisionPolygon has no effect on collision.");
  158. }
  159. return warning;
  160. }
  161. bool CollisionPolygon::_is_editable_3d_polygon() const {
  162. return true;
  163. }
  164. void CollisionPolygon::_bind_methods() {
  165. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CollisionPolygon::set_depth);
  166. ClassDB::bind_method(D_METHOD("get_depth"), &CollisionPolygon::get_depth);
  167. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon::set_polygon);
  168. ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon::get_polygon);
  169. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon::set_disabled);
  170. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon::is_disabled);
  171. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &CollisionPolygon::set_margin);
  172. ClassDB::bind_method(D_METHOD("get_margin"), &CollisionPolygon::get_margin);
  173. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CollisionPolygon::_is_editable_3d_polygon);
  174. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth"), "set_depth", "get_depth");
  175. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  176. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  177. ADD_PROPERTY(PropertyInfo(Variant::REAL, "margin", PROPERTY_HINT_RANGE, "0.001,10,0.001"), "set_margin", "get_margin");
  178. }
  179. CollisionPolygon::CollisionPolygon() {
  180. aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  181. depth = 1.0;
  182. set_notify_local_transform(true);
  183. parent = nullptr;
  184. owner_id = 0;
  185. disabled = false;
  186. }