collision_polygon.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*************************************************************************/
  2. /* collision_polygon.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_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. parent->shape_owner_clear_shapes(owner_id);
  38. if (polygon.size() == 0)
  39. return;
  40. Vector<Vector<Vector2> > decomp = Geometry::decompose_polygon(polygon);
  41. if (decomp.size() == 0)
  42. return;
  43. //here comes the sun, lalalala
  44. //decompose concave into multiple convex polygons and add them
  45. for (int i = 0; i < decomp.size(); i++) {
  46. Ref<ConvexPolygonShape> convex = memnew(ConvexPolygonShape);
  47. PoolVector<Vector3> cp;
  48. int cs = decomp[i].size();
  49. cp.resize(cs * 2);
  50. {
  51. PoolVector<Vector3>::Write w = cp.write();
  52. int idx = 0;
  53. for (int j = 0; j < cs; j++) {
  54. Vector2 d = decomp[i][j];
  55. w[idx++] = Vector3(d.x, d.y, depth * 0.5);
  56. w[idx++] = Vector3(d.x, d.y, -depth * 0.5);
  57. }
  58. }
  59. convex->set_points(cp);
  60. parent->shape_owner_add_shape(owner_id, convex);
  61. parent->shape_owner_set_disabled(owner_id, disabled);
  62. }
  63. }
  64. void CollisionPolygon::_update_in_shape_owner(bool p_xform_only) {
  65. parent->shape_owner_set_transform(owner_id, get_transform());
  66. if (p_xform_only)
  67. return;
  68. parent->shape_owner_set_disabled(owner_id, disabled);
  69. }
  70. void CollisionPolygon::_notification(int p_what) {
  71. switch (p_what) {
  72. case NOTIFICATION_PARENTED: {
  73. parent = Object::cast_to<CollisionObject>(get_parent());
  74. if (parent) {
  75. owner_id = parent->create_shape_owner(this);
  76. _build_polygon();
  77. _update_in_shape_owner();
  78. }
  79. } break;
  80. case NOTIFICATION_ENTER_TREE: {
  81. if (parent) {
  82. _update_in_shape_owner();
  83. }
  84. } break;
  85. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  86. if (parent) {
  87. _update_in_shape_owner(true);
  88. }
  89. } break;
  90. case NOTIFICATION_UNPARENTED: {
  91. if (parent) {
  92. parent->remove_shape_owner(owner_id);
  93. }
  94. owner_id = 0;
  95. parent = NULL;
  96. } break;
  97. }
  98. }
  99. void CollisionPolygon::set_polygon(const Vector<Point2> &p_polygon) {
  100. polygon = p_polygon;
  101. if (parent) {
  102. _build_polygon();
  103. }
  104. update_configuration_warning();
  105. update_gizmo();
  106. }
  107. Vector<Point2> CollisionPolygon::get_polygon() const {
  108. return polygon;
  109. }
  110. AABB CollisionPolygon::get_item_rect() const {
  111. return aabb;
  112. }
  113. void CollisionPolygon::set_depth(float p_depth) {
  114. depth = p_depth;
  115. _build_polygon();
  116. update_gizmo();
  117. }
  118. float CollisionPolygon::get_depth() const {
  119. return depth;
  120. }
  121. void CollisionPolygon::set_disabled(bool p_disabled) {
  122. disabled = p_disabled;
  123. if (parent) {
  124. parent->shape_owner_set_disabled(owner_id, p_disabled);
  125. }
  126. }
  127. bool CollisionPolygon::is_disabled() const {
  128. return disabled;
  129. }
  130. String CollisionPolygon::get_configuration_warning() const {
  131. if (!Object::cast_to<CollisionObject>(get_parent())) {
  132. return 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.");
  133. }
  134. if (polygon.empty()) {
  135. return TTR("An empty CollisionPolygon has no effect on collision.");
  136. }
  137. return String();
  138. }
  139. bool CollisionPolygon::_is_editable_3d_polygon() const {
  140. return true;
  141. }
  142. void CollisionPolygon::_bind_methods() {
  143. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CollisionPolygon::set_depth);
  144. ClassDB::bind_method(D_METHOD("get_depth"), &CollisionPolygon::get_depth);
  145. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon::set_polygon);
  146. ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon::get_polygon);
  147. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon::set_disabled);
  148. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon::is_disabled);
  149. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CollisionPolygon::_is_editable_3d_polygon);
  150. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth"), "set_depth", "get_depth");
  151. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  152. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  153. }
  154. CollisionPolygon::CollisionPolygon() {
  155. aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  156. depth = 1.0;
  157. set_notify_local_transform(true);
  158. parent = NULL;
  159. owner_id = 0;
  160. disabled = false;
  161. }