Debian Codegraph @

modules/godot_physics_3d/godot_soft_body_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* godot_soft_body_3d.h */
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 #pragma once
32
33 #include "godot_area_3d.h"
34 #include "godot_collision_object_3d.h"
35
36 #include "core/math/aabb.h"
37 #include "core/math/dynamic_bvh.h"
38 #include "core/math/vector3.h"
39 #include "core/templates/hash_set.h"
40 #include "core/templates/local_vector.h"
41 #include "core/templates/vset.h"
42
43 class GodotConstraint3D;
44
45 class GodotSoftBody3D : public GodotCollisionObject3D {
46 RID soft_mesh;
47
48 struct Node {
49 Vector3 s; // Source position
50 Vector3 x; // Position
51 Vector3 q; // Previous step position/Test position
52 Vector3 f; // Force accumulator
53 Vector3 v; // Velocity
54 Vector3 bv; // Biased Velocity
55 Vector3 n; // Normal
56 real_t area = 0.0; // Area
57 real_t im = 0.0; // 1/mass
58 DynamicBVH::ID leaf; // Leaf data
59 uint32_t index = 0;
60 };
61
62 struct Link {
63 Vector3 c3; // gradient
64 Node *n[2] = { nullptr, nullptr }; // Node pointers
65 real_t rl = 0.0; // Rest length
66 real_t c0 = 0.0; // (ima+imb)*kLST
67 real_t c1 = 0.0; // rl^2
68 real_t c2 = 0.0; // |gradient|^2/c0
69 };
70
71 struct Face {
72 Vector3 centroid;
73 Node *n[3] = { nullptr, nullptr, nullptr }; // Node pointers
74 Vector3 normal; // Normal
75 real_t ra = 0.0; // Rest area
76 DynamicBVH::ID leaf; // Leaf data
77 uint32_t index = 0;
78 };
79
80 LocalVector<Node> nodes;
81 LocalVector<Link> links;
82 LocalVector<Face> faces;
83
84 DynamicBVH node_tree;
85 DynamicBVH face_tree;
86
87 LocalVector<uint32_t> map_visual_to_physics;
88
89 AABB bounds;
90
91 real_t collision_margin = 0.05;
92
93 real_t total_mass = 1.0;
94 real_t inv_total_mass = 1.0;
95
96 int iteration_count = 5;
97 real_t linear_stiffness = 0.5; // [0,1]
98 real_t shrinking_factor = 0.0; // [-1,1]
99 real_t pressure_coefficient = 0.0; // [-inf,+inf]
100 real_t damping_coefficient = 0.01; // [0,1]
101 real_t drag_coefficient = 0.0; // [0,1]
102 LocalVector<int> pinned_vertices;
103
104 SelfList<GodotSoftBody3D> active_list;
105
106 HashSet<GodotConstraint3D *> constraints;
107
108 Vector<AreaCMP> areas;
109
110 VSet<RID> exceptions;
111
112 uint64_t island_step = 0;
113
114 _FORCE_INLINE_ Vector3 _compute_area_windforce(const GodotArea3D *p_area, const Face *p_face);
115
116 public:
117 GodotSoftBody3D();
118
119 const AABB &get_bounds() const { return bounds; }
120
121 void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant);
122 Variant get_state(PhysicsServer3D::BodyState p_state) const;
123
124 _FORCE_INLINE_ void add_constraint(GodotConstraint3D *p_constraint) { constraints.insert(p_constraint); }
125 _FORCE_INLINE_ void remove_constraint(GodotConstraint3D *p_constraint) { constraints.erase(p_constraint); }
126 _FORCE_INLINE_ const HashSet<GodotConstraint3D *> &get_constraints() const { return constraints; }
127 _FORCE_INLINE_ void clear_constraints() { constraints.clear(); }
128
129 _FORCE_INLINE_ void add_exception(const RID &p_exception) { exceptions.insert(p_exception); }
130 _FORCE_INLINE_ void remove_exception(const RID &p_exception) { exceptions.erase(p_exception); }
131 _FORCE_INLINE_ bool has_exception(const RID &p_exception) const { return exceptions.has(p_exception); }
132 _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
133
134 _FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }
135 _FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step = p_step; }
136
137 _FORCE_INLINE_ void add_area(GodotArea3D *p_area) {
138 int index = areas.find(AreaCMP(p_area));
139 if (index > -1) {
140 areas.write[index].refCount += 1;
141 } else {
142 areas.ordered_insert(AreaCMP(p_area));
143 }
144 }
145
146 _FORCE_INLINE_ void remove_area(GodotArea3D *p_area) {
147 int index = areas.find(AreaCMP(p_area));
148 if (index > -1) {
149 areas.write[index].refCount -= 1;
150 if (areas[index].refCount < 1) {
151 areas.remove_at(index);
152 }
153 }
154 }
155
156 virtual void set_space(GodotSpace3D *p_space) override;
157
158 void set_mesh(RID p_mesh);
159
160 void update_rendering_server(PhysicsServer3DRenderingServerHandler *p_rendering_server_handler);
161
162 Vector3 get_vertex_position(int p_index) const;
163 void set_vertex_position(int p_index, const Vector3 &p_position);
164
165 void pin_vertex(int p_index);
166 void unpin_vertex(int p_index);
167 void unpin_all_vertices();
168 bool is_vertex_pinned(int p_index) const;
169
170 uint32_t get_node_count() const;
171 real_t get_node_inv_mass(uint32_t p_node_index) const;
172 Vector3 get_node_position(uint32_t p_node_index) const;
173 Vector3 get_node_velocity(uint32_t p_node_index) const;
174 Vector3 get_node_biased_velocity(uint32_t p_node_index) const;
175 void apply_node_impulse(uint32_t p_node_index, const Vector3 &p_impulse);
176 void apply_node_force(uint32_t p_node_index, const Vector3 &p_force);
177 void apply_central_impulse(const Vector3 &p_impulse);
178 void apply_central_force(const Vector3 &p_force);
179 void apply_node_bias_impulse(uint32_t p_node_index, const Vector3 &p_impulse);
180
181 uint32_t get_face_count() const;
182 void get_face_points(uint32_t p_face_index, Vector3 &r_point_1, Vector3 &r_point_2, Vector3 &r_point_3) const;
183 Vector3 get_face_normal(uint32_t p_face_index) const;
184
185 void set_iteration_count(int p_val);
186 _FORCE_INLINE_ real_t get_iteration_count() const { return iteration_count; }
187
188 void set_total_mass(real_t p_val);
189 _FORCE_INLINE_ real_t get_total_mass() const { return total_mass; }
190 _FORCE_INLINE_ real_t get_total_inv_mass() const { return inv_total_mass; }
191
192 void set_collision_margin(real_t p_val);
193 _FORCE_INLINE_ real_t get_collision_margin() const { return collision_margin; }
194
195 void set_linear_stiffness(real_t p_val);
196 _FORCE_INLINE_ real_t get_linear_stiffness() const { return linear_stiffness; }
197
198 void set_shrinking_factor(real_t p_val);
199 _FORCE_INLINE_ real_t get_shrinking_factor() const { return shrinking_factor; }
200
201 void set_pressure_coefficient(real_t p_val);
202 _FORCE_INLINE_ real_t get_pressure_coefficient() const { return pressure_coefficient; }
203
204 void set_damping_coefficient(real_t p_val);
205 _FORCE_INLINE_ real_t get_damping_coefficient() const { return damping_coefficient; }
206
207 void set_drag_coefficient(real_t p_val);
208 _FORCE_INLINE_ real_t get_drag_coefficient() const { return drag_coefficient; }
209
210 void predict_motion(real_t p_delta);
211 void solve_constraints(real_t p_delta);
212
213 _FORCE_INLINE_ uint32_t get_node_index(void *p_node) const { return static_cast<Node *>(p_node)->index; }
214 _FORCE_INLINE_ uint32_t get_face_index(void *p_face) const { return static_cast<Face *>(p_face)->index; }
215
216 // Return true to stop the query.
217 // p_index is the node index for AABB query, face index for Ray query.
218 typedef bool (*QueryResultCallback)(uint32_t p_index, void *p_userdata);
219
220 void query_aabb(const AABB &p_aabb, QueryResultCallback p_result_callback, void *p_userdata);
221 void query_ray(const Vector3 &p_from, const Vector3 &p_to, QueryResultCallback p_result_callback, void *p_userdata);
222
223 protected:
224 virtual void _shapes_changed() override;
225
226 private:
227 void update_normals_and_centroids();
228 void update_bounds();
229 void update_constants();
230 void update_area();
231 void reset_link_rest_lengths();
232 void update_link_constants();
233
234 void apply_nodes_transform(const Transform3D &p_transform);
235
236 void add_velocity(const Vector3 &p_velocity);
237
238 void apply_forces(const LocalVector<GodotArea3D *> &p_wind_areas);
239
240 bool create_from_trimesh(const Vector<int> &p_indices, const Vector<Vector3> &p_vertices);
241 void generate_bending_constraints(int p_distance);
242 void reoptimize_link_order();
243 void append_link(uint32_t p_node1, uint32_t p_node2);
244 void append_face(uint32_t p_node1, uint32_t p_node2, uint32_t p_node3);
245
246 void solve_links(real_t kst, real_t ti);
247
248 void initialize_face_tree();
249 void update_face_tree(real_t p_delta);
250
251 void initialize_shape(bool p_force_move = true);
252 void deinitialize_shape();
253
254 void destroy();
255 };
256
257 class GodotSoftBodyShape3D : public GodotShape3D {
258 GodotSoftBody3D *soft_body = nullptr;
259
260 public:
261 GodotSoftBody3D *get_soft_body() const { return soft_body; }
262
263 virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_SOFT_BODY; }
264 virtual void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const override { r_min = r_max = 0.0; }
265 virtual Vector3 get_support(const Vector3 &p_normal) const override { return Vector3(); }
266 virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const override { r_amount = 0; }
267
268 virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal, int &r_face_index, bool p_hit_back_faces) const override;
269 virtual bool intersect_point(const Vector3 &p_point) const override;
270 virtual Vector3 get_closest_point_to(const Vector3 &p_point) const override;
271 virtual Vector3 get_moment_of_inertia(real_t p_mass) const override { return Vector3(); }
272
273 virtual void set_data(const Variant &p_data) override {}
274 virtual Variant get_data() const override { return Variant(); }
275
276 void update_bounds();
277
278 GodotSoftBodyShape3D(GodotSoftBody3D *p_soft_body);
279 ~GodotSoftBodyShape3D() {}
280 };