soft_body_bullet.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**************************************************************************/
  2. /* soft_body_bullet.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_BULLET_H
  31. #define SOFT_BODY_BULLET_H
  32. #include "collision_object_bullet.h"
  33. #include "scene/resources/material.h" // TODO remove this please
  34. #ifdef None
  35. /// This is required to remove the macro None defined by x11 compiler because this word "None" is used internally by Bullet
  36. #undef None
  37. #define x11_None 0L
  38. #endif
  39. #include "BulletSoftBody/btSoftBodyHelpers.h"
  40. #include "collision_object_bullet.h"
  41. #include "scene/resources/mesh.h"
  42. #include "servers/physics_server.h"
  43. #ifdef x11_None
  44. /// This is required to re add the macro None defined by x11 compiler
  45. #undef x11_None
  46. #define None 0L
  47. #endif
  48. /**
  49. @author AndreaCatania
  50. */
  51. class SoftBodyBullet : public CollisionObjectBullet {
  52. private:
  53. btSoftBody *bt_soft_body;
  54. Vector<Vector<int>> indices_table;
  55. btSoftBody::Material *mat0; // This is just a copy of pointer managed by btSoftBody
  56. bool isScratched;
  57. Ref<Mesh> soft_mesh;
  58. int simulation_precision;
  59. real_t total_mass;
  60. real_t linear_stiffness; // [0,1]
  61. real_t areaAngular_stiffness; // [0,1]
  62. real_t volume_stiffness; // [0,1]
  63. real_t pressure_coefficient; // [-inf,+inf]
  64. real_t pose_matching_coefficient; // [0,1]
  65. real_t damping_coefficient; // [0,1]
  66. real_t drag_coefficient; // [0,1]
  67. Vector<int> pinned_nodes;
  68. // Other property to add
  69. //btScalar kVC; // Volume conversation coefficient [0,+inf]
  70. //btScalar kDF; // Dynamic friction coefficient [0,1]
  71. //btScalar kMT; // Pose matching coefficient [0,1]
  72. //btScalar kCHR; // Rigid contacts hardness [0,1]
  73. //btScalar kKHR; // Kinetic contacts hardness [0,1]
  74. //btScalar kSHR; // Soft contacts hardness [0,1]
  75. public:
  76. SoftBodyBullet();
  77. ~SoftBodyBullet();
  78. virtual void reload_body();
  79. virtual void set_space(SpaceBullet *p_space);
  80. virtual void dispatch_callbacks() {}
  81. virtual void on_collision_filters_change() {}
  82. virtual void on_collision_checker_start() {}
  83. virtual void on_collision_checker_end() {}
  84. virtual void on_enter_area(AreaBullet *p_area);
  85. virtual void on_exit_area(AreaBullet *p_area);
  86. _FORCE_INLINE_ btSoftBody *get_bt_soft_body() const { return bt_soft_body; }
  87. void update_visual_server(class SoftBodyVisualServerHandler *p_visual_server_handler);
  88. void set_soft_mesh(const Ref<Mesh> &p_mesh);
  89. void destroy_soft_body();
  90. // Special function. This function has bad performance
  91. void set_soft_transform(const Transform &p_transform);
  92. void move_all_nodes(const Transform &p_transform);
  93. void set_node_position(int node_index, const Vector3 &p_global_position);
  94. void set_node_position(int node_index, const btVector3 &p_global_position);
  95. void get_node_position(int node_index, Vector3 &r_position) const;
  96. // Heavy function, Please cache this info
  97. void get_node_offset(int node_index, Vector3 &r_offset) const;
  98. // Heavy function, Please cache this info
  99. void get_node_offset(int node_index, btVector3 &r_offset) const;
  100. void set_node_mass(int node_index, btScalar p_mass);
  101. btScalar get_node_mass(int node_index) const;
  102. void reset_all_node_mass();
  103. void reset_all_node_positions();
  104. void set_activation_state(bool p_active);
  105. void set_total_mass(real_t p_val);
  106. _FORCE_INLINE_ real_t get_total_mass() const { return total_mass; }
  107. void set_linear_stiffness(real_t p_val);
  108. _FORCE_INLINE_ real_t get_linear_stiffness() const { return linear_stiffness; }
  109. void set_areaAngular_stiffness(real_t p_val);
  110. _FORCE_INLINE_ real_t get_areaAngular_stiffness() const { return areaAngular_stiffness; }
  111. void set_volume_stiffness(real_t p_val);
  112. _FORCE_INLINE_ real_t get_volume_stiffness() const { return volume_stiffness; }
  113. void set_simulation_precision(int p_val);
  114. _FORCE_INLINE_ int get_simulation_precision() const { return simulation_precision; }
  115. void set_pressure_coefficient(real_t p_val);
  116. _FORCE_INLINE_ real_t get_pressure_coefficient() const { return pressure_coefficient; }
  117. void set_pose_matching_coefficient(real_t p_val);
  118. _FORCE_INLINE_ real_t get_pose_matching_coefficient() const { return pose_matching_coefficient; }
  119. void set_damping_coefficient(real_t p_val);
  120. _FORCE_INLINE_ real_t get_damping_coefficient() const { return damping_coefficient; }
  121. void set_drag_coefficient(real_t p_val);
  122. _FORCE_INLINE_ real_t get_drag_coefficient() const { return drag_coefficient; }
  123. private:
  124. void set_trimesh_body_shape(PoolVector<int> p_indices, PoolVector<Vector3> p_vertices);
  125. void setup_soft_body();
  126. void pin_node(int p_node_index);
  127. void unpin_node(int p_node_index);
  128. int search_node_pinned(int p_node_index) const;
  129. };
  130. #endif // SOFT_BODY_BULLET_H