Add support for teleporting deformables

This commit is contained in:
Xuchen Han
2020-03-11 19:27:46 -07:00
parent 4ab1ded781
commit cbf282fb2f
3 changed files with 69 additions and 2 deletions

View File

@@ -9814,6 +9814,33 @@ bool PhysicsServerCommandProcessor::processInitPoseCommand(const struct SharedMe
body->m_rigidBody->setAngularVelocity(baseAngVel);
}
}
if (body && body->m_softBody)
{
if (clientCmd.m_updateFlags & INIT_POSE_HAS_BASE_LINEAR_VELOCITY)
{
body->m_softBody->setLinearVelocity(baseLinVel);
}
if (clientCmd.m_updateFlags & INIT_POSE_HAS_BASE_ANGULAR_VELOCITY)
{
body->m_softBody->setAngularVelocity(baseAngVel);
}
if (clientCmd.m_updateFlags & INIT_POSE_HAS_INITIAL_POSITION)
{
btTransform tr;
tr.setIdentity();
tr.setOrigin(basePos);
body->m_softBody->transform(tr);
}
if (clientCmd.m_updateFlags & INIT_POSE_HAS_INITIAL_ORIENTATION)
{
btTransform tr;
tr.setIdentity();
tr.setRotation(baseOrn);
body->m_softBody->transform(tr);
}
}
syncPhysicsToGraphics2();

View File

@@ -998,9 +998,43 @@ void btSoftBody::setVolumeDensity(btScalar density)
setVolumeMass(volume * density / 6);
}
//
btVector3 btSoftBody::getLinearVelocity()
{
btVector3 total_momentum = btVector3(0,0,0);
for (int i = 0; i < m_nodes.size(); ++i)
{
btScalar mass = m_nodes[i].m_im == 0 ? 0 : 1.0/m_nodes[i].m_im;
total_momentum += mass * m_nodes[i].m_v;
}
btScalar total_mass = getTotalMass();
return total_mass == 0 ? total_momentum : total_momentum / total_mass;
}
//
void btSoftBody::setLinearVelocity(const btVector3& linVel)
{
btVector3 old_vel = getLinearVelocity();
btVector3 diff = linVel - old_vel;
for (int i = 0; i < m_nodes.size(); ++i)
m_nodes[i].m_v += diff;
}
//
void btSoftBody::setAngularVelocity(const btVector3& angVel)
{
btVector3 old_vel = getLinearVelocity();
btVector3 com = getCenterOfMass();
for (int i = 0; i < m_nodes.size(); ++i)
{
m_nodes[i].m_v = angVel.cross(m_nodes[i].m_x - com) + old_vel;
}
}
//
void btSoftBody::transform(const btTransform& trs)
{
btVector3 com = getCenterOfMass();
const btScalar margin = getCollisionShape()->getMargin();
ATTRIBUTE_ALIGNED16(btDbvtVolume)
vol;
@@ -1008,8 +1042,8 @@ void btSoftBody::transform(const btTransform& trs)
for (int i = 0, ni = m_nodes.size(); i < ni; ++i)
{
Node& n = m_nodes[i];
n.m_x = trs * n.m_x;
n.m_q = trs * n.m_q;
n.m_x = trs * (n.m_x - com) + com;
n.m_q = trs * (n.m_q - com) + com;
n.m_n = trs.getBasis() * n.m_n;
vol = btDbvtVolume::FromCR(n.m_x, margin);

View File

@@ -965,6 +965,12 @@ public:
void setVolumeMass(btScalar mass);
/* Set volume density (using tetrahedrons) */
void setVolumeDensity(btScalar density);
/* Get the linear velocity of the center of mass */
btVector3 getLinearVelocity();
/* Set the linear velocity of the center of mass */
void setLinearVelocity(const btVector3& linVel);
/* Set the angular velocity of the center of mass */
void setAngularVelocity(const btVector3& angVel);
/* Transform */
void transform(const btTransform& trs);
/* Translate */