collision_shape.cpp 6.7 KB

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