soft_body_bullet.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*************************************************************************/
  2. /* soft_body_bullet.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_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_3d.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 RenderingServerHandler;
  52. class SoftBodyBullet : public CollisionObjectBullet {
  53. private:
  54. btSoftBody *bt_soft_body = nullptr;
  55. Vector<Vector<int>> indices_table;
  56. btSoftBody::Material *mat0 = nullptr; // This is just a copy of pointer managed by btSoftBody
  57. bool isScratched = false;
  58. Ref<Mesh> soft_mesh;
  59. int simulation_precision = 5;
  60. real_t total_mass = 1.;
  61. real_t linear_stiffness = 0.5; // [0,1]
  62. real_t pressure_coefficient = 0.; // [-inf,+inf]
  63. real_t damping_coefficient = 0.01; // [0,1]
  64. real_t drag_coefficient = 0.; // [0,1]
  65. Vector<int> pinned_nodes;
  66. // Other property to add
  67. //btScalar kVC; // Volume conversation coefficient [0,+inf]
  68. //btScalar kDF; // Dynamic friction coefficient [0,1]
  69. //btScalar kMT; // Pose matching coefficient [0,1]
  70. //btScalar kCHR; // Rigid contacts hardness [0,1]
  71. //btScalar kKHR; // Kinetic contacts hardness [0,1]
  72. //btScalar kSHR; // Soft contacts hardness [0,1]
  73. public:
  74. SoftBodyBullet();
  75. ~SoftBodyBullet();
  76. virtual void reload_body();
  77. virtual void set_space(SpaceBullet *p_space);
  78. virtual void dispatch_callbacks() {}
  79. virtual void on_collision_filters_change() {}
  80. virtual void on_collision_checker_start() {}
  81. virtual void on_collision_checker_end() {}
  82. virtual void on_enter_area(AreaBullet *p_area);
  83. virtual void on_exit_area(AreaBullet *p_area);
  84. _FORCE_INLINE_ btSoftBody *get_bt_soft_body() const { return bt_soft_body; }
  85. void update_rendering_server(RenderingServerHandler *p_rendering_server_handler);
  86. void set_soft_mesh(const Ref<Mesh> &p_mesh);
  87. void destroy_soft_body();
  88. // Special function. This function has bad performance
  89. void set_soft_transform(const Transform &p_transform);
  90. AABB get_bounds() const;
  91. void move_all_nodes(const Transform &p_transform);
  92. void set_node_position(int node_index, const Vector3 &p_global_position);
  93. void set_node_position(int node_index, const btVector3 &p_global_position);
  94. void get_node_position(int node_index, Vector3 &r_position) const;
  95. void set_node_mass(int node_index, btScalar p_mass);
  96. btScalar get_node_mass(int node_index) const;
  97. void reset_all_node_mass();
  98. void reset_all_node_positions();
  99. void set_activation_state(bool p_active);
  100. void set_total_mass(real_t p_val);
  101. _FORCE_INLINE_ real_t get_total_mass() const { return total_mass; }
  102. void set_linear_stiffness(real_t p_val);
  103. _FORCE_INLINE_ real_t get_linear_stiffness() const { return linear_stiffness; }
  104. void set_simulation_precision(int p_val);
  105. _FORCE_INLINE_ int get_simulation_precision() const { return simulation_precision; }
  106. void set_pressure_coefficient(real_t p_val);
  107. _FORCE_INLINE_ real_t get_pressure_coefficient() const { return pressure_coefficient; }
  108. void set_damping_coefficient(real_t p_val);
  109. _FORCE_INLINE_ real_t get_damping_coefficient() const { return damping_coefficient; }
  110. void set_drag_coefficient(real_t p_val);
  111. _FORCE_INLINE_ real_t get_drag_coefficient() const { return drag_coefficient; }
  112. private:
  113. void set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector3> p_vertices);
  114. void setup_soft_body();
  115. void pin_node(int p_node_index);
  116. void unpin_node(int p_node_index);
  117. int search_node_pinned(int p_node_index) const;
  118. };
  119. #endif // SOFT_BODY_BULLET_H