reformulate how constraints are managed in the projection class

This commit is contained in:
Xuchen Han
2019-07-11 11:26:30 -07:00
parent b28f1fdac3
commit 4e5f4b9fe9
5 changed files with 173 additions and 138 deletions

View File

@@ -12,20 +12,21 @@ void btContactProjection::update(const TVStack& dv, const TVStack& backupVelocit
///solve rigid body constraints
m_world->btMultiBodyDynamicsWorld::solveConstraints(m_world->getSolverInfo());
// loop through contacts to create contact constraints
for (int i = 0; i < m_softBodies.size(); ++i)
// loop through constraints to set constrained values
for (auto it : m_constraints)
{
btSoftBody* psb = m_softBodies[i];
btMultiBodyJacobianData jacobianData;
for (int i = 0, ni = psb->m_rcontacts.size(); i < ni; ++i)
btAlignedObjectArray<Constraint>& constraints = it.second;
for (int i = 0; i < constraints.size(); ++i)
{
const btSoftBody::RContact& c = psb->m_rcontacts[i];
// skip anchor points
if (c.m_node->m_im == 0)
Constraint& constraint = constraints[i];
if (constraint.m_contact == nullptr)
{
// nothing needs to be done for dirichelet constraints
continue;
const btSoftBody::sCti& cti = c.m_cti;
}
const btSoftBody::RContact* c = constraint.m_contact;
const btSoftBody::sCti& cti = c->m_cti;
btMultiBodyJacobianData jacobianData;
if (cti.m_colObj->hasContactResponse())
{
btVector3 va(0, 0, 0);
@@ -37,7 +38,7 @@ void btContactProjection::update(const TVStack& dv, const TVStack& backupVelocit
if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
{
rigidCol = (btRigidBody*)btRigidBody::upcast(cti.m_colObj);
va = rigidCol ? (rigidCol->getVelocityInLocalPoint(c.m_c1)) * m_dt : btVector3(0, 0, 0);
va = rigidCol ? (rigidCol->getVelocityInLocalPoint(c->m_c1)) * m_dt : btVector3(0, 0, 0);
}
else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
{
@@ -49,7 +50,7 @@ void btContactProjection::update(const TVStack& dv, const TVStack& backupVelocit
jacobianData.m_deltaVelocitiesUnitImpulse.resize(ndof);
btScalar* jac = &jacobianData.m_jacobians[0];
multibodyLinkCol->m_multiBody->fillContactJacobianMultiDof(multibodyLinkCol->m_link, c.m_node->m_x, cti.m_normal, jac, jacobianData.scratch_r, jacobianData.scratch_v, jacobianData.scratch_m);
multibodyLinkCol->m_multiBody->fillContactJacobianMultiDof(multibodyLinkCol->m_link, c->m_node->m_x, cti.m_normal, jac, jacobianData.scratch_r, jacobianData.scratch_v, jacobianData.scratch_m);
deltaV = &jacobianData.m_deltaVelocitiesUnitImpulse[0];
multibodyLinkCol->m_multiBody->calcAccelerationDeltasMultiDof(&jacobianData.m_jacobians[0], deltaV, jacobianData.scratch_r, jacobianData.scratch_v);
@@ -62,26 +63,26 @@ void btContactProjection::update(const TVStack& dv, const TVStack& backupVelocit
}
}
const btVector3 vb = c.m_node->m_v * m_dt;
const btVector3 vb = c->m_node->m_v * m_dt;
const btVector3 vr = vb - va;
const btScalar dn = btDot(vr, cti.m_normal);
if (1) // in the same CG solve, the set of constraits doesn't change
// if (dn <= SIMD_EPSILON)
// if (dn <= SIMD_EPSILON)
{
// c0 is the impulse matrix, c3 is 1 - the friction coefficient or 0, c4 is the contact hardness coefficient
const btVector3 impulse = c.m_c0 *(cti.m_normal * dn);
const btVector3 impulse = c->m_c0 *(cti.m_normal * dn);
// TODO: only contact is considered here, add friction later
// dv = new_impulse + accumulated velocity change in previous CG iterations
// so we have the invariant node->m_v = backupVelocity + dv;
btVector3 dv = -impulse * c.m_c2/m_dt + c.m_node->m_v - backupVelocity[m_indices[c.m_node]];
btVector3 dv = -impulse * c->m_c2/m_dt + c->m_node->m_v - backupVelocity[m_indices[c->m_node]];
btScalar dvn = dv.dot(cti.m_normal);
m_constrainedValues[m_indices[c.m_node]][0]=(dvn);
constraint.m_value = dvn;
if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
{
if (rigidCol)
rigidCol->applyImpulse(impulse, c.m_c1);
rigidCol->applyImpulse(impulse, c->m_c1);
}
else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
{
@@ -97,24 +98,23 @@ void btContactProjection::update(const TVStack& dv, const TVStack& backupVelocit
}
}
void btContactProjection::setConstraintDirections()
{
// set Dirichlet constraint
size_t counter = 0;
for (int i = 0; i < m_softBodies.size(); ++i)
{
const btSoftBody* psb = m_softBodies[i];
btSoftBody* psb = m_softBodies[i];
for (int j = 0; j < psb->m_nodes.size(); ++j)
{
if (psb->m_nodes[j].m_im == 0)
{
m_constrainedDirections[counter].push_back(btVector3(1,0,0));
m_constrainedDirections[counter].push_back(btVector3(0,1,0));
m_constrainedDirections[counter].push_back(btVector3(0,0,1));
m_constrainedValues[counter].push_back(0);
m_constrainedValues[counter].push_back(0);
m_constrainedValues[counter].push_back(0);
m_constrainedId.push_back(counter);
btAlignedObjectArray<Constraint> c;
c.push_back(Constraint(btVector3(1,0,0)));
c.push_back(Constraint(btVector3(0,1,0)));
c.push_back(Constraint(btVector3(0,0,1)));
m_constraints[&(psb->m_nodes[j])] = c;
}
++counter;
}
@@ -125,14 +125,12 @@ void btContactProjection::setConstraintDirections()
btSoftBody* psb = m_softBodies[i];
btMultiBodyJacobianData jacobianData;
int j = 0;
while (j < psb->m_rcontacts.size())
for (int j = 0; j < psb->m_rcontacts.size(); ++j)
{
const btSoftBody::RContact& c = psb->m_rcontacts[j];
// skip anchor points
if (c.m_node->m_im == 0)
{
psb->m_rcontacts.removeAtIndex(j);
continue;
}
@@ -178,68 +176,69 @@ void btContactProjection::setConstraintDirections()
const btScalar dn = btDot(vr, cti.m_normal);
if (dn < SIMD_EPSILON)
{
++j;
m_constrainedDirections[m_indices[c.m_node]].push_back(cti.m_normal);
m_constrainedValues[m_indices[c.m_node]].resize(m_constrainedValues[m_indices[c.m_node]].size()+1);
m_constrainedId.push_back(m_indices[c.m_node]);
if (m_constraints.find(c.m_node) == m_constraints.end())
{
btAlignedObjectArray<Constraint> constraints;
constraints.push_back(Constraint(c));
m_constraints[c.m_node] = constraints;
}
else
{
m_constraints[c.m_node].push_back(Constraint(c));
}
continue;
}
}
psb->m_rcontacts.removeAtIndex(j);
}
}
// for particles with more than three constrained directions, prune constrained directions so that there are at most three constrained directions
counter = 0;
const int dim = 3;
for (int i = 0; i < m_softBodies.size(); ++i)
for (auto it : m_constraints)
{
const btSoftBody* psb = m_softBodies[i];
for (int j = 0; j < psb->m_nodes.size(); ++j)
const btAlignedObjectArray<Constraint>& c = it.second;
if (c.size() > dim)
{
if (m_constrainedDirections[counter].size() > dim)
btAlignedObjectArray<Constraint> prunedConstraints;
// always keep the first constrained direction
prunedConstraints.push_back(c[0]);
// find the direction most orthogonal to the first direction and keep it
size_t selected = 1;
btScalar min_dotProductAbs = std::abs(prunedConstraints[0].m_direction.dot(c[selected].m_direction));
for (int j = 2; j < c.size(); ++j)
{
btAlignedObjectArray<btVector3> prunedConstraints;
// always keep the first constrained direction
prunedConstraints.push_back(m_constrainedDirections[counter][0]);
// find the direction most orthogonal to the first direction and keep it
size_t selected = 1;
btScalar min_dotProductAbs = std::abs(prunedConstraints[0].dot(m_constrainedDirections[counter][selected]));
for (int j = 2; j < m_constrainedDirections[counter].size(); ++j)
btScalar dotProductAbs =std::abs(prunedConstraints[0].m_direction.dot(c[j].m_direction));
if (dotProductAbs < min_dotProductAbs)
{
btScalar dotProductAbs =std::abs(prunedConstraints[0].dot(m_constrainedDirections[counter][j]));
if (dotProductAbs < min_dotProductAbs)
{
selected = j;
min_dotProductAbs = dotProductAbs;
}
selected = j;
min_dotProductAbs = dotProductAbs;
}
if (std::abs(min_dotProductAbs-1) < SIMD_EPSILON)
{
m_constrainedDirections[counter++] = prunedConstraints;
continue;
}
prunedConstraints.push_back(m_constrainedDirections[counter][selected]);
// find the direction most orthogonal to the previous two directions and keep it
size_t selected2 = (selected == 1) ? 2 : 1;
btVector3 normal = btCross(prunedConstraints[0], prunedConstraints[1]);
normal.normalize();
btScalar max_dotProductAbs = std::abs(normal.dot(m_constrainedDirections[counter][selected2]));
for (int j = 3; j < m_constrainedDirections[counter].size(); ++j)
{
btScalar dotProductAbs = std::abs(normal.dot(m_constrainedDirections[counter][j]));
if (dotProductAbs > min_dotProductAbs)
{
selected2 = j;
max_dotProductAbs = dotProductAbs;
}
}
prunedConstraints.push_back(m_constrainedDirections[counter][selected2]);
m_constrainedDirections[counter] = prunedConstraints;
m_constrainedValues[counter].resize(dim);
}
++counter;
if (std::abs(min_dotProductAbs-1) < SIMD_EPSILON)
{
it.second = prunedConstraints;
continue;
}
prunedConstraints.push_back(c[selected]);
// find the direction most orthogonal to the previous two directions and keep it
size_t selected2 = (selected == 1) ? 2 : 1;
btVector3 normal = btCross(prunedConstraints[0].m_direction, prunedConstraints[1].m_direction);
normal.normalize();
btScalar max_dotProductAbs = std::abs(normal.dot(c[selected2].m_direction));
for (int j = 3; j < c.size(); ++j)
{
btScalar dotProductAbs = std::abs(normal.dot(c[j].m_direction));
if (dotProductAbs > min_dotProductAbs)
{
selected2 = j;
max_dotProductAbs = dotProductAbs;
}
}
prunedConstraints.push_back(c[selected2]);
it.second = prunedConstraints;
}
}
}