I'm writing a game engine with DirectX 11 and now i've implemented bullet physics into it.
I have an issue with all shapes (spheres, boxes, convexHulls, etc)...
You can see the problem in video attached.
https://youtu.be/KFW0jgunfDc
Here boxes are btBoxShape's and the plane has btStaticPlaneShape.
Below is the function that initializes bullet:
Code: Select all
void World::InitDynamicsWorld()
{
m_collisionConfiguration = new btDefaultCollisionConfiguration();
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
m_overlappingPairCache = new btDbvtBroadphase();
m_solver = new btSequentialImpulseConstraintSolver;
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher, m_overlappingPairCache, m_solver, m_collisionConfiguration);
m_dynamicsWorld->setGravity(btVector3(0, -10, 0));
}
Code: Select all
void RigidBody3D::InitializePhysics()
{
if (m_type == SPHERE_TYPE)
{
float radius;
float x, y, z;
x = Object3D::m_vertices[0].position.x;
y = Object3D::m_vertices[0].position.y;
z = Object3D::m_vertices[0].position.z;
radius = max(x, y);
if (radius == x)
radius = max(x, z);
else
radius = max(y, z);
btSphereShape* sphere = new btSphereShape(btScalar(radius));
btTransform tr;
tr.setIdentity();
m_compoundShape = new btCompoundShape();
m_compoundShape->addChildShape(tr, sphere);
}
else if (m_type == CUBE_TYPE)
{
float sizeX = 0;
float sizeY = 0;
float sizeZ = 0;
for (size_t i = 1; i < Object3D::m_vertices.size(); i++)
{
float dX = Object3D::m_vertices[i].position.x - Object3D::m_vertices[i - 1].position.x;
dX = abs(dX);
sizeX = max(sizeX, dX);
float dY = Object3D::m_vertices[i].position.y - Object3D::m_vertices[i - 1].position.y;
dY = abs(dY);
sizeY = max(sizeY, dY);
float dZ = Object3D::m_vertices[i].position.z - Object3D::m_vertices[i - 1].position.z;
dZ = abs(dZ);
sizeZ = max(sizeZ, dZ);
}
btBoxShape* box = new btBoxShape(btVector3(sizeX / 2, sizeY / 2, sizeZ / 2));
btTransform tr;
tr.setIdentity();
m_compoundShape = new btCompoundShape();
m_compoundShape->addChildShape(tr, box);
}
else if (m_type == TRIANGLEMESH_TYPE)
{
int sizeOfConvexHull = 5;
for (size_t i = sizeOfConvexHull; i <= Object3D::m_vertices.size(); i += sizeOfConvexHull)
{
if (i > Object3D::m_vertices.size())
i = Object3D::m_vertices.size();
m_convexArray.push_back(new btConvexHullShape());
for (size_t j = i - 1; j >= i - sizeOfConvexHull; j--)
{
m_convexArray.back()->addPoint(btVector3(Object3D::m_vertices[j].position.x, Object3D::m_vertices[j].position.y, Object3D::m_vertices[j].position.z));
}
}
btTransform trans;
trans.setIdentity();
m_compoundShape = new btCompoundShape();
for (size_t i = 0; i < m_convexArray.size(); i++)
m_compoundShape->addChildShape(trans, m_convexArray[i]);
}
else if (m_type == PLANE_TYPE)
{
btStaticPlaneShape* plane = new btStaticPlaneShape(btVector3(Object3D::m_vertices[0].normal.x, Object3D::m_vertices[0].normal.y, Object3D::m_vertices[0].normal.z), 0);
btTransform tr;
tr.setIdentity();
m_compoundShape = new btCompoundShape();
m_compoundShape->addChildShape(tr, plane);
}
btVector3 inertia = btVector3(0, 0, 0);
if (m_mass != 0)
m_compoundShape->calculateLocalInertia(m_mass, inertia);
btTransform positionMatrix;
positionMatrix.setIdentity();
positionMatrix.setOrigin(m_position);
btMotionState* motionState = new btDefaultMotionState(positionMatrix);
m_body = new btRigidBody(m_mass, motionState, m_compoundShape, inertia);
m_body->setFriction(1.0f);
m_body->setRestitution(0.5f);
}
Code: Select all
void World::StepSimulation(float time)
{
m_dynamicsWorld->stepSimulation(time, 1, time);
for (int i = m_dynamicsWorld->getNumCollisionObjects() - 1; i >= 0; i--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
btRigidBody* body = btRigidBody::upcast(obj);
btTransform trans;
if (body && body->getMotionState())
body->getMotionState()->getWorldTransform(trans);
else
trans = obj->getWorldTransform();
GetRSM()->GetObjectsVector()[i]->GetObject3D()->SetPosition(trans.getOrigin().getX(), trans.getOrigin().getY(), trans.getOrigin().getZ());
GetRSM()->GetObjectsVector()[i]->GetObject3D()->SetRotation(trans.getRotation().getX(), trans.getRotation().getY(), trans.getRotation().getZ());
}
}