Creating a Hull Of Static Mesh from OpenGL ...

ckoeber
Posts: 7
Joined: Sat May 10, 2014 2:23 am

Creating a Hull Of Static Mesh from OpenGL ...

Post by ckoeber »

Hello,

I am attempting to load mesh data that I use to draw with OpenGL into the Bullet Engine.

The problem is I don't believe that the mesh data is actually being read from the pointers. Thus the physical world is not matching up and the "Character Controller" is falling through the floor.

The rendered world is fine, so I know the OpenGL data is fine.

Here is the function of my controller class that I am using to transfer the data into OpenGL.

Is there something I am missing here?

Thank you:

Code: Select all

void PhysicsController::AddStaticBasicMesh(PhysicsObject* NewObject, bool ReducePolygonCount = true)
{
	btTriangleMesh* OriginalTriangleMesh = new btTriangleMesh();
	btIndexedMesh* NewMesh = new btIndexedMesh();
	NewMesh->m_triangleIndexBase = (unsigned char *)NewObject->Indices;
	NewMesh->m_triangleIndexStride = 3 * sizeof(unsigned int);
	NewMesh->m_vertexBase = (unsigned char *)NewObject->Vertices;
	NewMesh->m_vertexStride = 3 * sizeof(float);
	NewMesh->m_numVertices = NewObject->NumberOfVertices;
	NewMesh->m_numTriangles = NewObject->NumberOfTriangles;
	OriginalTriangleMesh->addIndexedMesh((*NewMesh));
	btConvexShape* NewStaticMesh = new btConvexTriangleMeshShape(OriginalTriangleMesh);
	btConvexHullShape* ReducedPolygonStaticMesh;

	if (ReducePolygonCount == true) {
		btShapeHull* HullOfOriginalShape = new btShapeHull(NewStaticMesh);
		btScalar CurrentMargin = NewStaticMesh->getMargin();
		HullOfOriginalShape->buildHull(CurrentMargin);
		ReducedPolygonStaticMesh = new btConvexHullShape();

		for (int i = 0; i < HullOfOriginalShape->numVertices(); i++) {
			ReducedPolygonStaticMesh->addPoint(HullOfOriginalShape->getVertexPointer()[i], false);
		}

		ReducedPolygonStaticMesh->recalcLocalAabb();
		/*
		
		Find out what this line does.

		ReducedPolygonStaticMesh->initializePolyhedralFeatures();

		*/
		
		StaticShapes.push_back(ReducedPolygonStaticMesh);
		btDefaultMotionState* StaticMotionState = new btDefaultMotionState((*NewObject->PositionAndOrientation));
		btRigidBody::btRigidBodyConstructionInfo StaticMeshRigidBodyInfo(0, StaticMotionState, ReducedPolygonStaticMesh, btVector3(0.0f, 0.0f, 0.0f));
		btRigidBody* StaticRigidMesh = new btRigidBody(StaticMeshRigidBodyInfo);
		NewObject->Body = StaticRigidMesh;
		StaticRigidMesh->setCollisionFlags(StaticRigidMesh->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
		MainPhysicsWorld->addRigidBody(StaticRigidMesh, btBroadphaseProxy::StaticFilter, btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::DefaultFilter);
		AllRigidBodies.push_back(NewObject);
		StaticRigidMesh->setUserPointer(AllRigidBodies[AllRigidBodies.size() - 1]);
		delete HullOfOriginalShape;
		delete NewStaticMesh;
	} else {
		StaticShapes.push_back(NewStaticMesh);;
		btDefaultMotionState* StaticMotionState = new btDefaultMotionState((*NewObject->PositionAndOrientation));
		btRigidBody::btRigidBodyConstructionInfo StaticMeshRigidBodyInfo(0, StaticMotionState, NewStaticMesh, btVector3(0.0f, 0.0f, 0.0f));
		btRigidBody* StaticRigidMesh = new btRigidBody(StaticMeshRigidBodyInfo);
		NewObject->Body = StaticRigidMesh;
		StaticRigidMesh->setCollisionFlags(StaticRigidMesh->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
		MainPhysicsWorld->addRigidBody(StaticRigidMesh, btBroadphaseProxy::StaticFilter, btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::DefaultFilter);
		AllRigidBodies.push_back(NewObject);
		StaticRigidMesh->setUserPointer(AllRigidBodies[AllRigidBodies.size() - 1]);
	}
}
nikomaster
Posts: 13
Joined: Fri May 16, 2014 9:55 am

Re: Creating a Hull Of Static Mesh from OpenGL ...

Post by nikomaster »

Hello,

I am seeing you are pasing no data to the constructor of btConvexHullShape. I am not sure whether that could be the reason.
This is what I do

Code: Select all

            btConvexTriangleMeshShape *chs=new btConvexTriangleMeshShape(triangleMesh);
            btShapeHull *sh=new btShapeHull(chs);
            btScalar margin= chs->getMargin();
            sh->buildHull(margin);
            btCollisionShape *cs =new btConvexHullShape((const btScalar *)sh->getVertexPointer(),sh->numVertices());
it works like a charm. Need to check the differences between this and adding points later.
ReducedPolygonStaticMesh->initializePolyhedralFeatures(); not sure what it does but you could test withtout it.


Another option for static shapes is btBvhTriangleMeshShape where you can set the triangle mesh directly without reduciton and work fast no matter the size.

On the other hand you should implement the interface btIDebugDraw so you can find out what is going on bullet's side.
ckoeber
Posts: 7
Joined: Sat May 10, 2014 2:23 am

Re: Creating a Hull Of Static Mesh from OpenGL ...

Post by ckoeber »

Thanks; I got the meshes loaded in! I do have a question about debugging but I will as that in a separate question.