Cloth Simulation problem in iOS app

Post Reply
codetiger
Posts: 19
Joined: Sat Aug 18, 2012 2:20 am
Location: Chennai, India
Contact:

Cloth Simulation problem in iOS app

Post by codetiger »

Hi, we are adding Bullet Physics support to our Mobile App (Iyan 3D - iOS / Android) and have successfully integrated Rigidbody physics. However, we are having a tough time adding SoftBody support. The app is a simple animation tool for casual user and will soon become a simulation tool in next update.

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;
}
Our successful RigidBody Simulation video is here. If you want to see it.
https://www.youtube.com/watch?v=KThLtlhDMWo
mobeen
Posts: 122
Joined: Thu May 05, 2011 11:47 am

Re: Cloth Simulation problem in iOS app

Post by mobeen »

There can be many reasons but highly likely seems to me that the mass or scale of your cloth mesh is off. Try setting the mass and/or scale to 1. Another thing would be to generateBendingConstraints at distance of 3 or 4.

I also notice that the size of your graphical sphere does not match the size of your collision sphere. Try to set these to same scale if possible. Also try setting the piterations to a larger value like 10. See if this helps.
Post Reply