Debian Codegraph @

scene/3d/physics/static_body_3d.cpp

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