soft_body_bullet.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "space_bullet.h"
  34. #include "servers/rendering_server.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 rendering 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(RID p_mesh) {
  88. destroy_soft_body();
  89. soft_mesh = p_mesh;
  90. if (soft_mesh.is_null()) {
  91. return;
  92. }
  93. Array arrays = RenderingServer::get_singleton()->mesh_surface_get_arrays(soft_mesh, 0);
  94. ERR_FAIL_COND(arrays.is_empty());
  95. bool success = set_trimesh_body_shape(arrays[RS::ARRAY_INDEX], arrays[RS::ARRAY_VERTEX]);
  96. if (!success) {
  97. destroy_soft_body();
  98. }
  99. }
  100. void SoftBodyBullet::destroy_soft_body() {
  101. soft_mesh = RID();
  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 Transform3D &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 Transform3D &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 p_node_index, btScalar p_mass) {
  154. if (0 >= p_mass) {
  155. pin_node(p_node_index);
  156. } else {
  157. unpin_node(p_node_index);
  158. }
  159. if (bt_soft_body) {
  160. ERR_FAIL_INDEX(p_node_index, bt_soft_body->m_nodes.size());
  161. bt_soft_body->setMass(p_node_index, p_mass);
  162. }
  163. }
  164. btScalar SoftBodyBullet::get_node_mass(int p_node_index) const {
  165. if (bt_soft_body) {
  166. ERR_FAIL_INDEX_V(p_node_index, bt_soft_body->m_nodes.size(), 1);
  167. return bt_soft_body->getMass(p_node_index);
  168. } else {
  169. return -1 == search_node_pinned(p_node_index) ? 1 : 0;
  170. }
  171. }
  172. void SoftBodyBullet::reset_all_node_mass() {
  173. if (bt_soft_body) {
  174. for (int i = pinned_nodes.size() - 1; 0 <= i; --i) {
  175. bt_soft_body->setMass(pinned_nodes[i], 1);
  176. }
  177. }
  178. pinned_nodes.resize(0);
  179. }
  180. void SoftBodyBullet::reset_all_node_positions() {
  181. if (soft_mesh.is_null()) {
  182. return;
  183. }
  184. Array arrays = soft_mesh->surface_get_arrays(0);
  185. Vector<Vector3> vs_vertices(arrays[RS::ARRAY_VERTEX]);
  186. const Vector3 *vs_vertices_read = vs_vertices.ptr();
  187. for (int vertex_index = bt_soft_body->m_nodes.size() - 1; 0 <= vertex_index; --vertex_index) {
  188. G_TO_B(vs_vertices_read[indices_table[vertex_index][0]], bt_soft_body->m_nodes[vertex_index].m_x);
  189. bt_soft_body->m_nodes[vertex_index].m_q = bt_soft_body->m_nodes[vertex_index].m_x;
  190. bt_soft_body->m_nodes[vertex_index].m_v = btVector3(0, 0, 0);
  191. bt_soft_body->m_nodes[vertex_index].m_f = btVector3(0, 0, 0);
  192. }
  193. }
  194. void SoftBodyBullet::set_activation_state(bool p_active) {
  195. if (p_active) {
  196. bt_soft_body->setActivationState(ACTIVE_TAG);
  197. } else {
  198. bt_soft_body->setActivationState(WANTS_DEACTIVATION);
  199. }
  200. }
  201. void SoftBodyBullet::set_total_mass(real_t p_val) {
  202. if (0 >= p_val) {
  203. p_val = 1;
  204. }
  205. total_mass = p_val;
  206. if (bt_soft_body) {
  207. bt_soft_body->setTotalMass(total_mass);
  208. }
  209. }
  210. void SoftBodyBullet::set_linear_stiffness(real_t p_val) {
  211. linear_stiffness = p_val;
  212. if (bt_soft_body) {
  213. mat0->m_kLST = linear_stiffness;
  214. }
  215. }
  216. void SoftBodyBullet::set_simulation_precision(int p_val) {
  217. simulation_precision = p_val;
  218. if (bt_soft_body) {
  219. bt_soft_body->m_cfg.piterations = simulation_precision;
  220. bt_soft_body->m_cfg.viterations = simulation_precision;
  221. bt_soft_body->m_cfg.diterations = simulation_precision;
  222. bt_soft_body->m_cfg.citerations = simulation_precision;
  223. }
  224. }
  225. void SoftBodyBullet::set_pressure_coefficient(real_t p_val) {
  226. pressure_coefficient = p_val;
  227. if (bt_soft_body) {
  228. bt_soft_body->m_cfg.kPR = pressure_coefficient;
  229. }
  230. }
  231. void SoftBodyBullet::set_damping_coefficient(real_t p_val) {
  232. damping_coefficient = p_val;
  233. if (bt_soft_body) {
  234. bt_soft_body->m_cfg.kDP = damping_coefficient;
  235. }
  236. }
  237. void SoftBodyBullet::set_drag_coefficient(real_t p_val) {
  238. drag_coefficient = p_val;
  239. if (bt_soft_body) {
  240. bt_soft_body->m_cfg.kDG = drag_coefficient;
  241. }
  242. }
  243. bool SoftBodyBullet::set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector3> p_vertices) {
  244. ERR_FAIL_COND_V(p_indices.is_empty(), false);
  245. ERR_FAIL_COND_V(p_vertices.is_empty(), false);
  246. /// Parse rendering server indices to physical indices.
  247. /// Merge all overlapping vertices and create a map of physical vertices to rendering server
  248. {
  249. /// This is the map of rendering 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
  250. Vector<int> vs_indices_to_physics_table;
  251. { // Map vertices
  252. indices_table.resize(0);
  253. int index = 0;
  254. Map<Vector3, int> unique_vertices;
  255. const int vs_vertices_size(p_vertices.size());
  256. const Vector3 *p_vertices_read = p_vertices.ptr();
  257. for (int vs_vertex_index = 0; vs_vertex_index < vs_vertices_size; ++vs_vertex_index) {
  258. Map<Vector3, int>::Element *e = unique_vertices.find(p_vertices_read[vs_vertex_index]);
  259. int vertex_id;
  260. if (e) {
  261. // Already existing
  262. vertex_id = e->value();
  263. } else {
  264. // Create new one
  265. unique_vertices[p_vertices_read[vs_vertex_index]] = vertex_id = index++;
  266. indices_table.push_back(Vector<int>());
  267. }
  268. indices_table.write[vertex_id].push_back(vs_vertex_index);
  269. vs_indices_to_physics_table.push_back(vertex_id);
  270. }
  271. }
  272. const int indices_map_size(indices_table.size());
  273. Vector<btScalar> bt_vertices;
  274. { // Parse vertices to bullet
  275. bt_vertices.resize(indices_map_size * 3);
  276. const Vector3 *p_vertices_read = p_vertices.ptr();
  277. for (int i = 0; i < indices_map_size; ++i) {
  278. bt_vertices.write[3 * i + 0] = p_vertices_read[indices_table[i][0]].x;
  279. bt_vertices.write[3 * i + 1] = p_vertices_read[indices_table[i][0]].y;
  280. bt_vertices.write[3 * i + 2] = p_vertices_read[indices_table[i][0]].z;
  281. }
  282. }
  283. Vector<int> bt_triangles;
  284. const int triangles_size(p_indices.size() / 3);
  285. { // Parse indices
  286. bt_triangles.resize(triangles_size * 3);
  287. const int *p_indices_read = p_indices.ptr();
  288. for (int i = 0; i < triangles_size; ++i) {
  289. bt_triangles.write[3 * i + 0] = vs_indices_to_physics_table[p_indices_read[3 * i + 2]];
  290. bt_triangles.write[3 * i + 1] = vs_indices_to_physics_table[p_indices_read[3 * i + 1]];
  291. bt_triangles.write[3 * i + 2] = vs_indices_to_physics_table[p_indices_read[3 * i + 0]];
  292. }
  293. }
  294. btSoftBodyWorldInfo fake_world_info;
  295. bt_soft_body = btSoftBodyHelpers::CreateFromTriMesh(fake_world_info, &bt_vertices[0], &bt_triangles[0], triangles_size, false);
  296. setup_soft_body();
  297. }
  298. return true;
  299. }
  300. void SoftBodyBullet::setup_soft_body() {
  301. if (!bt_soft_body) {
  302. return;
  303. }
  304. // Soft body setup
  305. setupBulletCollisionObject(bt_soft_body);
  306. bt_soft_body->m_worldInfo = nullptr; // Remove fake world info
  307. bt_soft_body->getCollisionShape()->setMargin(0.01);
  308. bt_soft_body->setCollisionFlags(bt_soft_body->getCollisionFlags() & (~(btCollisionObject::CF_KINEMATIC_OBJECT | btCollisionObject::CF_STATIC_OBJECT)));
  309. // Space setup
  310. if (space) {
  311. space->add_soft_body(this);
  312. }
  313. mat0 = bt_soft_body->appendMaterial();
  314. // Assign soft body data
  315. bt_soft_body->generateBendingConstraints(2, mat0);
  316. mat0->m_kLST = linear_stiffness;
  317. // Clusters allow to have Soft vs Soft collision but doesn't work well right now
  318. //bt_soft_body->m_cfg.kSRHR_CL = 1;// Soft vs rigid hardness [0,1] (cluster only)
  319. //bt_soft_body->m_cfg.kSKHR_CL = 1;// Soft vs kinematic hardness [0,1] (cluster only)
  320. //bt_soft_body->m_cfg.kSSHR_CL = 1;// Soft vs soft hardness [0,1] (cluster only)
  321. //bt_soft_body->m_cfg.kSR_SPLT_CL = 1; // Soft vs rigid impulse split [0,1] (cluster only)
  322. //bt_soft_body->m_cfg.kSK_SPLT_CL = 1; // Soft vs kinematic impulse split [0,1] (cluster only)
  323. //bt_soft_body->m_cfg.kSS_SPLT_CL = 1; // Soft vs Soft impulse split [0,1] (cluster only)
  324. //bt_soft_body->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS + btSoftBody::fCollision::VF_SS;
  325. //bt_soft_body->generateClusters(64);
  326. bt_soft_body->m_cfg.piterations = simulation_precision;
  327. bt_soft_body->m_cfg.viterations = simulation_precision;
  328. bt_soft_body->m_cfg.diterations = simulation_precision;
  329. bt_soft_body->m_cfg.citerations = simulation_precision;
  330. bt_soft_body->m_cfg.kDP = damping_coefficient;
  331. bt_soft_body->m_cfg.kDG = drag_coefficient;
  332. bt_soft_body->m_cfg.kPR = pressure_coefficient;
  333. bt_soft_body->setTotalMass(total_mass);
  334. btSoftBodyHelpers::ReoptimizeLinkOrder(bt_soft_body);
  335. bt_soft_body->updateBounds();
  336. // Set pinned nodes
  337. for (int i = pinned_nodes.size() - 1; 0 <= i; --i) {
  338. const int node_index = pinned_nodes[i];
  339. ERR_CONTINUE(0 > node_index || bt_soft_body->m_nodes.size() <= node_index);
  340. bt_soft_body->setMass(node_index, 0);
  341. }
  342. }
  343. void SoftBodyBullet::pin_node(int p_node_index) {
  344. if (bt_soft_body) {
  345. ERR_FAIL_INDEX(p_node_index, bt_soft_body->m_nodes.size());
  346. }
  347. if (-1 == search_node_pinned(p_node_index)) {
  348. pinned_nodes.push_back(p_node_index);
  349. }
  350. }
  351. void SoftBodyBullet::unpin_node(int p_node_index) {
  352. if (bt_soft_body) {
  353. ERR_FAIL_INDEX(p_node_index, bt_soft_body->m_nodes.size());
  354. }
  355. const int id = search_node_pinned(p_node_index);
  356. if (-1 != id) {
  357. pinned_nodes.remove(id);
  358. }
  359. }
  360. int SoftBodyBullet::search_node_pinned(int p_node_index) const {
  361. for (int i = pinned_nodes.size() - 1; 0 <= i; --i) {
  362. if (p_node_index == pinned_nodes[i]) {
  363. return i;
  364. }
  365. }
  366. return -1;
  367. }