soft_body.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**************************************************************************/
  2. /* soft_body.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 SOFT_BODY_H
  31. #define SOFT_BODY_H
  32. #include "scene/3d/mesh_instance.h"
  33. class SoftBody;
  34. class SoftBodyVisualServerHandler {
  35. friend class SoftBody;
  36. RID mesh;
  37. int surface;
  38. PoolVector<uint8_t> buffer;
  39. uint32_t stride;
  40. uint32_t offset_vertices;
  41. uint32_t offset_normal;
  42. PoolVector<uint8_t>::Write write_buffer;
  43. private:
  44. SoftBodyVisualServerHandler();
  45. bool is_ready(RID p_mesh_rid) const { return mesh.is_valid() && mesh == p_mesh_rid; }
  46. void prepare(RID p_mesh_rid, int p_surface);
  47. void clear();
  48. void open();
  49. void close();
  50. void commit_changes();
  51. public:
  52. void set_vertex(int p_vertex_id, const void *p_vector3);
  53. void set_normal(int p_vertex_id, const void *p_vector3);
  54. void set_aabb(const AABB &p_aabb);
  55. };
  56. class SoftBody : public MeshInstance {
  57. GDCLASS(SoftBody, MeshInstance);
  58. public:
  59. struct PinnedPoint {
  60. int point_index;
  61. NodePath spatial_attachment_path;
  62. Spatial *spatial_attachment; // Cache
  63. Vector3 offset;
  64. PinnedPoint();
  65. PinnedPoint(const PinnedPoint &obj_tocopy);
  66. PinnedPoint operator=(const PinnedPoint &obj);
  67. };
  68. private:
  69. SoftBodyVisualServerHandler visual_server_handler;
  70. RID physics_rid;
  71. bool physics_enabled = true;
  72. RID owned_mesh;
  73. uint32_t collision_mask;
  74. uint32_t collision_layer;
  75. NodePath parent_collision_ignore;
  76. PoolVector<PinnedPoint> pinned_points;
  77. bool simulation_started;
  78. bool pinned_points_cache_dirty;
  79. Ref<ArrayMesh> debug_mesh_cache;
  80. class MeshInstance *debug_mesh;
  81. bool capture_input_on_drag;
  82. bool ray_pickable;
  83. void _update_pickable();
  84. void _update_physics_server();
  85. void _draw_soft_mesh();
  86. void _prepare_physics_server();
  87. void _become_mesh_owner();
  88. protected:
  89. bool _set(const StringName &p_name, const Variant &p_value);
  90. bool _get(const StringName &p_name, Variant &r_ret) const;
  91. void _get_property_list(List<PropertyInfo> *p_list) const;
  92. bool _set_property_pinned_points_indices(const Array &p_indices);
  93. bool _set_property_pinned_points_attachment(int p_item, const String &p_what, const Variant &p_value);
  94. bool _get_property_pinned_points(int p_item, const String &p_what, Variant &r_ret) const;
  95. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  96. void _notification(int p_what);
  97. static void _bind_methods();
  98. virtual String get_configuration_warning() const;
  99. public:
  100. void set_collision_mask(uint32_t p_mask);
  101. uint32_t get_collision_mask() const;
  102. void set_collision_layer(uint32_t p_layer);
  103. uint32_t get_collision_layer() const;
  104. void set_collision_mask_bit(int p_bit, bool p_value);
  105. bool get_collision_mask_bit(int p_bit) const;
  106. void set_collision_layer_bit(int p_bit, bool p_value);
  107. bool get_collision_layer_bit(int p_bit) const;
  108. void set_parent_collision_ignore(const NodePath &p_parent_collision_ignore);
  109. const NodePath &get_parent_collision_ignore() const;
  110. void set_physics_enabled(bool p_enabled);
  111. bool is_physics_enabled() const;
  112. void set_pinned_points_indices(PoolVector<PinnedPoint> p_pinned_points_indices);
  113. PoolVector<PinnedPoint> get_pinned_points_indices();
  114. void set_simulation_precision(int p_simulation_precision);
  115. int get_simulation_precision();
  116. void set_total_mass(real_t p_total_mass);
  117. real_t get_total_mass();
  118. void set_linear_stiffness(real_t p_linear_stiffness);
  119. real_t get_linear_stiffness();
  120. void set_areaAngular_stiffness(real_t p_areaAngular_stiffness);
  121. real_t get_areaAngular_stiffness();
  122. void set_volume_stiffness(real_t p_volume_stiffness);
  123. real_t get_volume_stiffness();
  124. void set_pressure_coefficient(real_t p_pressure_coefficient);
  125. real_t get_pressure_coefficient();
  126. void set_pose_matching_coefficient(real_t p_pose_matching_coefficient);
  127. real_t get_pose_matching_coefficient();
  128. void set_damping_coefficient(real_t p_damping_coefficient);
  129. real_t get_damping_coefficient();
  130. void set_drag_coefficient(real_t p_drag_coefficient);
  131. real_t get_drag_coefficient();
  132. Array get_collision_exceptions();
  133. void add_collision_exception_with(Node *p_node);
  134. void remove_collision_exception_with(Node *p_node);
  135. Vector3 get_point_transform(int p_point_index);
  136. void pin_point_toggle(int p_point_index);
  137. void pin_point(int p_point_index, bool pin, const NodePath &p_spatial_attachment_path = NodePath());
  138. bool is_point_pinned(int p_point_index) const;
  139. void set_ray_pickable(bool p_ray_pickable);
  140. bool is_ray_pickable() const;
  141. SoftBody();
  142. ~SoftBody();
  143. private:
  144. void reset_softbody_pin();
  145. void _make_cache_dirty();
  146. void _update_cache_pin_points_datas();
  147. void _pin_point_on_physics_server(int p_point_index, bool pin);
  148. void _add_pinned_point(int p_point_index, const NodePath &p_spatial_attachment_path);
  149. void _reset_points_offsets();
  150. void _remove_pinned_point(int p_point_index);
  151. int _get_pinned_point(int p_point_index, PinnedPoint *&r_point) const;
  152. int _has_pinned_point(int p_point_index) const;
  153. };
  154. #endif // SOFT_BODY_H