collision_shape.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************************************************/
  2. /* collision_shape.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.h"
  31. #include "core/math/quick_hull.h"
  32. #include "mesh_instance.h"
  33. #include "physics_body.h"
  34. #include "scene/resources/box_shape.h"
  35. #include "scene/resources/capsule_shape.h"
  36. #include "scene/resources/concave_polygon_shape.h"
  37. #include "scene/resources/convex_polygon_shape.h"
  38. #include "scene/resources/plane_shape.h"
  39. #include "scene/resources/ray_shape.h"
  40. #include "scene/resources/sphere_shape.h"
  41. #include "servers/visual_server.h"
  42. void CollisionShape::make_convex_from_brothers() {
  43. Node *p = get_parent();
  44. if (!p) {
  45. return;
  46. }
  47. for (int i = 0; i < p->get_child_count(); i++) {
  48. Node *n = p->get_child(i);
  49. MeshInstance *mi = Object::cast_to<MeshInstance>(n);
  50. if (mi) {
  51. Ref<Mesh> m = mi->get_mesh();
  52. if (m.is_valid()) {
  53. Ref<Shape> s = m->create_convex_shape();
  54. set_shape(s);
  55. }
  56. }
  57. }
  58. }
  59. void CollisionShape::_update_in_shape_owner(bool p_xform_only) {
  60. parent->shape_owner_set_transform(owner_id, get_transform());
  61. if (p_xform_only) {
  62. return;
  63. }
  64. parent->shape_owner_set_disabled(owner_id, disabled);
  65. }
  66. void CollisionShape::_notification(int p_what) {
  67. switch (p_what) {
  68. case NOTIFICATION_PARENTED: {
  69. parent = Object::cast_to<CollisionObject>(get_parent());
  70. if (parent) {
  71. owner_id = parent->create_shape_owner(this);
  72. if (shape.is_valid()) {
  73. parent->shape_owner_add_shape(owner_id, shape);
  74. }
  75. _update_in_shape_owner();
  76. }
  77. } break;
  78. case NOTIFICATION_ENTER_TREE: {
  79. if (parent) {
  80. _update_in_shape_owner();
  81. }
  82. } break;
  83. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  84. if (parent) {
  85. _update_in_shape_owner(true);
  86. }
  87. } break;
  88. case NOTIFICATION_UNPARENTED: {
  89. if (parent) {
  90. parent->remove_shape_owner(owner_id);
  91. }
  92. owner_id = 0;
  93. parent = nullptr;
  94. } break;
  95. }
  96. }
  97. void CollisionShape::resource_changed(RES res) {
  98. update_gizmo();
  99. }
  100. String CollisionShape::get_configuration_warning() const {
  101. String warning = Spatial::get_configuration_warning();
  102. if (!Object::cast_to<CollisionObject>(get_parent())) {
  103. if (warning != String()) {
  104. warning += "\n\n";
  105. }
  106. warning += TTR("CollisionShape 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.");
  107. }
  108. if (!shape.is_valid()) {
  109. if (warning != String()) {
  110. warning += "\n\n";
  111. }
  112. warning += TTR("A shape must be provided for CollisionShape to function. Please create a shape resource for it.");
  113. } else {
  114. if (shape->is_class("PlaneShape")) {
  115. if (warning != String()) {
  116. warning += "\n\n";
  117. }
  118. warning += TTR("Plane shapes don't work well and will be removed in future versions. Please don't use them.");
  119. }
  120. if (Object::cast_to<RigidBody>(get_parent()) &&
  121. Object::cast_to<ConcavePolygonShape>(*shape) &&
  122. Object::cast_to<RigidBody>(get_parent())->get_mode() != RigidBody::MODE_STATIC) {
  123. if (warning != String()) {
  124. warning += "\n\n";
  125. }
  126. warning += TTR("ConcavePolygonShape doesn't support RigidBody in another mode than static.");
  127. }
  128. }
  129. return warning;
  130. }
  131. void CollisionShape::_bind_methods() {
  132. //not sure if this should do anything
  133. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape::resource_changed);
  134. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape::set_shape);
  135. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape::get_shape);
  136. ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape::set_disabled);
  137. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape::is_disabled);
  138. ClassDB::bind_method(D_METHOD("make_convex_from_brothers"), &CollisionShape::make_convex_from_brothers);
  139. ClassDB::set_method_flags("CollisionShape", "make_convex_from_brothers", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  140. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape"), "set_shape", "get_shape");
  141. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  142. }
  143. void CollisionShape::set_shape(const Ref<Shape> &p_shape) {
  144. if (p_shape == shape) {
  145. return;
  146. }
  147. if (!shape.is_null()) {
  148. shape->unregister_owner(this);
  149. }
  150. shape = p_shape;
  151. if (!shape.is_null()) {
  152. shape->register_owner(this);
  153. }
  154. update_gizmo();
  155. if (parent) {
  156. parent->shape_owner_clear_shapes(owner_id);
  157. if (shape.is_valid()) {
  158. parent->shape_owner_add_shape(owner_id, shape);
  159. }
  160. }
  161. if (is_inside_tree() && parent) {
  162. // If this is a heightfield shape our center may have changed
  163. _update_in_shape_owner(true);
  164. }
  165. update_configuration_warning();
  166. }
  167. Ref<Shape> CollisionShape::get_shape() const {
  168. return shape;
  169. }
  170. void CollisionShape::set_disabled(bool p_disabled) {
  171. disabled = p_disabled;
  172. update_gizmo();
  173. if (parent) {
  174. parent->shape_owner_set_disabled(owner_id, p_disabled);
  175. }
  176. }
  177. bool CollisionShape::is_disabled() const {
  178. return disabled;
  179. }
  180. CollisionShape::CollisionShape() {
  181. //indicator = RID_PRIME(VisualServer::get_singleton()->mesh_create());
  182. disabled = false;
  183. parent = nullptr;
  184. owner_id = 0;
  185. set_notify_local_transform(true);
  186. }
  187. CollisionShape::~CollisionShape() {
  188. if (!shape.is_null()) {
  189. shape->unregister_owner(this);
  190. }
  191. //VisualServer::get_singleton()->free(indicator);
  192. }