soft_body_bullet.cpp 15 KB

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