collision_shape_3d.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**************************************************************************/
  2. /* collision_shape_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_shape_3d.h"
  31. #include "mesh_instance_3d.h"
  32. #include "physics_body_3d.h"
  33. #include "scene/resources/concave_polygon_shape_3d.h"
  34. #include "scene/resources/convex_polygon_shape_3d.h"
  35. #include "scene/resources/world_boundary_shape_3d.h"
  36. void CollisionShape3D::make_convex_from_siblings() {
  37. Node *p = get_parent();
  38. if (!p) {
  39. return;
  40. }
  41. Vector<Vector3> vertices;
  42. for (int i = 0; i < p->get_child_count(); i++) {
  43. Node *n = p->get_child(i);
  44. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(n);
  45. if (mi) {
  46. Ref<Mesh> m = mi->get_mesh();
  47. if (m.is_valid()) {
  48. for (int j = 0; j < m->get_surface_count(); j++) {
  49. Array a = m->surface_get_arrays(j);
  50. if (!a.is_empty()) {
  51. Vector<Vector3> v = a[RenderingServer::ARRAY_VERTEX];
  52. for (int k = 0; k < v.size(); k++) {
  53. vertices.append(mi->get_transform().xform(v[k]));
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. Ref<ConvexPolygonShape3D> shape_new = memnew(ConvexPolygonShape3D);
  61. shape_new->set_points(vertices);
  62. set_shape(shape_new);
  63. }
  64. void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
  65. collision_object->shape_owner_set_transform(owner_id, get_transform());
  66. if (p_xform_only) {
  67. return;
  68. }
  69. collision_object->shape_owner_set_disabled(owner_id, disabled);
  70. }
  71. void CollisionShape3D::_notification(int p_what) {
  72. switch (p_what) {
  73. case NOTIFICATION_PARENTED: {
  74. collision_object = Object::cast_to<CollisionObject3D>(get_parent());
  75. if (collision_object) {
  76. owner_id = collision_object->create_shape_owner(this);
  77. if (shape.is_valid()) {
  78. collision_object->shape_owner_add_shape(owner_id, shape);
  79. }
  80. _update_in_shape_owner();
  81. }
  82. } break;
  83. case NOTIFICATION_ENTER_TREE: {
  84. if (collision_object) {
  85. _update_in_shape_owner();
  86. }
  87. } break;
  88. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  89. if (collision_object) {
  90. _update_in_shape_owner(true);
  91. }
  92. update_configuration_warnings();
  93. } break;
  94. case NOTIFICATION_UNPARENTED: {
  95. if (collision_object) {
  96. collision_object->remove_shape_owner(owner_id);
  97. }
  98. owner_id = 0;
  99. collision_object = nullptr;
  100. } break;
  101. }
  102. }
  103. #ifndef DISABLE_DEPRECATED
  104. void CollisionShape3D::resource_changed(Ref<Resource> res) {
  105. }
  106. #endif
  107. PackedStringArray CollisionShape3D::get_configuration_warnings() const {
  108. PackedStringArray warnings = Node::get_configuration_warnings();
  109. CollisionObject3D *col_object = Object::cast_to<CollisionObject3D>(get_parent());
  110. if (col_object == nullptr) {
  111. warnings.push_back(RTR("CollisionShape3D 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."));
  112. }
  113. if (!shape.is_valid()) {
  114. warnings.push_back(RTR("A shape must be provided for CollisionShape3D to function. Please create a shape resource for it."));
  115. }
  116. if (shape.is_valid() && Object::cast_to<RigidBody3D>(col_object)) {
  117. if (Object::cast_to<ConcavePolygonShape3D>(*shape)) {
  118. warnings.push_back(RTR("ConcavePolygonShape3D doesn't support RigidBody3D in another mode than static."));
  119. } else if (Object::cast_to<WorldBoundaryShape3D>(*shape)) {
  120. warnings.push_back(RTR("WorldBoundaryShape3D doesn't support RigidBody3D in another mode than static."));
  121. }
  122. }
  123. Vector3 scale = get_transform().get_basis().get_scale();
  124. if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
  125. warnings.push_back(RTR("A non-uniformly scaled CollisionShape3D node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change the size of its shape resource instead."));
  126. }
  127. return warnings;
  128. }
  129. void CollisionShape3D::_bind_methods() {
  130. #ifndef DISABLE_DEPRECATED
  131. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape3D::resource_changed);
  132. #endif
  133. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape3D::set_shape);
  134. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape3D::get_shape);
  135. ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape3D::set_disabled);
  136. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape3D::is_disabled);
  137. ClassDB::bind_method(D_METHOD("make_convex_from_siblings"), &CollisionShape3D::make_convex_from_siblings);
  138. ClassDB::set_method_flags("CollisionShape3D", "make_convex_from_siblings", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  139. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
  140. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  141. }
  142. void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
  143. if (p_shape == shape) {
  144. return;
  145. }
  146. if (shape.is_valid()) {
  147. shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
  148. }
  149. shape = p_shape;
  150. if (shape.is_valid()) {
  151. shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
  152. }
  153. update_gizmos();
  154. if (collision_object) {
  155. collision_object->shape_owner_clear_shapes(owner_id);
  156. if (shape.is_valid()) {
  157. collision_object->shape_owner_add_shape(owner_id, shape);
  158. }
  159. }
  160. if (is_inside_tree() && collision_object) {
  161. // If this is a heightfield shape our center may have changed
  162. _update_in_shape_owner(true);
  163. }
  164. update_configuration_warnings();
  165. }
  166. Ref<Shape3D> CollisionShape3D::get_shape() const {
  167. return shape;
  168. }
  169. void CollisionShape3D::set_disabled(bool p_disabled) {
  170. disabled = p_disabled;
  171. update_gizmos();
  172. if (collision_object) {
  173. collision_object->shape_owner_set_disabled(owner_id, p_disabled);
  174. }
  175. }
  176. bool CollisionShape3D::is_disabled() const {
  177. return disabled;
  178. }
  179. CollisionShape3D::CollisionShape3D() {
  180. //indicator = RenderingServer::get_singleton()->mesh_create();
  181. set_notify_local_transform(true);
  182. }
  183. CollisionShape3D::~CollisionShape3D() {
  184. //RenderingServer::get_singleton()->free(indicator);
  185. }