Im trying to implement bulletphysics into a project Im working on currently, and I ran into a couple of problems along the way.
First the setup:
I get a scene serialized as a std::map of the objects, and their properties/parameters (unique ID, vertices, mass, etc). At the moment the transformation data (trans/rot/scale) is baked into the vertices. The plan is to set up a program that deserializes the objects, runs a stepsimulation and sends the updated transformation matrices to a server via TCP to update the actual scene (rather convoluted, I know).
Now my first attempt was to write my own solution, taking the "Hello World"-example from the wiki as a base, just adding in my Objects as btConvexHulls and send back the data from
Code: Select all
btTransform transMtx;
body->getMotionState()->getWorldTransform(transMtx);
transMtx.getOrigin()
The scene consists of a table (static, 4 cubes for legs,1 for the tabletop), flat cube for the ground(static) and 3 primitive objects(dynamic) above the table.
The objects don't collide at all, and seem to jump arround and odd angles, and somehow seem to act like some sort of pendulum, swinging back and forth suspended in midair.
To get a better grasp on what was going on, I decided to throw everything into the basicexample.cpp of the example browser(I literally ctrl+c/ctrl+v'ed it), and, lo and behold, the scene rendered by the debugdrawer actually matches the original one. The objects fall down, collide with the table and each other, and finally hit the ground.
But...the transformation matrices I get from the m_dynamicsWorld and/or motionState() aren't representing that behaviour at all. The values I send back just describe the 3 dynamic objects falling down, no collision with each other, or the table. They all come to rest slightly below the ground block, clipped into each other, but on the same height.
Sooo...
1. Where would I be able to read out the correct values for the transformation matrices, including the collisions ? I tried several functions, but the results were all the same.
2. While the scene, objects and collisions are all there in the debugdrawer-representation, they behave a bit odd. Could it be that the center of mass of all the dynamic objects isn't actually in the center of their collisionshapes, since they were build from the vertices in their "final" position instead of being build with (0,0,0) as the center ?
Thanks,
cout
If it helps any, here's the changes to the basicexample.cpp:
Code: Select all
void BasicExample::initPhysics()
{
std::vector<SceneObject> retval;
//deserialization happens here
std::vector<int> extract_Objects(std::map<int, SceneObjects> const& Object);
{
for (auto const& element : Object)
{
retval.push_back(element.second);
}
}
m_guiHelper->setUpAxis(2);
createEmptyDynamicsWorld();
m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
if (m_dynamicsWorld->getDebugDrawer())
m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe+btIDebugDraw::DBG_DrawContactPoints);
m_dynamicsWorld->setGravity(btVector3(0, 0, -9.81));
for (unsigned int i = 0; i < physObject.size(); i++)
{
std::vector<std::vector<float>> vertices = retval[i].vertices;
btConvexHullShape *convex = new btConvexHullShape();
for (unsigned int j = 0; j < vertices.size(); j++)
{
convex->addPoint(btVector3(vertices.at(j)[0], vertices.at(j)[1], vertices.at(j)[2]));
}
btTransform tr;
tr.setIdentity();
btScalar mass(retval[i].mass);
btVector3 localInertia(1, 1, 1);
if (mass != 0.f)
{
convex->calculateLocalInertia(mass, localInertia);
}
createRigidBody(mass, tr, convex);
}
m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
}
void BasicExample::renderScene()
{
CommonRigidBodyBase::renderScene();
//one of the spots I tried to grab the transformed values to transfere from
}