soft_body_bullet.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*************************************************************************/
  2. /* soft_body_bullet.cpp */
  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. #include "soft_body_bullet.h"
  31. #include "bullet_types_converter.h"
  32. #include "bullet_utilities.h"
  33. #include "scene/3d/soft_body_3d.h"
  34. #include "space_bullet.h"
  35. SoftBodyBullet::SoftBodyBullet() :
  36. CollisionObjectBullet(CollisionObjectBullet::TYPE_SOFT_BODY) {}
  37. SoftBodyBullet::~SoftBodyBullet() {
  38. }
  39. void SoftBodyBullet::reload_body() {
  40. if (space) {
  41. space->remove_soft_body(this);
  42. space->add_soft_body(this);
  43. }
  44. }
  45. void SoftBodyBullet::set_space(SpaceBullet *p_space) {
  46. if (space) {
  47. isScratched = false;
  48. space->remove_soft_body(this);
  49. }
  50. space = p_space;
  51. if (space) {
  52. space->add_soft_body(this);
  53. }
  54. }
  55. void SoftBodyBullet::on_enter_area(AreaBullet *p_area) {}
  56. void SoftBodyBullet::on_exit_area(AreaBullet *p_area) {}
  57. void SoftBodyBullet::update_rendering_server(RenderingServerHandler *p_rendering_server_handler) {
  58. if (!bt_soft_body) {
  59. return;
  60. }
  61. /// Update visual server vertices
  62. const btSoftBody::tNodeArray &nodes(bt_soft_body->m_nodes);
  63. const int nodes_count = nodes.size();
  64. const Vector<int> *vs_indices;
  65. const void *vertex_position;
  66. const void *vertex_normal;
  67. for (int vertex_index = 0; vertex_index < nodes_count; ++vertex_index) {
  68. vertex_position = reinterpret_cast<const void *>(&nodes[vertex_index].m_x);
  69. vertex_normal = reinterpret_cast<const void *>(&nodes[vertex_index].m_n);
  70. vs_indices = &indices_table[vertex_index];
  71. const int vs_indices_size(vs_indices->size());
  72. for (int x = 0; x < vs_indices_size; ++x) {
  73. p_rendering_server_handler->set_vertex((*vs_indices)[x], vertex_position);
  74. p_rendering_server_handler->set_normal((*vs_indices)[x], vertex_normal);
  75. }
  76. }
  77. /// Generate AABB
  78. btVector3 aabb_min;
  79. btVector3 aabb_max;
  80. bt_soft_body->getAabb(aabb_min, aabb_max);
  81. btVector3 size(aabb_max - aabb_min);
  82. AABB aabb;
  83. B_TO_G(aabb_min, aabb.position);
  84. B_TO_G(size, aabb.size);
  85. p_rendering_server_handler->set_aabb(aabb);
  86. }
  87. void SoftBodyBullet::set_soft_mesh(const Ref<Mesh> &p_mesh) {
  88. if (p_mesh.is_null()) {
  89. soft_mesh.unref();
  90. } else {
  91. soft_mesh = p_mesh;
  92. }
  93. if (soft_mesh.is_null()) {
  94. destroy_soft_body();
  95. return;
  96. }
  97. Array arrays = soft_mesh->surface_get_arrays(0);
  98. ERR_FAIL_COND(!(soft_mesh->surface_get_format(0) & RS::ARRAY_FORMAT_INDEX));
  99. set_trimesh_body_shape(arrays[RS::ARRAY_INDEX], arrays[RS::ARRAY_VERTEX]);
  100. }
  101. void SoftBodyBullet::destroy_soft_body() {
  102. if (!bt_soft_body) {
  103. return;
  104. }
  105. if (space) {
  106. /// Remove from world before deletion
  107. space->remove_soft_body(this);
  108. }
  109. destroyBulletCollisionObject();
  110. bt_soft_body = nullptr;
  111. }
  112. void SoftBodyBullet::set_soft_transform(const Transform &p_transform) {
  113. reset_all_node_positions();
  114. move_all_nodes(p_transform);
  115. }
  116. AABB SoftBodyBullet::get_bounds() const {
  117. if (!bt_soft_body) {
  118. return AABB();
  119. }
  120. btVector3 aabb_min;
  121. btVector3 aabb_max;
  122. bt_soft_body->getAabb(aabb_min, aabb_max);
  123. btVector3 size(aabb_max - aabb_min);
  124. AABB aabb;
  125. B_TO_G(aabb_min, aabb.position);
  126. B_TO_G(size, aabb.size);
  127. return aabb;
  128. }
  129. void SoftBodyBullet::move_all_nodes(const Transform &p_transform) {
  130. if (!bt_soft_body) {
  131. return;
  132. }
  133. btTransform bt_transf;
  134. G_TO_B(p_transform, bt_transf);
  135. bt_soft_body->transform(bt_transf);
  136. }
  137. void SoftBodyBullet::set_node_position(int p_node_index, const Vector3 &p_global_position) {
  138. btVector3 bt_pos;
  139. G_TO_B(p_global_position, bt_pos);
  140. set_node_position(p_node_index, bt_pos);
  141. }
  142. void SoftBodyBullet::set_node_position(int p_node_index, const btVector3 &p_global_position) {
  143. if (bt_soft_body) {
  144. bt_soft_body->m_nodes[p_node_index].m_q = bt_soft_body->m_nodes[p_node_index].m_x;
  145. bt_soft_body->m_nodes[p_node_index].m_x = p_global_position;
  146. }
  147. }
  148. void SoftBodyBullet::get_node_position(int p_node_index, Vector3 &r_position) const {
  149. if (bt_soft_body) {
  150. B_TO_G(bt_soft_body->m_nodes[p_node_index].m_x, r_position);
  151. }
  152. }
  153. void SoftBodyBullet::set_node_mass(int node_index, btScalar p_mass) {
  154. if (0 >= p_mass) {
  155. pin_node(node_index);
  156. } else {
  157. unpin_node(node_index);
  158. }
  159. if (bt_soft_body) {
  160. bt_soft_body->setMass(node_index, p_mass);
  161. }
  162. }
  163. btScalar SoftBodyBullet::get_node_mass(int node_index) const {
  164. if (bt_soft_body) {
  165. return bt_soft_body->getMass(node_index);
  166. } else {
  167. return -1 == search_node_pinned(node_index) ? 1 : 0;
  168. }
  169. }
  170. void SoftBodyBullet::reset_all_node_mass() {
  171. if (bt_soft_body) {
  172. for (int i = pinned_nodes.size() - 1; 0 <= i; --i) {
  173. bt_soft_body->setMass(pinned_nodes[i], 1);
  174. }
  175. }
  176. pinned_nodes.resize(0);
  177. }
  178. void SoftBodyBullet::reset_all_node_positions() {
  179. if (soft_mesh.is_null()) {
  180. return;
  181. }
  182. Array arrays = soft_mesh->surface_get_arrays(0);
  183. Vector<Vector3> vs_vertices(arrays[RS::ARRAY_VERTEX]);
  184. const Vector3 *vs_vertices_read = vs_vertices.ptr();
  185. for (int vertex_index = bt_soft_body->m_nodes.size() - 1; 0 <= vertex_index; --vertex_index) {
  186. G_TO_B(vs_vertices_read[indices_table[vertex_index][0]], bt_soft_body->m_nodes[vertex_index].m_x);
  187. bt_soft_body->m_nodes[vertex_index].m_q = bt_soft_body->m_nodes[vertex_index].m_x;
  188. bt_soft_body->m_nodes[vertex_index].m_v = btVector3(0, 0, 0);
  189. bt_soft_body->m_nodes[vertex_index].m_f = btVector3(0, 0, 0);
  190. }
  191. }
  192. void SoftBodyBullet::set_activation_state(bool p_active) {
  193. if (p_active) {
  194. bt_soft_body->setActivationState(ACTIVE_TAG);
  195. } else {
  196. bt_soft_body->setActivationState(WANTS_DEACTIVATION);
  197. }
  198. }
  199. void SoftBodyBullet::set_total_mass(real_t p_val) {
  200. if (0 >= p_val) {
  201. p_val = 1;
  202. }
  203. total_mass = p_val;
  204. if (bt_soft_body) {
  205. bt_soft_body->setTotalMass(total_mass);
  206. }
  207. }
  208. void SoftBodyBullet::set_linear_stiffness(real_t p_val) {
  209. linear_stiffness = p_val;
  210. if (bt_soft_body) {
  211. mat0->m_kLST = linear_stiffness;
  212. }
  213. }
  214. void SoftBodyBullet::set_simulation_precision(int p_val) {
  215. simulation_precision = p_val;
  216. if (bt_soft_body) {
  217. bt_soft_body->m_cfg.piterations = simulation_precision;
  218. bt_soft_body->m_cfg.viterations = simulation_precision;
  219. bt_soft_body->m_cfg.diterations = simulation_precision;
  220. bt_soft_body->m_cfg.citerations = simulation_precision;
  221. }
  222. }
  223. void SoftBodyBullet::set_pressure_coefficient(real_t p_val) {
  224. pressure_coefficient = p_val;
  225. if (bt_soft_body) {
  226. bt_soft_body->m_cfg.kPR = pressure_coefficient;
  227. }
  228. }
  229. void SoftBodyBullet::set_damping_coefficient(real_t p_val) {
  230. damping_coefficient = p_val;
  231. if (bt_soft_body) {
  232. bt_soft_body->m_cfg.kDP = damping_coefficient;
  233. }
  234. }
  235. void SoftBodyBullet::set_drag_coefficient(real_t p_val) {
  236. drag_coefficient = p_val;
  237. if (bt_soft_body) {
  238. bt_soft_body->m_cfg.kDG = drag_coefficient;
  239. }
  240. }
  241. void SoftBodyBullet::set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector3> p_vertices) {
  242. /// Assert the current soft body is destroyed
  243. destroy_soft_body();
  244. /// Parse visual server indices to physical indices.
  245. /// Merge all overlapping vertices and create a map of physical vertices to visual server
  246. {
  247. /// This is the map of visual server indices to physics indices (So it's the inverse of idices_map), Thanks to it I don't need make a heavy search in the indices_map
  248. Vector<int> vs_indices_to_physics_table;
  249. { // Map vertices
  250. indices_table.resize(0);
  251. int index = 0;
  252. Map<Vector3, int> unique_vertices;
  253. const int vs_vertices_size(p_vertices.size());
  254. const Vector3 *p_vertices_read = p_vertices.ptr();
  255. for (int vs_vertex_index = 0; vs_vertex_index < vs_vertices_size; ++vs_vertex_index) {
  256. Map<Vector3, int>::Element *e = unique_vertices.find(p_vertices_read[vs_vertex_index]);
  257. int vertex_id;
  258. if (e) {
  259. // Already existing
  260. vertex_id = e->value();
  261. } else {
  262. // Create new one
  263. unique_vertices[p_vertices_read[vs_vertex_index]] = vertex_id = index++;
  264. indices_table.push_back(Vector<int>());
  265. }
  266. indices_table.write[vertex_id].push_back(vs_vertex_index);
  267. vs_indices_to_physics_table.push_back(vertex_id);
  268. }
  269. }
  270. const int indices_map_size(indices_table.size());
  271. Vector<btScalar> bt_vertices;
  272. { // Parse vertices to bullet
  273. bt_vertices.resize(indices_map_size * 3);
  274. const Vector3 *p_vertices_read = p_vertices.ptr();
  275. for (int i = 0; i < indices_map_size; ++i) {
  276. bt_vertices.write[3 * i + 0] = p_vertices_read[indices_table[i][0]].x;
  277. bt_vertices.write[3 * i + 1] = p_vertices_read[indices_table[i][0]].y;
  278. bt_vertices.write[3 * i + 2] = p_vertices_read[indices_table[i][0]].z;
  279. }
  280. }
  281. Vector<int> bt_triangles;
  282. const int triangles_size(p_indices.size() / 3);
  283. { // Parse indices
  284. bt_triangles.resize(triangles_size * 3);
  285. const int *p_indices_read = p_indices.ptr();
  286. for (int i = 0; i < triangles_size; ++i) {
  287. bt_triangles.write[3 * i + 0] = vs_indices_to_physics_table[p_indices_read[3 * i + 2]];
  288. bt_triangles.write[3 * i + 1] = vs_indices_to_physics_table[p_indices_read[3 * i + 1]];
  289. bt_triangles.write[3 * i + 2] = vs_indices_to_physics_table[p_indices_read[3 * i + 0]];
  290. }
  291. }
  292. btSoftBodyWorldInfo fake_world_info;
  293. bt_soft_body = btSoftBodyHelpers::CreateFromTriMesh(fake_world_info, &bt_vertices[0], &bt_triangles[0], triangles_size, false);
  294. setup_soft_body();
  295. }
  296. }
  297. void SoftBodyBullet::setup_soft_body() {
  298. if (!bt_soft_body) {
  299. return;
  300. }
  301. // Soft body setup
  302. setupBulletCollisionObject(bt_soft_body);
  303. bt_soft_body->m_worldInfo = nullptr; // Remove fake world info
  304. bt_soft_body->getCollisionShape()->setMargin(0.01);
  305. bt_soft_body->setCollisionFlags(bt_soft_body->getCollisionFlags() & (~(btCollisionObject::CF_KINEMATIC_OBJECT | btCollisionObject::CF_STATIC_OBJECT)));
  306. // Space setup
  307. if (space) {
  308. space->add_soft_body(this);
  309. }
  310. mat0 = bt_soft_body->appendMaterial();
  311. // Assign soft body data
  312. bt_soft_body->generateBendingConstraints(2, mat0);
  313. mat0->m_kLST = linear_stiffness;
  314. // Clusters allow to have Soft vs Soft collision but doesn't work well right now
  315. //bt_soft_body->m_cfg.kSRHR_CL = 1;// Soft vs rigid hardness [0,1] (cluster only)
  316. //bt_soft_body->m_cfg.kSKHR_CL = 1;// Soft vs kinematic hardness [0,1] (cluster only)
  317. //bt_soft_body->m_cfg.kSSHR_CL = 1;// Soft vs soft hardness [0,1] (cluster only)
  318. //bt_soft_body->m_cfg.kSR_SPLT_CL = 1; // Soft vs rigid impulse split [0,1] (cluster only)
  319. //bt_soft_body->m_cfg.kSK_SPLT_CL = 1; // Soft vs kinematic impulse split [0,1] (cluster only)
  320. //bt_soft_body->m_cfg.kSS_SPLT_CL = 1; // Soft vs Soft impulse split [0,1] (cluster only)
  321. //bt_soft_body->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS + btSoftBody::fCollision::VF_SS;
  322. //bt_soft_body->generateClusters(64);
  323. bt_soft_body->m_cfg.piterations = simulation_precision;
  324. bt_soft_body->m_cfg.viterations = simulation_precision;
  325. bt_soft_body->m_cfg.diterations = simulation_precision;
  326. bt_soft_body->m_cfg.citerations = simulation_precision;
  327. bt_soft_body->m_cfg.kDP = damping_coefficient;
  328. bt_soft_body->m_cfg.kDG = drag_coefficient;
  329. bt_soft_body->m_cfg.kPR = pressure_coefficient;
  330. bt_soft_body->setTotalMass(total_mass);
  331. btSoftBodyHelpers::ReoptimizeLinkOrder(bt_soft_body);
  332. bt_soft_body->updateBounds();
  333. // Set pinned nodes
  334. for (int i = pinned_nodes.size() - 1; 0 <= i; --i) {
  335. bt_soft_body->setMass(pinned_nodes[i], 0);
  336. }
  337. }
  338. void SoftBodyBullet::pin_node(int p_node_index) {
  339. if (-1 == search_node_pinned(p_node_index)) {
  340. pinned_nodes.push_back(p_node_index);
  341. }
  342. }
  343. void SoftBodyBullet::unpin_node(int p_node_index) {
  344. const int id = search_node_pinned(p_node_index);
  345. if (-1 != id) {
  346. pinned_nodes.remove(id);
  347. }
  348. }
  349. int SoftBodyBullet::search_node_pinned(int p_node_index) const {
  350. for (int i = pinned_nodes.size() - 1; 0 <= i; --i) {
  351. if (p_node_index == pinned_nodes[i]) {
  352. return i;
  353. }
  354. }
  355. return -1;
  356. }