collision_shape_3d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "scene/3d/mesh_instance_3d.h"
  32. #include "scene/3d/physics/character_body_3d.h"
  33. #include "scene/3d/physics/vehicle_body_3d.h"
  34. #include "scene/resources/3d/concave_polygon_shape_3d.h"
  35. #include "scene/resources/3d/convex_polygon_shape_3d.h"
  36. #include "scene/resources/3d/world_boundary_shape_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. PackedStringArray CollisionShape3D::get_configuration_warnings() const {
  109. PackedStringArray warnings = Node3D::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. #ifdef DEBUG_ENABLED
  152. ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape3D::set_debug_color);
  153. ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape3D::get_debug_color);
  154. ClassDB::bind_method(D_METHOD("set_enable_debug_fill", "enable"), &CollisionShape3D::set_debug_fill_enabled);
  155. ClassDB::bind_method(D_METHOD("get_enable_debug_fill"), &CollisionShape3D::get_debug_fill_enabled);
  156. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
  157. // Default value depends on a project setting, override for doc generation purposes.
  158. ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
  159. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_fill"), "set_enable_debug_fill", "get_enable_debug_fill");
  160. #endif // DEBUG_ENABLED
  161. }
  162. void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
  163. if (p_shape == shape) {
  164. return;
  165. }
  166. if (shape.is_valid()) {
  167. #ifdef DEBUG_ENABLED
  168. shape->disconnect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
  169. #endif // DEBUG_ENABLED
  170. shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
  171. }
  172. shape = p_shape;
  173. if (shape.is_valid()) {
  174. #ifdef DEBUG_ENABLED
  175. if (shape->are_debug_properties_edited()) {
  176. set_debug_color(shape->get_debug_color());
  177. set_debug_fill_enabled(shape->get_debug_fill());
  178. } else {
  179. shape->set_debug_color(debug_color);
  180. shape->set_debug_fill(debug_fill);
  181. }
  182. #endif // DEBUG_ENABLED
  183. shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
  184. #ifdef DEBUG_ENABLED
  185. shape->connect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
  186. #endif // DEBUG_ENABLED
  187. }
  188. update_gizmos();
  189. if (collision_object) {
  190. collision_object->shape_owner_clear_shapes(owner_id);
  191. if (shape.is_valid()) {
  192. collision_object->shape_owner_add_shape(owner_id, shape);
  193. }
  194. }
  195. if (is_inside_tree() && collision_object) {
  196. // If this is a heightfield shape our center may have changed
  197. _update_in_shape_owner(true);
  198. }
  199. update_configuration_warnings();
  200. }
  201. Ref<Shape3D> CollisionShape3D::get_shape() const {
  202. return shape;
  203. }
  204. void CollisionShape3D::set_disabled(bool p_disabled) {
  205. disabled = p_disabled;
  206. update_gizmos();
  207. if (collision_object) {
  208. collision_object->shape_owner_set_disabled(owner_id, p_disabled);
  209. }
  210. }
  211. bool CollisionShape3D::is_disabled() const {
  212. return disabled;
  213. }
  214. #ifdef DEBUG_ENABLED
  215. Color CollisionShape3D::_get_default_debug_color() const {
  216. const SceneTree *st = SceneTree::get_singleton();
  217. return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
  218. }
  219. void CollisionShape3D::set_debug_color(const Color &p_color) {
  220. if (debug_color == p_color) {
  221. return;
  222. }
  223. debug_color = p_color;
  224. if (shape.is_valid()) {
  225. shape->set_debug_color(p_color);
  226. }
  227. }
  228. Color CollisionShape3D::get_debug_color() const {
  229. return debug_color;
  230. }
  231. void CollisionShape3D::set_debug_fill_enabled(bool p_enable) {
  232. if (debug_fill == p_enable) {
  233. return;
  234. }
  235. debug_fill = p_enable;
  236. if (shape.is_valid()) {
  237. shape->set_debug_fill(p_enable);
  238. }
  239. }
  240. bool CollisionShape3D::get_debug_fill_enabled() const {
  241. return debug_fill;
  242. }
  243. bool CollisionShape3D::_property_can_revert(const StringName &p_name) const {
  244. if (p_name == "debug_color") {
  245. return true;
  246. }
  247. return false;
  248. }
  249. bool CollisionShape3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  250. if (p_name == "debug_color") {
  251. r_property = _get_default_debug_color();
  252. return true;
  253. }
  254. return false;
  255. }
  256. void CollisionShape3D::_validate_property(PropertyInfo &p_property) const {
  257. if (p_property.name == "debug_color") {
  258. if (debug_color == _get_default_debug_color()) {
  259. p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;
  260. } else {
  261. p_property.usage = PROPERTY_USAGE_DEFAULT;
  262. }
  263. }
  264. }
  265. void CollisionShape3D::_shape_changed() {
  266. if (shape->get_debug_color() != debug_color) {
  267. set_debug_color(shape->get_debug_color());
  268. }
  269. if (shape->get_debug_fill() != debug_fill) {
  270. set_debug_fill_enabled(shape->get_debug_fill());
  271. }
  272. }
  273. #endif // DEBUG_ENABLED
  274. CollisionShape3D::CollisionShape3D() {
  275. //indicator = RenderingServer::get_singleton()->mesh_create();
  276. set_notify_local_transform(true);
  277. #ifdef DEBUG_ENABLED
  278. debug_color = _get_default_debug_color();
  279. #endif // DEBUG_ENABLED
  280. }
  281. CollisionShape3D::~CollisionShape3D() {
  282. //RenderingServer::get_singleton()->free(indicator);
  283. }