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