Code: Select all
double CurTime = GetTime();
double CurTime_Last = GetTime();
while (game_loop)
{
CurTime = GetTime();
double DelTime = CurTime - CurTime_Last;
CurTime_Last = CurTime;
printf("/tTIME %f %f\n", GetFrameTime(), DelTime);
_Physics->stepSimulation(DelTime);
}
I believe my calculation of 'DelTime' to be a bit more accurate than 'GetFrameTime()' since it takes into account the entire time it took to compute the previous loop iteration.
Needless to say I was previously passing like this:
Code: Select all
dynamicsWorld->stepSimulation(timeStep, 10);
which was 10 substeps, changing it to 0 reduced the 'squish factor' down to an
almost unnoticeable amount albeit if you look hard enough you can tell the simulation still has some 'squish'..
As far as the gap is concerned, you can better see it here:
I am actually using convex decomposition with VHACD:
Code: Select all
#pragma once
#include <VHACD.h>
//
// Structure containing objects created during composition
struct DecompResults {
btCompoundShape* CompoundShape = nullptr;
btAlignedObjectArray<btConvexShape*> m_convexShapes = {};
btAlignedObjectArray<btTriangleMesh*> m_trimeshes = {};
};
//
// FBXObject contains vectors of Vertices and Indices
DecompResults* Decomp(Mesh RayMesh) {
//
// Setup Indices
const uint32_t nTriangles = RayMesh.triangleCount;
printf("\tINDICE SIZE %i\n", (int)sizeof(RayMesh.indices));
printf("Decomp: NumTris %i\n", nTriangles);
std::vector<uint32_t> Triangles;
for (uint32_t i = 0; i < nTriangles*3; i++) {
Triangles.push_back(RayMesh.indices[i]);
}
//
// Setup Points (3 Points is 1 Vertex)
const uint32_t nPoints = RayMesh.vertexCount;
printf("Decomp: NumPoints %i\n", nPoints);
std::vector<float> Points;
for (uint32_t i = 0; i < nPoints; i++) {
Points.push_back(RayMesh.vertices[i*3]);
Points.push_back(RayMesh.vertices[i * 3 + 1]);
Points.push_back(RayMesh.vertices[i * 3 + 2]);
}
//
// Setup VHACD Parameters and create its interface
VHACD::IVHACD::Parameters params;
VHACD::IVHACD* interfaceVHACD = VHACD::CreateVHACD();
VHACD::IVHACD::ConvexHull Hull;
//
// Compute approximate convex decomposition
//printf("Compute V-HACD: Points %i Triangles %i\n", Points.size(), Triangles.size());
bool res = interfaceVHACD->Compute(Points.data(), (uint32_t)(Points.size() / 3),
Triangles.data(), (uint32_t)(Triangles.size() / 3), params);
//
// Get the number of convex hulls
unsigned int nConvexHulls = interfaceVHACD->GetNConvexHulls();
//printf("V-HACD Done: Hull Count %i\n", nConvexHulls);
//
// Create a new DecompResults structure
DecompResults* Results = new DecompResults;
//
// Create a new Compound Shape for this decomposition
Results->CompoundShape = new btCompoundShape();
//
// Iterate through each convex hull and fill results
for (unsigned int h = 0; h < nConvexHulls; ++h)
{
//printf("\tHull: %i\n", h);
//printf("\t\tPoints: %i\n", Hull.m_points);
//printf("\t\tTriangles: %i\n", Hull.m_triangles);
//printf("\t\tVertices: %i\n", vertices.size());
//
// Fill 'Hull' for each individual convex hull
interfaceVHACD->GetConvexHull(h, Hull);
//
// Create a new Triangle Mesh for this hull
btTriangleMesh* trimesh = new btTriangleMesh();
Results->m_trimeshes.push_back(trimesh);
//
// Grab the hulls center position
const btVector3 centroid(Hull.m_center[0], Hull.m_center[1], Hull.m_center[2]);
//printf("Hull Center %f %f %f\n", Hull.m_center[0], Hull.m_center[1], Hull.m_center[2]);
//
// Iterate through this hulls triangles
for (unsigned int i = 0; i < Hull.m_nTriangles; i++) {
//
// Calculate indices
const unsigned int index0 = Hull.m_triangles[i * 3];
const unsigned int index1 = Hull.m_triangles[i * 3 + 1];
const unsigned int index2 = Hull.m_triangles[i * 3 + 2];
//
// Calculate vertices
const btVector3 vertex0(Hull.m_points[index0 * 3], Hull.m_points[index0 * 3 + 1], Hull.m_points[index0 * 3 + 2]);
const btVector3 vertex1(Hull.m_points[index1 * 3], Hull.m_points[index1 * 3 + 1], Hull.m_points[index1 * 3 + 2]);
const btVector3 vertex2(Hull.m_points[index2 * 3], Hull.m_points[index2 * 3 + 1], Hull.m_points[index2 * 3 + 2]);
//
// Add this triangle into our Triangle Mesh
trimesh->addTriangle(vertex0 - centroid, vertex1 - centroid, vertex2 - centroid);
}
//
// Create a new ConvexShape from this hulls Triangle Mesh
btConvexShape* convexShape = new btConvexTriangleMeshShape(trimesh);
Results->m_convexShapes.push_back(convexShape);
//
// Create a transform using centroid as origin
btTransform trans;
trans.setIdentity();
trans.setOrigin(centroid);
//
// Add this ConvexShape to our CompoundShape
Results->CompoundShape->addChildShape(trans, convexShape);
}
//
// release memory
interfaceVHACD->Clean();
interfaceVHACD->Release();
//
// Return our DecompResults containing the CompoundShape full of Convexically Decomposed Convex Shapes
return Results;
}
And then finally creating my collision shape as follows:
Code: Select all
void InitPhysics(Physics* Phys)
{
Phy = Phys;
//
// Check if we already have a cached btCollisionShape for this file
if (Phys->_CollisionShapes.count(_FileName))
{
_CollisionShape = Phys->_CollisionShapes[_FileName];
}
//
// Otherwise create one and cache it away
else {
printf("\tCreate New ENTITY CollisionShape\n");
DecompResults* Results = Decomp(_Model.meshes[0]);
_CollisionShape = Results->CompoundShape;
for (int i = 0; i < Results->m_convexShapes.size(); i++) {
Phys->_ConvexShapes.push_back(Results->m_convexShapes[i]);
}
for (int i = 0; i < Results->m_trimeshes.size(); i++) {
Phys->_TriangleMeshes.push_back(Results->m_trimeshes[i]);
}
delete Results;
Phys->_CollisionShapes[_FileName] = _CollisionShape;
}
_CollisionShape->setUserPointer(this);
DefaultMass = 10.0f;
Mass = DefaultMass;
bool isDynamic = (Mass != 0.f);
if (isDynamic) {
_CollisionShape->calculateLocalInertia(Mass, localInertia);
}
SceneNodeMotionState* MotionState = new SceneNodeMotionState(this);
btRigidBody::btRigidBodyConstructionInfo rbInfo(Mass, MotionState, _CollisionShape, localInertia);
_RigidBody = new btRigidBody(rbInfo);
_RigidBody->setUserPointer(this);
Phys->dynamicsWorld->addRigidBody(_RigidBody);
}
The vertex and index data being passed into VHACD should be exactly the same that is used to render the cube.
Turning on debug draw shows a 1:1 overlap of collision shape vs rendered object:
Maybe there are some solver settings or some other configuration that can be changed to optimize the simulation..?