collision_shape.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* collision_shape.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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.h"
  31. #include "scene/resources/box_shape.h"
  32. #include "scene/resources/capsule_shape.h"
  33. #include "scene/resources/concave_polygon_shape.h"
  34. #include "scene/resources/convex_polygon_shape.h"
  35. #include "scene/resources/plane_shape.h"
  36. #include "scene/resources/ray_shape.h"
  37. #include "scene/resources/sphere_shape.h"
  38. #include "servers/visual_server.h"
  39. //TODO: Implement CylinderShape and HeightMapShape?
  40. #include "core/math/quick_hull.h"
  41. #include "mesh_instance.h"
  42. #include "physics_body.h"
  43. void CollisionShape::make_convex_from_brothers() {
  44. Node *p = get_parent();
  45. if (!p)
  46. return;
  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. parent->shape_owner_set_disabled(owner_id, disabled);
  64. }
  65. void CollisionShape::_notification(int p_what) {
  66. switch (p_what) {
  67. case NOTIFICATION_PARENTED: {
  68. parent = Object::cast_to<CollisionObject>(get_parent());
  69. if (parent) {
  70. owner_id = parent->create_shape_owner(this);
  71. if (shape.is_valid()) {
  72. parent->shape_owner_add_shape(owner_id, shape);
  73. }
  74. _update_in_shape_owner();
  75. }
  76. } break;
  77. case NOTIFICATION_ENTER_TREE: {
  78. if (parent) {
  79. _update_in_shape_owner();
  80. }
  81. if (get_tree()->is_debugging_collisions_hint()) {
  82. _create_debug_shape();
  83. }
  84. } break;
  85. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  86. if (parent) {
  87. _update_in_shape_owner(true);
  88. }
  89. } break;
  90. case NOTIFICATION_UNPARENTED: {
  91. if (parent) {
  92. parent->remove_shape_owner(owner_id);
  93. }
  94. owner_id = 0;
  95. parent = NULL;
  96. } break;
  97. }
  98. }
  99. void CollisionShape::resource_changed(RES res) {
  100. update_gizmo();
  101. }
  102. String CollisionShape::get_configuration_warning() const {
  103. if (!Object::cast_to<CollisionObject>(get_parent())) {
  104. return 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.");
  105. }
  106. if (!shape.is_valid()) {
  107. return TTR("A shape must be provided for CollisionShape to function. Please create a shape resource for it!");
  108. }
  109. return String();
  110. }
  111. void CollisionShape::_bind_methods() {
  112. //not sure if this should do anything
  113. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape::resource_changed);
  114. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape::set_shape);
  115. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape::get_shape);
  116. ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape::set_disabled);
  117. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape::is_disabled);
  118. ClassDB::bind_method(D_METHOD("make_convex_from_brothers"), &CollisionShape::make_convex_from_brothers);
  119. ClassDB::set_method_flags("CollisionShape", "make_convex_from_brothers", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  120. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape"), "set_shape", "get_shape");
  121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  122. }
  123. void CollisionShape::set_shape(const Ref<Shape> &p_shape) {
  124. if (!shape.is_null())
  125. shape->unregister_owner(this);
  126. shape = p_shape;
  127. if (!shape.is_null())
  128. shape->register_owner(this);
  129. update_gizmo();
  130. if (parent) {
  131. parent->shape_owner_clear_shapes(owner_id);
  132. if (shape.is_valid()) {
  133. parent->shape_owner_add_shape(owner_id, shape);
  134. }
  135. }
  136. update_configuration_warning();
  137. }
  138. Ref<Shape> CollisionShape::get_shape() const {
  139. return shape;
  140. }
  141. void CollisionShape::set_disabled(bool p_disabled) {
  142. disabled = p_disabled;
  143. update_gizmo();
  144. if (parent) {
  145. parent->shape_owner_set_disabled(owner_id, p_disabled);
  146. }
  147. }
  148. bool CollisionShape::is_disabled() const {
  149. return disabled;
  150. }
  151. CollisionShape::CollisionShape() {
  152. //indicator = VisualServer::get_singleton()->mesh_create();
  153. disabled = false;
  154. debug_shape = NULL;
  155. parent = NULL;
  156. owner_id = 0;
  157. set_notify_local_transform(true);
  158. }
  159. CollisionShape::~CollisionShape() {
  160. if (!shape.is_null())
  161. shape->unregister_owner(this);
  162. //VisualServer::get_singleton()->free(indicator);
  163. }
  164. void CollisionShape::_create_debug_shape() {
  165. if (debug_shape) {
  166. debug_shape->queue_delete();
  167. debug_shape = NULL;
  168. }
  169. Ref<Shape> s = get_shape();
  170. if (s.is_null())
  171. return;
  172. Ref<Mesh> mesh = s->get_debug_mesh();
  173. MeshInstance *mi = memnew(MeshInstance);
  174. mi->set_mesh(mesh);
  175. add_child(mi);
  176. debug_shape = mi;
  177. }