How to import stl file and make it a rigid body

Post Reply
mali28
Posts: 5
Joined: Fri Aug 10, 2018 3:06 pm

How to import stl file and make it a rigid body

Post by mali28 »

Hello I am new to bullet physics. I want to import stl (3d geometry) file as a rigid body. I am able to import the file but the particles do not recognise the imported stl geometry and pass through it. I want to collide the particles with imported geometry. Please tell me the code to import the stl file as a rigid body.

Thank you.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to import stl file and make it a rigid body

Post by drleviathan »

More details please. Are you using C++ or pybullet? Maybe supply your code or a snippet of the part that actually loads your file and builds the body.

What are these "particles" you're talking about? There is no "particle" support in bullet although it could support collision of many small dynamic objects.
mali28
Posts: 5
Joined: Fri Aug 10, 2018 3:06 pm

Re: How to import stl file and make it a rigid body

Post by mali28 »

Thank you for your reply. I am using visual studio for compiling. I have slightly modified the original code in the example folder (demo) to add collision bodies and also import the stl file.
The screenshot of the output is below.
I am using the following code to import and display the stl file:



ImportSTLSetup::ImportSTLSetup(struct GUIHelperInterface* helper, const char* fileName)
:CommonRigidBodyBase(helper),
m_scaling(btVector3(0.025,0.025,0.025))
{
if (fileName)
{
m_fileName = fileName;
m_scaling = btVector3(0.0001,0.0001,0.0001);
} else
{
// m_fileName = "l_finger.stl";
m_fileName = "poly_box.stl";
//m_fileName="teddy.obj";

}
}

and within initphysics:




GLInstanceGraphicsShape* gfxShape = LoadMeshFromSTL(relativeFileName);

btTransform trans;
trans.setIdentity();
trans.setRotation(btQuaternion(btVector3(1,0,0),SIMD_HALF_PI));

btVector3 position = trans.getOrigin();
btQuaternion orn = trans.getRotation();

btVector4 color(0.3,0.1,0.1,0.4);
//btVector3 alpha(0.3,0.4,0.2);

int shapeId = m_guiHelper->getRenderInterface()->registerShape(&gfxShape->m_vertices->at(0).xyzw[0], gfxShape->m_numvertices, &gfxShape->m_indices->at(0), gfxShape->m_numIndices);


const GLInstanceVertex& v = gfxShape->m_vertices->at(0);
btConvexHullShape* shape = new btConvexHullShape((const btScalar*)(&(v.xyzw[0])), gfxShape->m_numvertices, sizeof(GLInstanceVertex));

float scaling[4] = {0.1,0.1,0.1,1};

btVector3 localScaling(scaling[0],scaling[1],scaling[2]);
shape->setLocalScaling(localScaling);
m_collisionShapes.push_back(shape);
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(0.1f);
btVector3 localInertia(1,1,1);
bool isDynamic = (mass != 0.f);
shape->calculateLocalInertia(mass,localInertia);
startTransform.setOrigin(position);
m_guiHelper->getRenderInterface()->registerGraphicsInstance(shapeId,position,orn,color,m_scaling);
Attachments
problem.png
problem.png (289.89 KiB) Viewed 9248 times
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to import stl file and make it a rigid body

Post by drleviathan »

I see. You are modifying examples/Importers/ImportSTLDemo/ImportSTLSetup.cpp.

When I look at that file I see what appears to be a truly minimal example whose only purpose is to demo how to load an STL model from file and to build a btCollisionShape from it. It does create a dynamicsWorld but then does not create a btRigidBody to use the shape, nor does it add any body to the world.

I think you need to find some other example that actually calls m_dynamicsWorld->addRigidBody(). Follow the pattern in that example to finish modifying your custom ImportSTLSetup.cpp.
mali28
Posts: 5
Joined: Fri Aug 10, 2018 3:06 pm

Re: How to import stl file and make it a rigid body

Post by mali28 »

Thank you for your reply. I can not find any example code for addRigidBody() can you please send me a link?
mali28
Posts: 5
Joined: Fri Aug 10, 2018 3:06 pm

Re: How to import stl file and make it a rigid body

Post by mali28 »

I have used the following code to make it a rigid body:

btDefaultMotionState* motionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,-1,0)));
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(
10, // mass
motionState, // initial position
shape, // collision shape of body
btVector3(1,1,1) // local inertia
);

btRigidBody *rigidBody = new btRigidBody(rigidBodyCI);
m_dynamicsWorld->addRigidBody(rigidBody);



But the shape is still unrecognised by the particles. Please let me know what is wrong in the code.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to import stl file and make it a rigid body

Post by drleviathan »

Giving it a btRigidBody is definitely a step forward, but if the particles pass through the object then there must still be something else wrong. However I don't have enough info to know what it is.

Do the particles collide with anything in the world? Are they dynamic at all?

What btCollisionShape are you using for the particles?

What does the code look like that generates the particle bodies and adds them to the world?
usamarafiq93
Posts: 5
Joined: Mon Jun 10, 2019 3:46 pm

Re: How to import stl file and make it a rigid body

Post by usamarafiq93 »

I'm having the same problem where my objects are just passing through each other. I IMPORTED an STL file. Do you need to see the code?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to import stl file and make it a rigid body

Post by drleviathan »

Now that I look back at this issue I notice: it doesn't look like the OP was creating the motionState and then not using it. The motionState is a "bridge" object between the btRigidBody and the GameObject (rendered thing). One should at least supply the motionState to the btRigidBody constructor, and also probably give it to some other construct on the rendering side.
Post Reply