collision_shape_3d.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "scene/3d/mesh_instance_3d.h"
  32. #include "scene/3d/physics/character_body_3d.h"
  33. #include "scene/3d/physics/physics_body_3d.h"
  34. #include "scene/3d/physics/vehicle_body_3d.h"
  35. #include "scene/resources/3d/concave_polygon_shape_3d.h"
  36. #include "scene/resources/3d/convex_polygon_shape_3d.h"
  37. #include "scene/resources/3d/world_boundary_shape_3d.h"
  38. void CollisionShape3D::make_convex_from_siblings() {
  39. Node *p = get_parent();
  40. if (!p) {
  41. return;
  42. }
  43. Vector<Vector3> vertices;
  44. for (int i = 0; i < p->get_child_count(); i++) {
  45. Node *n = p->get_child(i);
  46. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(n);
  47. if (mi) {
  48. Ref<Mesh> m = mi->get_mesh();
  49. if (m.is_valid()) {
  50. for (int j = 0; j < m->get_surface_count(); j++) {
  51. Array a = m->surface_get_arrays(j);
  52. if (!a.is_empty()) {
  53. Vector<Vector3> v = a[RenderingServer::ARRAY_VERTEX];
  54. for (int k = 0; k < v.size(); k++) {
  55. vertices.append(mi->get_transform().xform(v[k]));
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. Ref<ConvexPolygonShape3D> shape_new = memnew(ConvexPolygonShape3D);
  63. shape_new->set_points(vertices);
  64. set_shape(shape_new);
  65. }
  66. void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
  67. collision_object->shape_owner_set_transform(owner_id, get_transform());
  68. if (p_xform_only) {
  69. return;
  70. }
  71. collision_object->shape_owner_set_disabled(owner_id, disabled);
  72. }
  73. void CollisionShape3D::_notification(int p_what) {
  74. switch (p_what) {
  75. case NOTIFICATION_PARENTED: {
  76. collision_object = Object::cast_to<CollisionObject3D>(get_parent());
  77. if (collision_object) {
  78. owner_id = collision_object->create_shape_owner(this);
  79. if (shape.is_valid()) {
  80. collision_object->shape_owner_add_shape(owner_id, shape);
  81. }
  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. #ifndef DISABLE_DEPRECATED
  106. void CollisionShape3D::resource_changed(Ref<Resource> res) {
  107. }
  108. #endif
  109. PackedStringArray CollisionShape3D::get_configuration_warnings() const {
  110. PackedStringArray warnings = Node3D::get_configuration_warnings();
  111. CollisionObject3D *col_object = Object::cast_to<CollisionObject3D>(get_parent());
  112. if (col_object == nullptr) {
  113. 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."));
  114. }
  115. if (!shape.is_valid()) {
  116. warnings.push_back(RTR("A shape must be provided for CollisionShape3D to function. Please create a shape resource for it."));
  117. }
  118. if (shape.is_valid() && Object::cast_to<RigidBody3D>(col_object)) {
  119. String body_type = "RigidBody3D";
  120. if (Object::cast_to<VehicleBody3D>(col_object)) {
  121. body_type = "VehicleBody3D";
  122. }
  123. if (Object::cast_to<ConcavePolygonShape3D>(*shape)) {
  124. warnings.push_back(vformat(RTR("When used for collision, ConcavePolygonShape3D is intended to work with static CollisionObject3D nodes like StaticBody3D.\nIt will likely not behave well for %ss (except when frozen and freeze_mode set to FREEZE_MODE_STATIC)."), body_type));
  125. } else if (Object::cast_to<WorldBoundaryShape3D>(*shape)) {
  126. warnings.push_back(RTR("WorldBoundaryShape3D doesn't support RigidBody3D in another mode than static."));
  127. }
  128. }
  129. if (shape.is_valid() && Object::cast_to<CharacterBody3D>(col_object)) {
  130. if (Object::cast_to<ConcavePolygonShape3D>(*shape)) {
  131. warnings.push_back(RTR("When used for collision, ConcavePolygonShape3D is intended to work with static CollisionObject3D nodes like StaticBody3D.\nIt will likely not behave well for CharacterBody3Ds."));
  132. }
  133. }
  134. Vector3 scale = get_transform().get_basis().get_scale();
  135. if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
  136. 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."));
  137. }
  138. return warnings;
  139. }
  140. void CollisionShape3D::_bind_methods() {
  141. #ifndef DISABLE_DEPRECATED
  142. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape3D::resource_changed);
  143. #endif
  144. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape3D::set_shape);
  145. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape3D::get_shape);
  146. ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape3D::set_disabled);
  147. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape3D::is_disabled);
  148. ClassDB::bind_method(D_METHOD("make_convex_from_siblings"), &CollisionShape3D::make_convex_from_siblings);
  149. ClassDB::set_method_flags("CollisionShape3D", "make_convex_from_siblings", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  150. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
  151. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  152. }
  153. void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
  154. if (p_shape == shape) {
  155. return;
  156. }
  157. if (shape.is_valid()) {
  158. shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
  159. }
  160. shape = p_shape;
  161. if (shape.is_valid()) {
  162. shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
  163. }
  164. update_gizmos();
  165. if (collision_object) {
  166. collision_object->shape_owner_clear_shapes(owner_id);
  167. if (shape.is_valid()) {
  168. collision_object->shape_owner_add_shape(owner_id, shape);
  169. }
  170. }
  171. if (is_inside_tree() && collision_object) {
  172. // If this is a heightfield shape our center may have changed
  173. _update_in_shape_owner(true);
  174. }
  175. update_configuration_warnings();
  176. }
  177. Ref<Shape3D> CollisionShape3D::get_shape() const {
  178. return shape;
  179. }
  180. void CollisionShape3D::set_disabled(bool p_disabled) {
  181. disabled = p_disabled;
  182. update_gizmos();
  183. if (collision_object) {
  184. collision_object->shape_owner_set_disabled(owner_id, p_disabled);
  185. }
  186. }
  187. bool CollisionShape3D::is_disabled() const {
  188. return disabled;
  189. }
  190. CollisionShape3D::CollisionShape3D() {
  191. //indicator = RenderingServer::get_singleton()->mesh_create();
  192. set_notify_local_transform(true);
  193. }
  194. CollisionShape3D::~CollisionShape3D() {
  195. //RenderingServer::get_singleton()->free(indicator);
  196. }