soft_body.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*************************************************************************/
  2. /* soft_body.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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_PHYSICS_BODY_H
  31. #define SOFT_PHYSICS_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() { return mesh.is_valid(); }
  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. };
  67. private:
  68. SoftBodyVisualServerHandler visual_server_handler;
  69. RID physics_rid;
  70. bool mesh_owner;
  71. uint32_t collision_mask;
  72. uint32_t collision_layer;
  73. NodePath parent_collision_ignore;
  74. PoolVector<PinnedPoint> pinned_points;
  75. bool simulation_started;
  76. bool pinned_points_cache_dirty;
  77. Ref<ArrayMesh> debug_mesh_cache;
  78. class MeshInstance *debug_mesh;
  79. bool capture_input_on_drag;
  80. bool ray_pickable;
  81. void _update_pickable();
  82. protected:
  83. bool _set(const StringName &p_name, const Variant &p_value);
  84. bool _get(const StringName &p_name, Variant &r_ret) const;
  85. void _get_property_list(List<PropertyInfo> *p_list) const;
  86. bool _set_property_pinned_points_indices(const Array &p_indices);
  87. bool _set_property_pinned_points_attachment(int p_item, const String &p_what, const Variant &p_value);
  88. bool _get_property_pinned_points(int p_item, const String &p_what, Variant &r_ret) const;
  89. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  90. void _notification(int p_what);
  91. static void _bind_methods();
  92. virtual String get_configuration_warning() const;
  93. protected:
  94. void _draw_soft_mesh();
  95. public:
  96. void update_physics_server();
  97. void become_mesh_owner();
  98. void set_collision_mask(uint32_t p_mask);
  99. uint32_t get_collision_mask() const;
  100. void set_collision_layer(uint32_t p_layer);
  101. uint32_t get_collision_layer() const;
  102. void set_collision_mask_bit(int p_bit, bool p_value);
  103. bool get_collision_mask_bit(int p_bit) const;
  104. void set_collision_layer_bit(int p_bit, bool p_value);
  105. bool get_collision_layer_bit(int p_bit) const;
  106. void set_parent_collision_ignore(const NodePath &p_parent_collision_ignore);
  107. const NodePath &get_parent_collision_ignore() const;
  108. void set_pinned_points_indices(PoolVector<PinnedPoint> p_pinned_points_indices);
  109. PoolVector<PinnedPoint> get_pinned_points_indices();
  110. void set_simulation_precision(int p_simulation_precision);
  111. int get_simulation_precision();
  112. void set_total_mass(real_t p_total_mass);
  113. real_t get_total_mass();
  114. void set_linear_stiffness(real_t p_linear_stiffness);
  115. real_t get_linear_stiffness();
  116. void set_areaAngular_stiffness(real_t p_areaAngular_stiffness);
  117. real_t get_areaAngular_stiffness();
  118. void set_volume_stiffness(real_t p_volume_stiffness);
  119. real_t get_volume_stiffness();
  120. void set_pressure_coefficient(real_t p_pressure_coefficient);
  121. real_t get_pressure_coefficient();
  122. void set_pose_matching_coefficient(real_t p_pose_matching_coefficient);
  123. real_t get_pose_matching_coefficient();
  124. void set_damping_coefficient(real_t p_damping_coefficient);
  125. real_t get_damping_coefficient();
  126. void set_drag_coefficient(real_t p_drag_coefficient);
  127. real_t get_drag_coefficient();
  128. Array get_collision_exceptions();
  129. void add_collision_exception_with(Node *p_node);
  130. void remove_collision_exception_with(Node *p_node);
  131. Vector3 get_point_transform(int p_point_index);
  132. void pin_point_toggle(int p_point_index);
  133. void pin_point(int p_point_index, bool pin, const NodePath &p_spatial_attachment_path = NodePath());
  134. bool is_point_pinned(int p_point_index) const;
  135. void set_ray_pickable(bool p_ray_pickable);
  136. bool is_ray_pickable() const;
  137. SoftBody();
  138. ~SoftBody();
  139. private:
  140. void reset_softbody_pin();
  141. void _make_cache_dirty();
  142. void _update_cache_pin_points_datas();
  143. void _pin_point_on_physics_server(int p_point_index, bool pin);
  144. void _add_pinned_point(int p_point_index, const NodePath &p_spatial_attachment_path);
  145. void _reset_points_offsets();
  146. void _remove_pinned_point(int p_point_index);
  147. int _get_pinned_point(int p_point_index, PinnedPoint *&r_point) const;
  148. int _has_pinned_point(int p_point_index) const;
  149. };
  150. #endif // SOFT_PHYSICS_BODY_H