godot_soft_body_3d.h 11 KB

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