[Solved] Softbody nodes incorrect values

turanszkij
Posts: 6
Joined: Sun Mar 16, 2014 8:17 pm

[Solved] Softbody nodes incorrect values

Post by turanszkij »

I recently switched from Havok physics to Bullet because I wanted to have soft body collisions, but I don't have any budget to license the Havok solution, I couldn't even try those functions out there so it is completely new to me.

I want to create a soft body from an arbitrary mesh and for that I use the soft body helper:

Code: Select all

btVector3* btVerts = new btVector3[vertices.size()];
for(int i=0;i<vertices.size();++i)
    btVerts[i] = (btVector3(vertices[i].pos.x,vertices[i].pos.y,vertices[i].pos.z));
const int iCount = indices.size();
const int tCount = iCount/3;
int* btInd = new int[iCount];
for(int i=0;i<iCount;++i){
    btInd[i] = (int)indices[i];
}

btSoftBody* softBody = btSoftBodyHelpers::CreateFromTriMesh(
        ((btSoftRigidDynamicsWorld*)dynamicsWorld)->getWorldInfo()
        ,(btScalar*)btVerts
        ,&btInd[0],tCount
        ,false
    );
Where vertices and indices are vector references coming from my graphics engine. I fill out the mesh with that information and seemingly I can create it without problem. After I step the physics world I connect my BULLET softbody nodes to my vertices in the graphics engine, then updating the vertex buffer.
I get the information from the softbody nodes by their tNodeArray m_nodes this way:

Code: Select all

btSoftBody::tNodeArray&   nodes(softBody->m_nodes);


    for(int j=0;j<nodes.size();++j)
    {
        verticesIn[j].pos=XMFLOAT3(nodes[j].m_x.getX(),nodes[j].m_x.getY(),nodes[j].m_x.getZ());
        verticesIn[j].nor=XMFLOAT3(nodes[j].m_n.getX(),nodes[j].m_n.getY(),nodes[j].m_n.getZ());
    }
The problem: However I do not get correct vertex positions and neither correct normals. I am using a simple quad mesh with 4 vertices (the positions): { (-1,0,-1), (1,0,-1), (1,0,1), (-1,0,1) }
But if I get values from the softbody's nodes after creation, but before stepping any physics I don't get back my original values from the nodes (the positions): { (-1,0,-1), (0,1,0), (-1,0,1), (0,1,0) }
I even noticed the node vertices kind of resemble the originals, just "overlapped" somehow or not sure how can I describe if it is even the case, but it is like the second vector has an extra zero at the beginning and the whole mesh got screwed up.
Could it be something is not right with my loading? Or do I miss something in the concept of soft body physics?
I have studied the bullet soft body sample which is quite informative and they sure load their meshes like I do (the bunny mesh for instance), at least it seems like it. I don't have any idea and got stuck here for a day now. Without connecting to the softbody, my mesh remains correct.
Last edited by turanszkij on Sun Mar 16, 2014 10:48 pm, edited 1 time in total.
turanszkij
Posts: 6
Joined: Sun Mar 16, 2014 8:17 pm

Re: Softbody nodes incorrect values

Post by turanszkij »

So I solved it shortly after posting this (as always :oops: ):

It was a really stupid amateur mistake. I filled my btVerts array of btVector3 references whereas I should have done it for btScalars for every one of the three vertex position coordinates. The function even expects that, and I din't see it because copy-pasted from my mesh loading, where I send btVector3s as vertices in the btTriangleIndexVertexArray method (but I can specify the striding there, whereas in btSoftBodyHelpers::CreateFromTriMesh I cannot).