Beginner Issues with ConvexHullShape

Post Reply
mc180
Posts: 2
Joined: Thu Jan 21, 2016 3:10 am

Beginner Issues with ConvexHullShape

Post by mc180 »

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
}
Attachments
sims.png
sims.png (19.08 KiB) Viewed 3785 times
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Beginner Issues with ConvexHullShape

Post by Flix »

//currently just a vector with the 8 corners of a 2x2x2 cube (0,0,0) (2,0,0) etc.
(0,0,0) is the center of mass. You must choose vertices like (-1,0,0) (1,0,0) etc.

P.S. People keeps asking the same things over and over again in this forum. It's not the first time I answer to questions like this one. Maybe using the "Search" button could help beginners...
mc180
Posts: 2
Joined: Thu Jan 21, 2016 3:10 am

Re: Beginner Issues with ConvexHullShape

Post by mc180 »

My apologies, thanks for the help.
Post Reply