soft_body_bullet.cpp 15 KB

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