collision_polygon_3d.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "core/math/geometry_2d.h"
  32. #include "scene/3d/physics/collision_object_3d.h"
  33. #include "scene/resources/3d/convex_polygon_shape_3d.h"
  34. void CollisionPolygon3D::_build_polygon() {
  35. if (!collision_object) {
  36. return;
  37. }
  38. collision_object->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. collision_object->shape_owner_add_shape(owner_id, convex);
  65. collision_object->shape_owner_set_disabled(owner_id, disabled);
  66. }
  67. }
  68. void CollisionPolygon3D::_update_in_shape_owner(bool p_xform_only) {
  69. collision_object->shape_owner_set_transform(owner_id, get_transform());
  70. if (p_xform_only) {
  71. return;
  72. }
  73. collision_object->shape_owner_set_disabled(owner_id, disabled);
  74. }
  75. void CollisionPolygon3D::_notification(int p_what) {
  76. switch (p_what) {
  77. case NOTIFICATION_PARENTED: {
  78. collision_object = Object::cast_to<CollisionObject3D>(get_parent());
  79. if (collision_object) {
  80. owner_id = collision_object->create_shape_owner(this);
  81. _build_polygon();
  82. _update_in_shape_owner();
  83. }
  84. } break;
  85. case NOTIFICATION_ENTER_TREE: {
  86. if (collision_object) {
  87. _update_in_shape_owner();
  88. }
  89. } break;
  90. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  91. if (collision_object) {
  92. _update_in_shape_owner(true);
  93. }
  94. update_configuration_warnings();
  95. } break;
  96. case NOTIFICATION_UNPARENTED: {
  97. if (collision_object) {
  98. collision_object->remove_shape_owner(owner_id);
  99. }
  100. owner_id = 0;
  101. collision_object = nullptr;
  102. } break;
  103. }
  104. }
  105. void CollisionPolygon3D::set_polygon(const Vector<Point2> &p_polygon) {
  106. polygon = p_polygon;
  107. if (collision_object) {
  108. _build_polygon();
  109. }
  110. update_configuration_warnings();
  111. update_gizmos();
  112. }
  113. Vector<Point2> CollisionPolygon3D::get_polygon() const {
  114. return polygon;
  115. }
  116. AABB CollisionPolygon3D::get_item_rect() const {
  117. return aabb;
  118. }
  119. void CollisionPolygon3D::set_depth(real_t p_depth) {
  120. depth = p_depth;
  121. _build_polygon();
  122. update_gizmos();
  123. }
  124. real_t CollisionPolygon3D::get_depth() const {
  125. return depth;
  126. }
  127. void CollisionPolygon3D::set_disabled(bool p_disabled) {
  128. disabled = p_disabled;
  129. update_gizmos();
  130. if (collision_object) {
  131. collision_object->shape_owner_set_disabled(owner_id, p_disabled);
  132. }
  133. }
  134. bool CollisionPolygon3D::is_disabled() const {
  135. return disabled;
  136. }
  137. real_t CollisionPolygon3D::get_margin() const {
  138. return margin;
  139. }
  140. void CollisionPolygon3D::set_margin(real_t p_margin) {
  141. margin = p_margin;
  142. if (collision_object) {
  143. _build_polygon();
  144. }
  145. }
  146. PackedStringArray CollisionPolygon3D::get_configuration_warnings() const {
  147. PackedStringArray warnings = Node3D::get_configuration_warnings();
  148. if (!Object::cast_to<CollisionObject3D>(get_parent())) {
  149. 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."));
  150. }
  151. if (polygon.is_empty()) {
  152. warnings.push_back(RTR("An empty CollisionPolygon3D has no effect on collision."));
  153. }
  154. Vector3 scale = get_transform().get_basis().get_scale();
  155. if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
  156. 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."));
  157. }
  158. return warnings;
  159. }
  160. bool CollisionPolygon3D::_is_editable_3d_polygon() const {
  161. return true;
  162. }
  163. void CollisionPolygon3D::_bind_methods() {
  164. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CollisionPolygon3D::set_depth);
  165. ClassDB::bind_method(D_METHOD("get_depth"), &CollisionPolygon3D::get_depth);
  166. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon3D::set_polygon);
  167. ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon3D::get_polygon);
  168. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon3D::set_disabled);
  169. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon3D::is_disabled);
  170. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &CollisionPolygon3D::set_margin);
  171. ClassDB::bind_method(D_METHOD("get_margin"), &CollisionPolygon3D::get_margin);
  172. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CollisionPolygon3D::_is_editable_3d_polygon);
  173. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_NONE, "suffix:m"), "set_depth", "get_depth");
  174. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  175. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  176. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0.001,10,0.001,suffix:m"), "set_margin", "get_margin");
  177. }
  178. CollisionPolygon3D::CollisionPolygon3D() {
  179. set_notify_local_transform(true);
  180. }