collision_shape_3d.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*************************************************************************/
  2. /* collision_shape_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_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. void CollisionShape3D::make_convex_from_siblings() {
  36. Node *p = get_parent();
  37. if (!p) {
  38. return;
  39. }
  40. Vector<Vector3> vertices;
  41. for (int i = 0; i < p->get_child_count(); i++) {
  42. Node *n = p->get_child(i);
  43. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(n);
  44. if (mi) {
  45. Ref<Mesh> m = mi->get_mesh();
  46. if (m.is_valid()) {
  47. for (int j = 0; j < m->get_surface_count(); j++) {
  48. Array a = m->surface_get_arrays(j);
  49. if (!a.is_empty()) {
  50. Vector<Vector3> v = a[RenderingServer::ARRAY_VERTEX];
  51. for (int k = 0; k < v.size(); k++) {
  52. vertices.append(mi->get_transform().xform(v[k]));
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. Ref<ConvexPolygonShape3D> shape = memnew(ConvexPolygonShape3D);
  60. shape->set_points(vertices);
  61. set_shape(shape);
  62. }
  63. void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
  64. parent->shape_owner_set_transform(owner_id, get_transform());
  65. if (p_xform_only) {
  66. return;
  67. }
  68. parent->shape_owner_set_disabled(owner_id, disabled);
  69. }
  70. void CollisionShape3D::_notification(int p_what) {
  71. switch (p_what) {
  72. case NOTIFICATION_PARENTED: {
  73. parent = Object::cast_to<CollisionObject3D>(get_parent());
  74. if (parent) {
  75. owner_id = parent->create_shape_owner(this);
  76. if (shape.is_valid()) {
  77. parent->shape_owner_add_shape(owner_id, shape);
  78. }
  79. _update_in_shape_owner();
  80. }
  81. } break;
  82. case NOTIFICATION_ENTER_TREE: {
  83. if (parent) {
  84. _update_in_shape_owner();
  85. }
  86. } break;
  87. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  88. if (parent) {
  89. _update_in_shape_owner(true);
  90. }
  91. } break;
  92. case NOTIFICATION_UNPARENTED: {
  93. if (parent) {
  94. parent->remove_shape_owner(owner_id);
  95. }
  96. owner_id = 0;
  97. parent = nullptr;
  98. } break;
  99. }
  100. }
  101. void CollisionShape3D::resource_changed(Ref<Resource> res) {
  102. update_gizmos();
  103. }
  104. TypedArray<String> CollisionShape3D::get_configuration_warnings() const {
  105. TypedArray<String> warnings = Node::get_configuration_warnings();
  106. if (!Object::cast_to<CollisionObject3D>(get_parent())) {
  107. warnings.push_back(RTR("CollisionShape3D only serves to provide a collision shape to a CollisionObject3D derived node. Please only use it as a child of Area3D, StaticBody3D, RigidBody3D, CharacterBody3D, etc. to give them a shape."));
  108. }
  109. if (!shape.is_valid()) {
  110. warnings.push_back(RTR("A shape must be provided for CollisionShape3D to function. Please create a shape resource for it."));
  111. }
  112. if (shape.is_valid() &&
  113. Object::cast_to<RigidBody3D>(get_parent()) &&
  114. Object::cast_to<ConcavePolygonShape3D>(*shape)) {
  115. warnings.push_back(RTR("ConcavePolygonShape3D doesn't support RigidBody3D in another mode than static."));
  116. }
  117. return warnings;
  118. }
  119. void CollisionShape3D::_bind_methods() {
  120. //not sure if this should do anything
  121. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape3D::resource_changed);
  122. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape3D::set_shape);
  123. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape3D::get_shape);
  124. ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape3D::set_disabled);
  125. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape3D::is_disabled);
  126. ClassDB::bind_method(D_METHOD("make_convex_from_siblings"), &CollisionShape3D::make_convex_from_siblings);
  127. ClassDB::set_method_flags("CollisionShape3D", "make_convex_from_siblings", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  128. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
  129. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  130. }
  131. void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
  132. if (p_shape == shape) {
  133. return;
  134. }
  135. if (!shape.is_null()) {
  136. shape->unregister_owner(this);
  137. }
  138. shape = p_shape;
  139. if (!shape.is_null()) {
  140. shape->register_owner(this);
  141. }
  142. update_gizmos();
  143. if (parent) {
  144. parent->shape_owner_clear_shapes(owner_id);
  145. if (shape.is_valid()) {
  146. parent->shape_owner_add_shape(owner_id, shape);
  147. }
  148. }
  149. if (is_inside_tree() && parent) {
  150. // If this is a heightfield shape our center may have changed
  151. _update_in_shape_owner(true);
  152. }
  153. update_configuration_warnings();
  154. }
  155. Ref<Shape3D> CollisionShape3D::get_shape() const {
  156. return shape;
  157. }
  158. void CollisionShape3D::set_disabled(bool p_disabled) {
  159. disabled = p_disabled;
  160. update_gizmos();
  161. if (parent) {
  162. parent->shape_owner_set_disabled(owner_id, p_disabled);
  163. }
  164. }
  165. bool CollisionShape3D::is_disabled() const {
  166. return disabled;
  167. }
  168. CollisionShape3D::CollisionShape3D() {
  169. //indicator = RenderingServer::get_singleton()->mesh_create();
  170. set_notify_local_transform(true);
  171. }
  172. CollisionShape3D::~CollisionShape3D() {
  173. if (!shape.is_null()) {
  174. shape->unregister_owner(this);
  175. }
  176. //RenderingServer::get_singleton()->free(indicator);
  177. }