collision_shape_3d.cpp 8.6 KB

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