2
0

collision_polygon_3d.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**************************************************************************/
  2. /* collision_polygon_3d.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_3d.h"
  31. #include "collision_object_3d.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "scene/resources/convex_polygon_shape_3d.h"
  34. void CollisionPolygon3D::_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 = Geometry2D::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<ConvexPolygonShape3D> convex = memnew(ConvexPolygonShape3D);
  50. Vector<Vector3> cp;
  51. int cs = decomp[i].size();
  52. cp.resize(cs * 2);
  53. {
  54. Vector3 *w = cp.ptrw();
  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 CollisionPolygon3D::_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 CollisionPolygon3D::_notification(int p_what) {
  76. switch (p_what) {
  77. case NOTIFICATION_PARENTED: {
  78. parent = Object::cast_to<CollisionObject3D>(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. #ifdef TOOLS_ENABLED
  95. if (Engine::get_singleton()->is_editor_hint()) {
  96. update_configuration_warnings();
  97. }
  98. #endif
  99. } break;
  100. case NOTIFICATION_UNPARENTED: {
  101. if (parent) {
  102. parent->remove_shape_owner(owner_id);
  103. }
  104. owner_id = 0;
  105. parent = nullptr;
  106. } break;
  107. }
  108. }
  109. void CollisionPolygon3D::set_polygon(const Vector<Point2> &p_polygon) {
  110. polygon = p_polygon;
  111. if (parent) {
  112. _build_polygon();
  113. }
  114. update_configuration_warnings();
  115. update_gizmos();
  116. }
  117. Vector<Point2> CollisionPolygon3D::get_polygon() const {
  118. return polygon;
  119. }
  120. AABB CollisionPolygon3D::get_item_rect() const {
  121. return aabb;
  122. }
  123. void CollisionPolygon3D::set_depth(real_t p_depth) {
  124. depth = p_depth;
  125. _build_polygon();
  126. update_gizmos();
  127. }
  128. real_t CollisionPolygon3D::get_depth() const {
  129. return depth;
  130. }
  131. void CollisionPolygon3D::set_disabled(bool p_disabled) {
  132. disabled = p_disabled;
  133. update_gizmos();
  134. if (parent) {
  135. parent->shape_owner_set_disabled(owner_id, p_disabled);
  136. }
  137. }
  138. bool CollisionPolygon3D::is_disabled() const {
  139. return disabled;
  140. }
  141. real_t CollisionPolygon3D::get_margin() const {
  142. return margin;
  143. }
  144. void CollisionPolygon3D::set_margin(real_t p_margin) {
  145. margin = p_margin;
  146. if (parent) {
  147. _build_polygon();
  148. }
  149. }
  150. PackedStringArray CollisionPolygon3D::get_configuration_warnings() const {
  151. PackedStringArray warnings = Node::get_configuration_warnings();
  152. if (!Object::cast_to<CollisionObject3D>(get_parent())) {
  153. warnings.push_back(RTR("CollisionPolygon3D only serves to provide a collision shape to a CollisionObject3D derived node.\nPlease only use it as a child of Area3D, StaticBody3D, RigidBody3D, CharacterBody3D, etc. to give them a shape."));
  154. }
  155. if (polygon.is_empty()) {
  156. warnings.push_back(RTR("An empty CollisionPolygon3D has no effect on collision."));
  157. }
  158. Vector3 scale = get_transform().get_basis().get_scale();
  159. if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
  160. warnings.push_back(RTR("A non-uniformly scaled CollisionPolygon3D node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change its polygon's vertices instead."));
  161. }
  162. return warnings;
  163. }
  164. bool CollisionPolygon3D::_is_editable_3d_polygon() const {
  165. return true;
  166. }
  167. void CollisionPolygon3D::_bind_methods() {
  168. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CollisionPolygon3D::set_depth);
  169. ClassDB::bind_method(D_METHOD("get_depth"), &CollisionPolygon3D::get_depth);
  170. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon3D::set_polygon);
  171. ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon3D::get_polygon);
  172. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon3D::set_disabled);
  173. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon3D::is_disabled);
  174. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &CollisionPolygon3D::set_margin);
  175. ClassDB::bind_method(D_METHOD("get_margin"), &CollisionPolygon3D::get_margin);
  176. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CollisionPolygon3D::_is_editable_3d_polygon);
  177. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_NONE, "suffix:m"), "set_depth", "get_depth");
  178. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  179. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  180. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0.001,10,0.001,suffix:m"), "set_margin", "get_margin");
  181. }
  182. CollisionPolygon3D::CollisionPolygon3D() {
  183. set_notify_local_transform(true);
  184. }