static_body_3d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* static_body_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 "static_body_3d.h"
  31. #ifndef NAVIGATION_3D_DISABLED
  32. #include "core/math/convex_hull.h"
  33. #include "scene/resources/3d/box_shape_3d.h"
  34. #include "scene/resources/3d/capsule_shape_3d.h"
  35. #include "scene/resources/3d/concave_polygon_shape_3d.h"
  36. #include "scene/resources/3d/convex_polygon_shape_3d.h"
  37. #include "scene/resources/3d/cylinder_shape_3d.h"
  38. #include "scene/resources/3d/height_map_shape_3d.h"
  39. #include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
  40. #include "scene/resources/3d/primitive_meshes.h"
  41. #include "scene/resources/3d/shape_3d.h"
  42. #include "scene/resources/3d/sphere_shape_3d.h"
  43. #include "scene/resources/3d/world_boundary_shape_3d.h"
  44. #include "scene/resources/navigation_mesh.h"
  45. #include "servers/navigation_server_3d.h"
  46. Callable StaticBody3D::_navmesh_source_geometry_parsing_callback;
  47. RID StaticBody3D::_navmesh_source_geometry_parser;
  48. #endif // NAVIGATION_3D_DISABLED
  49. void StaticBody3D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  50. if (physics_material_override.is_valid()) {
  51. physics_material_override->disconnect_changed(callable_mp(this, &StaticBody3D::_reload_physics_characteristics));
  52. }
  53. physics_material_override = p_physics_material_override;
  54. if (physics_material_override.is_valid()) {
  55. physics_material_override->connect_changed(callable_mp(this, &StaticBody3D::_reload_physics_characteristics));
  56. }
  57. _reload_physics_characteristics();
  58. }
  59. Ref<PhysicsMaterial> StaticBody3D::get_physics_material_override() const {
  60. return physics_material_override;
  61. }
  62. void StaticBody3D::set_constant_linear_velocity(const Vector3 &p_vel) {
  63. constant_linear_velocity = p_vel;
  64. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
  65. }
  66. void StaticBody3D::set_constant_angular_velocity(const Vector3 &p_vel) {
  67. constant_angular_velocity = p_vel;
  68. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
  69. }
  70. Vector3 StaticBody3D::get_constant_linear_velocity() const {
  71. return constant_linear_velocity;
  72. }
  73. Vector3 StaticBody3D::get_constant_angular_velocity() const {
  74. return constant_angular_velocity;
  75. }
  76. void StaticBody3D::_reload_physics_characteristics() {
  77. if (physics_material_override.is_null()) {
  78. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, 0);
  79. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, 1);
  80. } else {
  81. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  82. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  83. }
  84. }
  85. #ifndef NAVIGATION_3D_DISABLED
  86. void StaticBody3D::navmesh_parse_init() {
  87. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  88. if (!_navmesh_source_geometry_parser.is_valid()) {
  89. _navmesh_source_geometry_parsing_callback = callable_mp_static(&StaticBody3D::navmesh_parse_source_geometry);
  90. _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
  91. NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  92. }
  93. }
  94. void StaticBody3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
  95. StaticBody3D *static_body = Object::cast_to<StaticBody3D>(p_node);
  96. if (static_body == nullptr) {
  97. return;
  98. }
  99. NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  100. uint32_t parsed_collision_mask = p_navigation_mesh->get_collision_mask();
  101. if ((parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) && (static_body->get_collision_layer() & parsed_collision_mask)) {
  102. List<uint32_t> shape_owners;
  103. static_body->get_shape_owners(&shape_owners);
  104. for (uint32_t shape_owner : shape_owners) {
  105. if (static_body->is_shape_owner_disabled(shape_owner)) {
  106. continue;
  107. }
  108. const int shape_count = static_body->shape_owner_get_shape_count(shape_owner);
  109. for (int shape_index = 0; shape_index < shape_count; shape_index++) {
  110. Ref<Shape3D> s = static_body->shape_owner_get_shape(shape_owner, shape_index);
  111. if (s.is_null()) {
  112. continue;
  113. }
  114. const Transform3D transform = static_body->get_global_transform() * static_body->shape_owner_get_transform(shape_owner);
  115. BoxShape3D *box = Object::cast_to<BoxShape3D>(*s);
  116. if (box) {
  117. Array arr;
  118. arr.resize(RS::ARRAY_MAX);
  119. BoxMesh::create_mesh_array(arr, box->get_size());
  120. p_source_geometry_data->add_mesh_array(arr, transform);
  121. }
  122. CapsuleShape3D *capsule = Object::cast_to<CapsuleShape3D>(*s);
  123. if (capsule) {
  124. Array arr;
  125. arr.resize(RS::ARRAY_MAX);
  126. CapsuleMesh::create_mesh_array(arr, capsule->get_radius(), capsule->get_height());
  127. p_source_geometry_data->add_mesh_array(arr, transform);
  128. }
  129. CylinderShape3D *cylinder = Object::cast_to<CylinderShape3D>(*s);
  130. if (cylinder) {
  131. Array arr;
  132. arr.resize(RS::ARRAY_MAX);
  133. CylinderMesh::create_mesh_array(arr, cylinder->get_radius(), cylinder->get_radius(), cylinder->get_height());
  134. p_source_geometry_data->add_mesh_array(arr, transform);
  135. }
  136. SphereShape3D *sphere = Object::cast_to<SphereShape3D>(*s);
  137. if (sphere) {
  138. Array arr;
  139. arr.resize(RS::ARRAY_MAX);
  140. SphereMesh::create_mesh_array(arr, sphere->get_radius(), sphere->get_radius() * 2.0);
  141. p_source_geometry_data->add_mesh_array(arr, transform);
  142. }
  143. ConcavePolygonShape3D *concave_polygon = Object::cast_to<ConcavePolygonShape3D>(*s);
  144. if (concave_polygon) {
  145. p_source_geometry_data->add_faces(concave_polygon->get_faces(), transform);
  146. }
  147. ConvexPolygonShape3D *convex_polygon = Object::cast_to<ConvexPolygonShape3D>(*s);
  148. if (convex_polygon) {
  149. Vector<Vector3> varr = Variant(convex_polygon->get_points());
  150. Geometry3D::MeshData md;
  151. Error err = ConvexHullComputer::convex_hull(varr, md);
  152. if (err == OK) {
  153. PackedVector3Array faces;
  154. for (const Geometry3D::MeshData::Face &face : md.faces) {
  155. for (uint32_t k = 2; k < face.indices.size(); ++k) {
  156. faces.push_back(md.vertices[face.indices[0]]);
  157. faces.push_back(md.vertices[face.indices[k - 1]]);
  158. faces.push_back(md.vertices[face.indices[k]]);
  159. }
  160. }
  161. p_source_geometry_data->add_faces(faces, transform);
  162. }
  163. }
  164. HeightMapShape3D *heightmap_shape = Object::cast_to<HeightMapShape3D>(*s);
  165. if (heightmap_shape) {
  166. int heightmap_depth = heightmap_shape->get_map_depth();
  167. int heightmap_width = heightmap_shape->get_map_width();
  168. if (heightmap_depth >= 2 && heightmap_width >= 2) {
  169. const Vector<real_t> &map_data = heightmap_shape->get_map_data();
  170. Vector2 heightmap_gridsize(heightmap_width - 1, heightmap_depth - 1);
  171. Vector3 start = Vector3(heightmap_gridsize.x, 0, heightmap_gridsize.y) * -0.5;
  172. Vector<Vector3> vertex_array;
  173. vertex_array.resize((heightmap_depth - 1) * (heightmap_width - 1) * 6);
  174. Vector3 *vertex_array_ptrw = vertex_array.ptrw();
  175. const real_t *map_data_ptr = map_data.ptr();
  176. int vertex_index = 0;
  177. for (int d = 0; d < heightmap_depth - 1; d++) {
  178. for (int w = 0; w < heightmap_width - 1; w++) {
  179. vertex_array_ptrw[vertex_index] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + w], d);
  180. vertex_array_ptrw[vertex_index + 1] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + w + 1], d);
  181. vertex_array_ptrw[vertex_index + 2] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + heightmap_width + w], d + 1);
  182. vertex_array_ptrw[vertex_index + 3] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + w + 1], d);
  183. vertex_array_ptrw[vertex_index + 4] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + heightmap_width + w + 1], d + 1);
  184. vertex_array_ptrw[vertex_index + 5] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + heightmap_width + w], d + 1);
  185. vertex_index += 6;
  186. }
  187. }
  188. if (vertex_array.size() > 0) {
  189. p_source_geometry_data->add_faces(vertex_array, transform);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. #endif // NAVIGATION_3D_DISABLED
  198. void StaticBody3D::_bind_methods() {
  199. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody3D::set_constant_linear_velocity);
  200. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody3D::set_constant_angular_velocity);
  201. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody3D::get_constant_linear_velocity);
  202. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody3D::get_constant_angular_velocity);
  203. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody3D::set_physics_material_override);
  204. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody3D::get_physics_material_override);
  205. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  206. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_linear_velocity", PROPERTY_HINT_NONE, "suffix:m/s"), "set_constant_linear_velocity", "get_constant_linear_velocity");
  207. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_constant_angular_velocity", "get_constant_angular_velocity");
  208. }
  209. StaticBody3D::StaticBody3D(PhysicsServer3D::BodyMode p_mode) :
  210. PhysicsBody3D(p_mode) {
  211. }