Soft Body Rendering

From Physics Simulation Wiki

Jump to: navigation, search

Given a btSoftBody, here is some code snippet to retrieve the vertex/node, and edge/link information:

/* Dummy 'graphic driver'   */
struct
{
    void DrawPoint(const btVector3&) {}
    void DrawLine(const btVector3&,const btVector3&) {}
    void DrawTriangle(const btVector3&,const btVector3&,const btVector3&) {}
} *mygfx = 0;

/* Get a ref to the array of softbodies   */
btSoftBodyArray& softbodies( world->getSoftBodyArray() );

/* For each soft bodies               */
for(int i=0;i<softbodies.size();++i)
{
   btSoftBody*            softbody(softbodies[i]);

   /* Each soft body contain an array of vertices (nodes/particles_mass)   */
   btSoftBody::tNodeArray&   nodes(softbody->m_nodes);

   /* And edges (links/distances constraints)                        */
   btSoftBody::tLinkArray&   links(softbody->m_links);

   /* And finally, faces (triangles)                                 */
   btSoftBody::tFaceArray&   faces(softbody->m_faces);

   /* Then, you can draw vertices...      */
   /* Node::m_x => position            */
   /* Node::m_n => normal (if meaningful)   */
   for(int j=0;j<nodes.size();++j)
   {
      mygfx->DrawPoint(nodes[j].m_x);
   }

   /* Or edges (for ropes)               */
   /* Link::m_n[2] => pointers to nodes   */
   for(int j=0;j<links.size();++j)
   {
      btSoftBody::Node*   node_0=links[j].m_n[0];
      btSoftBody::Node*   node_1=links[j].m_n[1];
      mygfx->DrawLine(node_0->m_x,node_1->m_x);

      /* Or if you need indices...      */
      const int indices[]={   int(node_0-&nodes[0]),
                              int(node_1-&nodes[0])};
   }

   /* And even faces                  */
   /* Face::m_n[3] -> pointers to nodes   */
   for(int j=0;j<faces.size();++j)
   {
      btSoftBody::Node*   node_0=faces[j].m_n[0];
      btSoftBody::Node*   node_1=faces[j].m_n[1];
      btSoftBody::Node*   node_2=faces[j].m_n[2];
      mygfx->DrawTriangle(node_0->m_x,node_1->m_x,node_2->m_x);

      /* Or if you need indices...      */
      const int indices[]={   int(node_0-&nodes[0]),
                              int(node_1-&nodes[0]),
                              int(node_2-&nodes[0])};
   }
}
Personal tools