The problem we are facing are listed below.
1. The Cloth gets stretched too much and keep stretching. [Major]
2. The objects scale are messed up [Minor - I think we are missing something in the sync function]
Here is the video of the problem.
https://www.youtube.com/watch?v=Xpb3InmwumM
Here is our softbody code
Code: Select all
btSoftBody* PhysicsHelper::getSoftBody(SGNode* sgNode)
{
Mesh* mesh = dynamic_pointer_cast<MeshNode>(sgNode->node)->getMesh();
btScalar* mesh_vertices = new btScalar[mesh->getVerticesCount() * 3];
int* mesh_indices = new int[mesh->getTotalIndicesCount()];
for(int i = 0; i < mesh->getVerticesCount(); i++) {
vertexData *v = mesh->getLiteVertexByIndex(i);
mesh_vertices[i * 3] = v->vertPosition.x;
mesh_vertices[(i * 3) + 1] = v->vertPosition.y;
mesh_vertices[(i * 3) + 2] = v->vertPosition.z;
}
for( int i = 0; i < mesh->getTotalIndicesCount(); i++)
mesh_indices[i] = mesh->getTotalIndicesArray()[i];
std::map<btSoftBody::Node*, int> node_map;
std::vector<int> indices;
btSoftBody* sBody = btSoftBodyHelpers::CreateFromTriMesh(m_softBodyWorldInfo, mesh_vertices, mesh_indices, mesh->getTotalIndicesCount()/3);
btScalar bodyMass = sgNode->props.weight;
sBody->setMass(1, bodyMass);
for (int i = 0; i< sBody->m_faces.size(); i++) {
btSoftBody::Face face = sBody->m_faces[i];
for (int j = 0; j < 3; j++)
if (node_map.find(face.m_n[j]) == node_map.end())
node_map.insert(std::make_pair(face.m_n[j], node_map.size()));
for (int j = 0; j < 3; j++)
indices.push_back(node_map.find(face.m_n[j])->second);
}
std::map<btSoftBody::Node*, int>::const_iterator node_iter;
for (node_iter = node_map.begin(); node_iter != node_map.end(); ++node_iter)
vertices.insert(std::make_pair(node_iter->second, node_iter->first));
std::map<int, btSoftBody::Node*>::const_iterator it;
for (int i = 0; i < mesh->getVerticesCount(); i++) {
for (it = vertices.begin(); it != vertices.end(); ++it) {
int v_index = it->first;
btSoftBody::Node* node = it->second;
Vector3 vPos = mesh->getLiteVertexByIndex(i)->vertPosition;
if (node->m_x.x() == vPos.x && node->m_x.y() == vPos.y && node->m_x.z() == vPos.z) {
testMesh_map.insert(std::make_pair(i, v_index));
break;
}
}
}
ActionKey key = sgNode->getKeyForFrame(0);
Vector3 nodePos = key.position;
Quaternion nodeRot = key.rotation;
btQuaternion rotation = btQuaternion(nodeRot.x, nodeRot.y, nodeRot.z, nodeRot.w);
btVector3 position = btVector3(nodePos.x, nodePos.y, nodePos.z);
if(sgNode->props.physicsType == CLOTH) {
btSoftBody::Material* pm = sBody->appendMaterial();
sBody->generateBendingConstraints(2, pm);
sBody->setTotalMass(12);
sBody->m_cfg.piterations = 5;
sBody->m_cfg.kDP = 0.005f;
}
sBody->setUserPointer((void*)sgNode);
sBody->transform(btTransform(rotation, position));
return sBody;
}
https://www.youtube.com/watch?v=KThLtlhDMWo