From cca220eb271630cbb914e728db0d42023721ba78 Mon Sep 17 00:00:00 2001 From: Xuchen Han Date: Wed, 18 Sep 2019 16:06:09 -0700 Subject: [PATCH] add damping energy to mass spring --- examples/DeformableDemo/DeformableContact.cpp | 10 ++--- examples/DeformableDemo/DeformableRigid.cpp | 2 +- examples/DeformableDemo/GraspDeformable.cpp | 16 ++++---- .../btDeformableBackwardEulerObjective.h | 2 + .../btDeformableMassSpringForce.h | 38 +++++++++++++++++-- 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/examples/DeformableDemo/DeformableContact.cpp b/examples/DeformableDemo/DeformableContact.cpp index 381bd130b..ce12b8b12 100644 --- a/examples/DeformableDemo/DeformableContact.cpp +++ b/examples/DeformableDemo/DeformableContact.cpp @@ -133,7 +133,7 @@ void DeformableContact::initPhysics() btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, groundShape, localInertia); btRigidBody* body = new btRigidBody(rbInfo); - body->setFriction(1); + body->setFriction(2); //add the ground to the dynamics world m_dynamicsWorld->addRigidBody(body); @@ -156,12 +156,12 @@ void DeformableContact::initPhysics() psb->setTotalMass(1); psb->m_cfg.kKHR = 1; // collision hardness with kinematic objects psb->m_cfg.kCHR = 1; // collision hardness with rigid body - psb->m_cfg.kDF = 2; + psb->m_cfg.kDF = 1; psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RD; psb->m_cfg.collisions |= btSoftBody::fCollision::VF_DD; getDeformableDynamicsWorld()->addSoftBody(psb); - btDeformableMassSpringForce* mass_spring = new btDeformableMassSpringForce(20,0.1, true); + btDeformableMassSpringForce* mass_spring = new btDeformableMassSpringForce(10,1, true); getDeformableDynamicsWorld()->addForce(psb, mass_spring); m_forces.push_back(mass_spring); @@ -183,13 +183,13 @@ void DeformableContact::initPhysics() psb2->setTotalMass(1); psb2->m_cfg.kKHR = 1; // collision hardness with kinematic objects psb2->m_cfg.kCHR = 1; // collision hardness with rigid body - psb2->m_cfg.kDF = 2; + psb2->m_cfg.kDF = 1; psb2->m_cfg.collisions = btSoftBody::fCollision::SDF_RD; psb2->m_cfg.collisions |= btSoftBody::fCollision::VF_DD; psb->translate(btVector3(3,0,0)); getDeformableDynamicsWorld()->addSoftBody(psb2); - btDeformableMassSpringForce* mass_spring2 = new btDeformableMassSpringForce(20,0.1, true); + btDeformableMassSpringForce* mass_spring2 = new btDeformableMassSpringForce(10,1, true); getDeformableDynamicsWorld()->addForce(psb2, mass_spring2); m_forces.push_back(mass_spring2); diff --git a/examples/DeformableDemo/DeformableRigid.cpp b/examples/DeformableDemo/DeformableRigid.cpp index 850f72600..f34b2a0e8 100644 --- a/examples/DeformableDemo/DeformableRigid.cpp +++ b/examples/DeformableDemo/DeformableRigid.cpp @@ -238,7 +238,7 @@ void DeformableRigid::initPhysics() psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RD; getDeformableDynamicsWorld()->addSoftBody(psb); - btDeformableMassSpringForce* mass_spring = new btDeformableMassSpringForce(2,0.1, true); + btDeformableMassSpringForce* mass_spring = new btDeformableMassSpringForce(30,1, true); getDeformableDynamicsWorld()->addForce(psb, mass_spring); m_forces.push_back(mass_spring); diff --git a/examples/DeformableDemo/GraspDeformable.cpp b/examples/DeformableDemo/GraspDeformable.cpp index 514ee5cc5..08368398d 100644 --- a/examples/DeformableDemo/GraspDeformable.cpp +++ b/examples/DeformableDemo/GraspDeformable.cpp @@ -274,10 +274,10 @@ void GraspDeformable::initPhysics() { char relative_path[1024]; // b3FileUtils::findFile("banana.vtk", relative_path, 1024); - b3FileUtils::findFile("ball.vtk", relative_path, 1024); +// b3FileUtils::findFile("ball.vtk", relative_path, 1024); // b3FileUtils::findFile("paper_collision.vtk", relative_path, 1024); // b3FileUtils::findFile("single_tet.vtk", relative_path, 1024); -// b3FileUtils::findFile("tube.vtk", relative_path, 1024); + b3FileUtils::findFile("tube.vtk", relative_path, 1024); // b3FileUtils::findFile("torus.vtk", relative_path, 1024); // b3FileUtils::findFile("paper_roll.vtk", relative_path, 1024); // b3FileUtils::findFile("bread.vtk", relative_path, 1024); @@ -291,12 +291,12 @@ void GraspDeformable::initPhysics() btSoftBody* psb = btSoftBodyHelpers::CreateFromVtkFile(getDeformableDynamicsWorld()->getWorldInfo(), relative_path); // psb->scale(btVector3(30, 30, 30)); // for banana - psb->scale(btVector3(.05, .05, .05)); +// psb->scale(btVector3(.2, .2, .2)); // psb->scale(btVector3(2, 2, 2)); -// psb->scale(btVector3(.3, .3, .3)); // for tube, torus, boot -// psb->scale(btVector3(1, 1, 1)); // for ditto - psb->translate(btVector3(.25, 2, 0.4)); - psb->getCollisionShape()->setMargin(0.02); + psb->scale(btVector3(.3, .3, .3)); // for tube, torus, boot +// psb->scale(btVector3(.1, .1, .1)); // for ditto +// psb->translate(btVector3(.25, 2, 0.4)); + psb->getCollisionShape()->setMargin(0.01); psb->setTotalMass(.01); psb->m_cfg.kKHR = 1; // collision hardness with kinematic objects psb->m_cfg.kCHR = 1; // collision hardness with rigid body @@ -308,7 +308,7 @@ void GraspDeformable::initPhysics() getDeformableDynamicsWorld()->addForce(psb, gravity_force); m_forces.push_back(gravity_force); - btDeformableNeoHookeanForce* neohookean = new btDeformableNeoHookeanForce(250,500); + btDeformableNeoHookeanForce* neohookean = new btDeformableNeoHookeanForce(10,20, 0.01); getDeformableDynamicsWorld()->addForce(psb, neohookean); m_forces.push_back(neohookean); } diff --git a/src/BulletSoftBody/btDeformableBackwardEulerObjective.h b/src/BulletSoftBody/btDeformableBackwardEulerObjective.h index 8265d61d5..7699a195e 100644 --- a/src/BulletSoftBody/btDeformableBackwardEulerObjective.h +++ b/src/BulletSoftBody/btDeformableBackwardEulerObjective.h @@ -93,6 +93,7 @@ public: m_preconditioner->operator()(x,b); } + // reindex all the vertices virtual void updateId() { size_t node_id = 0; @@ -125,6 +126,7 @@ public: m_implicit = implicit; } + // Calculate the total potential energy in the system btScalar totalEnergy(btScalar dt); }; diff --git a/src/BulletSoftBody/btDeformableMassSpringForce.h b/src/BulletSoftBody/btDeformableMassSpringForce.h index 2f71d1d4a..f50f3389b 100644 --- a/src/BulletSoftBody/btDeformableMassSpringForce.h +++ b/src/BulletSoftBody/btDeformableMassSpringForce.h @@ -62,9 +62,9 @@ public: btVector3 scaled_force = scale * m_dampingStiffness * v_diff; if (m_momentum_conserving) { - if ((node2->m_q - node1->m_q).norm() > SIMD_EPSILON) + if ((node2->m_x - node1->m_x).norm() > SIMD_EPSILON) { - btVector3 dir = (node2->m_q - node1->m_q).normalized(); + btVector3 dir = (node2->m_x - node1->m_x).normalized(); scaled_force = scale * m_dampingStiffness * v_diff.dot(dir) * dir; } } @@ -118,9 +118,9 @@ public: btVector3 local_scaled_df = scaled_k_damp * (dv[id2] - dv[id1]); if (m_momentum_conserving) { - if ((node2->m_q - node1->m_q).norm() > SIMD_EPSILON) + if ((node2->m_x - node1->m_x).norm() > SIMD_EPSILON) { - btVector3 dir = (node2->m_q - node1->m_q).normalized(); + btVector3 dir = (node2->m_x - node1->m_x).normalized(); local_scaled_df= scaled_k_damp * (dv[id2] - dv[id1]).dot(dir) * dir; } } @@ -129,6 +129,7 @@ public: } } } + virtual double totalElasticEnergy(btScalar dt) { double energy = 0; @@ -150,6 +151,35 @@ public: return energy; } + virtual double totalDampingEnergy(btScalar dt) + { + double energy = 0; + int sz = 0; + for (int i = 0; i < m_softBodies.size(); ++i) + { + btSoftBody* psb = m_softBodies[i]; + for (int j = 0; j < psb->m_nodes.size(); ++j) + { + sz = btMax(sz, psb->m_nodes[j].index); + } + } + TVStack dampingForce; + dampingForce.resize(sz+1); + for (int i = 0; i < dampingForce.size(); ++i) + dampingForce[i].setZero(); + addScaledDampingForce(0.5, dampingForce); + for (int i = 0; i < m_softBodies.size(); ++i) + { + btSoftBody* psb = m_softBodies[i]; + for (int j = 0; j < psb->m_nodes.size(); ++j) + { + const btSoftBody::Node& node = psb->m_nodes[j]; + energy -= dampingForce[node.index].dot(node.m_v) / dt; + } + } + return energy; + } + virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df) { // implicit damping force differential