Half of TriangeMeshShape missing

Yartch
Posts: 2
Joined: Tue Dec 24, 2013 3:01 am

Half of TriangeMeshShape missing

Post by Yartch »

Hey there! I'm trying to set up my first level in bullet, but I've been stuck on this problem for a while.

My level's mesh is in an .obj, and I'm trying to load it into a btBvhTriangleMeshShape. I've had some success so far, with half the level working almost perfectly. My problem is the MeshShape doesn't seem to exist below x=0. If I step too far over x, I just end up in an infinite fall.

Here's some code. The vectors seem to fill up perfectly, and I don't see anything wrong with moving the data into the arrays. I tried to mimic the ConcavePhysics demo as closely as possible.

Code: Select all

struct CollisionVertex
{
	float x, y, z;
};

   //Parse .obj file
	ifstream objfile;
	objfile.open(file);
	if(!objfile.bad())
	{
		//Temp vectors to be filled with vert/index data
		vector<CollisionVertex> verts = vector<CollisionVertex>();
		vector<int> inds = vector<int>();

		while(!objfile.eof())
		{
			string line = "";
			getline(objfile, line);

			vector<string> strs;
			boost::split(strs, line, boost::is_any_of(" "));
			vector<string> cleanstrs = vector<string>();
			for(int i=0;i<(int)strs.size();i++)
			{
				if(strs[i] != "") cleanstrs.push_back(strs[i]);
			}
			strs = cleanstrs;

			if(strs.size() == 4)
			{
				string type = strs[0];

				if(type == "v")
				{
					CollisionVertex vert;
					vert.x = (float)atof(strs[1].c_str());
					vert.y = (float)atof(strs[2].c_str());
					vert.z = (float)atof(strs[3].c_str());
					verts.push_back(vert);
				}
				else if(type == "f")
				{
					inds.push_back(atoi(strs[1].c_str()));
					inds.push_back(atoi(strs[2].c_str()));
					inds.push_back(atoi(strs[3].c_str()));
				}
			}
		}
		objfile.close();

		int totalTris = (int)(inds.size() / 3);

		int indexStride = 3*sizeof(int);
		int vertStride = sizeof(btVector3);

		gVertices = new btVector3[verts.size()];
		gIndices = new int[inds.size()];

		for(int v=0;v<(int)verts.size();v++)
		{
			gVertices[v].setValue(verts[v].x, verts[v].y, verts[v].z);
		}
		for(int i=0;i<(int)inds.size();i++)
		{
			gIndices[i] = inds[i];
		}

		vertexarray = new btTriangleIndexVertexArray(totalTris, gIndices, indexStride, verts.size(), (btScalar*)&gVertices[0].x(), vertStride);

		btVector3 aabbMin(-1000,-1000,-1000),aabbMax(1000,1000,1000);
		trimeshShape = new btBvhTriangleMeshShape(vertexarray,true);//,aabbMin,aabbMax); //runtime error if these are included
	}
The comment I put on the last line makes me think that's where the root of the problem is. I have no clue what it is though. It breaks me in btQuantizedBvh.h when the error happens.

The shape gets passed to here, where it's used in the RigidBody.

Code: Select all

   //Level collision
	level_collision_mesh = new CollisionMesh("D:/Programming/Islands/levels/test/comp/testlevel_collision.obj");
	level_shape = level_collision_mesh->trimeshShape;
	level_motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,0,0)));
	level = new btRigidBody(0, level_motion, level_shape);

	//Player collision
	player_shape = new btSphereShape(1);
	player_motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,60,0)));
	player = new btRigidBody(10, player_motion, player_shape);
	
	//Create world
	btVector3 worldMin(-1000,-1000,-1000);
	btVector3 worldMax(1000,1000,1000);

	collisionConfiguration = new btDefaultCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collisionConfiguration);
	broadphase = new btAxisSweep3(worldMin,worldMax);
	solver = new btSequentialImpulseConstraintSolver;

	world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
	world->setGravity(btVector3(0,-10,0));

	world->addRigidBody(level);
	world->addRigidBody(player);
I think it's worth noting that the exact same thing happens, even if there's only 2 or 8 faces on the collision mesh.

Thanks for taking the time to read, hopefully someone can help out!
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Half of TriangeMeshShape missing

Post by xexuxjy »

usual trick is to see if the debug draw system is showing the mesh as you'd expect it.
other things to try : adjust your reader so that the x < 0 values are shift up and greater then zero ( I can't think of a reason why the -ve's would cause an issue but just in the spirit of debugging) , then adjust the world position down a bit to compensate.

when you say you get a runtime error on including aabbmin and max what is the error and at what line?
Yartch
Posts: 2
Joined: Tue Dec 24, 2013 3:01 am

Re: Half of TriangeMeshShape missing

Post by Yartch »

I never even knew about the Debug Drawer! Just got it working and it's definitely a problem with the parsing. The triangles are all over the place, and there's even a vertex at least 1000 units away from everything else (which I'm guessing is the cause of the aabbmin error). Thanks for showing me this, it'll help out a lot.

EDIT: Turns out I just needed to subtract 1 from each index, since my exporter starts the vertex indices at 1, but btTriangleIndexVertexArray starts at 0.
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Half of TriangeMeshShape missing

Post by xexuxjy »

great! nice easy fix :)