[Solved] adding rigid body to dynamics world

Post Reply
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

[Solved] adding rigid body to dynamics world

Post by johnsonalpha »

How can i turn my triangle mesh in to a convex dynamic rigid body

Ive already looked through the convexdecompositiondemo but im to stupid to understand it

Can you check if im doing this correctly and how do i make a rigid body from this and add it to the simulation?! IM LOST!!

Code: Select all


// i already have the trainglemesh setup
mTriMesh->addTriangle(v0, v1, v2, false);

btConvexShape* tmpConvexShape = new btConvexTriangleMeshShape(mTriMesh);

//create a hull approximation
btShapeHull* hull = new btShapeHull(tmpConvexShape);
btScalar margin = tmpConvexShape->getMargin();
hull->buildHull(margin);
tmpConvexShape->setUserPointer(hull);

btConvexHullShape* convexShape = new btConvexHullShape();
bool updateLocalAabb = false;

for (i=0;i<hull->numVertices();i++)
{
convexShape->addPoint(hull->getVertexPointer()[i],updateLocalAabb);	
}
convexShape->recalcLocalAabb();
														
convexShape->initializePolyhedralFeatures();
delete tmpConvexShape;
delete hull;

bool useQuantization = true;
btCollisionShape* concaveShape = new btBvhTriangleMeshShape(mTriMesh,useQuantization);
startTransform.setOrigin(convexDecompositionObjectOffset);.


Now how do i make a rigid body from this and add it to the dynamics world!
Last edited by johnsonalpha on Mon May 11, 2015 2:29 pm, edited 2 times in total.
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Traingle mesh to a btconvexhull ( HELP )

Post by johnsonalpha »

Any help or tips would be awesome!
joehot200
Posts: 15
Joined: Wed Apr 23, 2014 9:47 am

Re: HOW DOES TRAINGLE MESH TO CONVEXHULL WORK? ( HELP )!!!!

Post by joehot200 »

I only use JBullet, so the code I supply will be useless to you - Sorry about that.

A ConvexHullShape simply takes all vertices of the model and makes a convex hull out of them.

In JBullet, the code would be

Code: Select all

ConvexHullShape shape = new ConvexHullShape(ARRAYLIST_OF_VERTICES)
In other words, all you need is a list of all points of your model (those are the vertices), and then you simply create a new ConvexHullShape using them.

In java, the code is as simple as this:

Code: Select all

{
	    	Model Amod = null;
	    	try {
				Amod = OBJLoader.loadModel(new File("res/Archer.obj"));
			} catch (IOException e1) {
				e1.printStackTrace();
				System.exit(1);
			}
                //Model is just a model class that contains all vertices from an OBJ file.
	    	ObjectArrayList<Vector3f> Ash = new ObjectArrayList<Vector3f>();
	    	Ash.addAll(Amod.vertices);
	    	Archer.shape = new ConvexHullShape(Ash);
	    }
Just create one of those and then make a RigidBody out of it. Like,

Code: Select all

ConvexHullShape sh = shape;//new SphereShape(1.0f);
			DefaultMotionState motionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0f, 4500f, 0f, 1), new javax.vecmath.Vector3f(owner.x + location.x, owner.y, owner.z + location.z), 1)));
	        javax.vecmath.Vector3f inertia = new javax.vecmath.Vector3f();
	        sh.calculateLocalInertia(1f, inertia);
	        RigidBodyConstructionInfo constructionInfo = new RigidBodyConstructionInfo(1.0f, motionState, sh, inertia);
	        constructionInfo.restitution = 0.0f;
	        body = new RigidBody(constructionInfo);
	        body.setRestitution(0.0f);
	        body.setFriction(1);
	        body.setAngularFactor(0);
	        body.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
	        GameServer.dynamicsWorld.addRigidBody(body);
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: HOW DOES TRAINGLE MESH TO CONVEXHULL WORK? ( HELP )!!!!

Post by johnsonalpha »

