static_body_3d.cpp 12 KB

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