Beginner Issues with ConvexHullShape
Posted: Thu Jan 21, 2016 3:41 am
Hi, I am attempting to create a code which drops shapes into a box. I started off trying cubes created with btBoxShape, which works fine. I am now attempting to use less regular shapes created with btConvexHullShape. To start off, I am creating exactly the same cube - at least I think I am - but getting very strange results. I attempted to use the debug drawer to visualize the simulation, and got a wire that connects all the vertices instead of a cube. When I pull the vertices, it seems like the cube is balancing itself on one corner. Code and images are below. Any help would be great.
Code: Select all
static void genCube(int n)
{
btScalar mass = 1;
btVector3 crysInertia(0, 0, 0);
double randOffset = (rand() % 10) - 5;
crysShape.push_back(new btBoxShape(btVector3(1, 1, 1)));
btQuaternion randomQ = btQuaternion(btVector3(rand(), rand(), rand()), rand());
crysMotionState.push_back(new btDefaultMotionState(btTransform(randomQ, btVector3(randOffset, 20, 0))));
crysShape[n]->calculateLocalInertia(mass, crysInertia);
crysRigidBody.push_back(new btRigidBody(mass, crysMotionState[n], crysShape[n], crysInertia));
simWorld->addRigidBody(crysRigidBody[currentNumCrystals]);
}
static void genPrism(int n)
{
btScalar mass = 1;
btVector3 crysInertia(0, 0, 0);
double randOffset = 0;
btConvexHullShape* prismShape = new btConvexHullShape();
btVector3 localInertia(0, 0, 0);
std::vector<std::vector<float>> testCrystal = crysVertices[0]; //currently just a vector with the 8 corners of a 2x2x2 cube (0,0,0) (2,0,0) etc.
for (int i = 0; i < testCrystal.size(); i++)
{
float x = testCrystal[i][0];
float y = testCrystal[i][1];
float z = testCrystal[i][2];
prismShape->addPoint(btVector3(x,y,z),false);
}
prismShape->recalcLocalAabb();
crysShape.push_back(prismShape);
crysShape[n]->calculateLocalInertia(mass, crysInertia);
btQuaternion randomQ = btQuaternion(btVector3(rand(), rand(), rand()), rand());
btVector3 randomR = (btVector3(randOffset, 20, 0));
crysMotionState.push_back(new btDefaultMotionState(btTransform(randomQ, randomR)));
crysRigidBody.push_back(new btRigidBody(mass, crysMotionState[n], crysShape[n], crysInertia));
simWorld->addRigidBody(crysRigidBody[currentNumCrystals]); //I actually do this in another function
}