I now how to make a convex hull but i dont understand how to convert a bttrainglemesh to a dynamic convex hull :(
joehot200
Posts: 15
Joined: Wed Apr 23, 2014 9:47 am

Re: HOW DOES TRAINGLE MESH TO CONVEXHULL WORK? ( HELP )!!!!

Post by joehot200 »

johnsonalpha wrote:I now how to make a convex hull but i dont understand how to convert a bttrainglemesh to a dynamic convex hull :(
Wouldn't you just get all the vertices of the triangle mesh, and make a ConvexHullShape with them?
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: HOW DOES TRAINGLE MESH TO CONVEXHULL WORK? ( HELP )!!!!

Post by johnsonalpha »

Yeah but im not sure how to do that! :(
aviator
Posts: 13
Joined: Thu Apr 02, 2015 5:15 pm

Re: HOW DOES TRAINGLE MESH TO CONVEXHULL WORK? ( HELP )!!!!

Post by aviator »

johnsonalpha wrote:Yeah but im not sure how to do that! :(
This worked for me:

Code: Select all


btScalar mass = 5.0f;
btVector3 fallInertia(0, 0, 0);

btCollisionShape* testShape = NULL;
btTriangleMesh* tempTrimeshShape = new btTriangleMesh();

//Add triangles
btVector3 vertex0;
btVector3 vertex1;
btVector3 vertex2;
tempTrimeshShape->addTriangle(vertex0, vertex1, vertex2);

btConvexShape *tmpshape = new btConvexTriangleMeshShape(tempTrimeshShape);
btShapeHull *hull = new btShapeHull(tmpshape);

btScalar margin = tmpshape->getMargin();
hull->buildHull(margin);
tmpshape->setUserPointer(hull); 

testShape = tmpshape;
testShape->calculateLocalInertia(mass, fallInertia);

btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, testShape, fallInertia); //dynamicShape
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);


fallRigidBody->setRestitution(0.2f);
fallRigidBody->setFriction(0.2f);

fallRigidBody->setActivationState(true);


But for some reason I can't figure out why I can't use tmpshape shape in btCompoundShape ?
btCompoundShape* dynamicShape = new btCompoundShape();
dynamicShape->addChildShape(childTransform, tmpshape);
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: HOW DOES TRAINGLE MESH TO CONVEXHULL WORK? ( HELP )!!!!

Post by johnsonalpha »

aviator wrote:
johnsonalpha wrote:Yeah but im not sure how to do that! :(
This worked for me:

Code: Select all


btScalar mass = 5.0f;
btVector3 fallInertia(0, 0, 0);

btCollisionShape* testShape = NULL;
btTriangleMesh* tempTrimeshShape = new btTriangleMesh();

//Add triangles
btVector3 vertex0;
btVector3 vertex1;
btVector3 vertex2;
tempTrimeshShape->addTriangle(vertex0, vertex1, vertex2);

btConvexShape *tmpshape = new btConvexTriangleMeshShape(tempTrimeshShape);
btShapeHull *hull = new btShapeHull(tmpshape);

btScalar margin = tmpshape->getMargin();
hull->buildHull(margin);
tmpshape->setUserPointer(hull); 

testShape = tmpshape;
testShape->calculateLocalInertia(mass, fallInertia);

btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, testShape, fallInertia); //dynamicShape
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);


fallRigidBody->setRestitution(0.2f);
fallRigidBody->setFriction(0.2f);

fallRigidBody->setActivationState(true);


But for some reason I can't figure out why I can't use tmpshape shape in btCompoundShape ?
btCompoundShape* dynamicShape = new btCompoundShape();
dynamicShape->addChildShape(childTransform, tmpshape);
I did everything you did and it looks good so far so my qeustion is so i just make the btcompoundshape then add the shape then add the btcompound to the dynamics world?

Code: Select all

btScalar mass = 5.0f;
btVector3 fallInertia(0, 0, 0);

btCollisionShape* testShape = NULL;
btTriangleMesh* tempTrimeshShape = new btTriangleMesh();

//Add triangles
btVector3 vertex0;
btVector3 vertex1;
btVector3 vertex2;
tempTrimeshShape->addTriangle(vertex0, vertex1, vertex2);

btConvexShape *tmpshape = new btConvexTriangleMeshShape(tempTrimeshShape);
btShapeHull *hull = new btShapeHull(tmpshape);

btScalar margin = tmpshape->getMargin();
hull->buildHull(margin);
tmpshape->setUserPointer(hull); 

testShape = tmpshape;
testShape->calculateLocalInertia(mass, fallInertia);

btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, testShape, fallInertia); //dynamicShape
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);

btCompoundShape* dynamicShape = new btCompoundShape();
dynamicShape->addChildShape(childTransform, tmpshape);
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: HOW DOES TRAINGLE MESH TO CONVEXHULL WORK? ( HELP )!!!!

Post by johnsonalpha »

aviator wrote:
johnsonalpha wrote:Yeah but im not sure how to do that! :(
This worked for me:

Code: Select all


btScalar mass = 5.0f;
btVector3 fallInertia(0, 0, 0);

btCollisionShape* testShape = NULL;
btTriangleMesh* tempTrimeshShape = new btTriangleMesh();

//Add triangles
btVector3 vertex0;
btVector3 vertex1;
btVector3 vertex2;
tempTrimeshShape->addTriangle(vertex0, vertex1, vertex2);

btConvexShape *tmpshape = new btConvexTriangleMeshShape(tempTrimeshShape);
btShapeHull *hull = new btShapeHull(tmpshape);

btScalar margin = tmpshape->getMargin();
hull->buildHull(margin);
tmpshape->setUserPointer(hull); 

testShape = tmpshape;
testShape->calculateLocalInertia(mass, fallInertia);

btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, testShape, fallInertia); //dynamicShape
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);


fallRigidBody->setRestitution(0.2f);
fallRigidBody->setFriction(0.2f);

fallRigidBody->setActivationState(true);


But for some reason I can't figure out why I can't use tmpshape shape in btCompoundShape ?
btCompoundShape* dynamicShape = new btCompoundShape();
dynamicShape->addChildShape(childTransform, tmpshape);

nevermind i figured it out THANKS!!!
Post Reply