soft_body_bullet.cpp 14 KB

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