rayTest - didn't get all rigid bodies

cbacon
Posts: 2
Joined: Thu Jan 30, 2014 10:07 pm

rayTest - didn't get all rigid bodies

Post by cbacon »

Hi, i'm programming a small sandbox game and i added bullet physics for collision detection.
I added a function which should get the world cube, the player looks on.
I'm using rayTest to get the hit position and round to the cube position.
I added two pictures so you can see whats going wrong. The white line is the normal of the hit position.
The yellow line visualizes the rounded cube position.
On this picture, the ray didn't detect the cube
http://image-upload.de/file/ChfILi/24713e4f45.png
I moved the player to a larger distance and then it worked
http://image-upload.de/file/jG59Re/9a22773df9.png

Code: Select all

//do ray test
	btVector3 test;
	btVector3 start(position.x, position.y, position.z);
	btVector3 end = start;
	end += 3.0f*btVector3(lookvector.x, lookvector.y, lookvector.z);

	btCollisionWorld::AllHitsRayResultCallback    rayCallback(start, end);
	physicsInterface->getWorld()->rayTest(start, end, rayCallback);


	if (rayCallback.hasHit()) {
		btAlignedObjectArray<btVector3> results = rayCallback.m_hitPointWorld;
		btAlignedObjectArray<btVector3> normals = rayCallback.m_hitNormalWorld;
		do {
			btVector3 result = results[i]; //get result

			//draw debug line
			test = result + normals[i];
			physicsInterface->getWorld()->getDebugDrawer()->drawLine(result, test, btVector4(1, 1, 1, 1));

			//get inside block
			result = result- normals[i] * 0.005f;
			//round to block position
			result.setX(floor(result.x() / BLOCKSIZE + 0.5f)*BLOCKSIZE);
			result.setY(floor(result.y() / BLOCKSIZE + 0.5f)*BLOCKSIZE);
			result.setZ(floor(result.z() / BLOCKSIZE + 0.5f)*BLOCKSIZE);

			//draw debug line for rounded block pos
			test = result + normals[i];
			physicsInterface->getWorld()->getDebugDrawer()->drawLine(result, test, btVector4(1, 1, 0, 1));

			//get block ID, else -1
			blockID =  blockTypes->getInstanceIdbyPos(result.x(), result.y(), result.z());
			i++;
		} while (blockID < 0 && i < results.size());
		return blockID;
	}
	return -1; //no hit
I only check the hits until i get a valid cube position.
I neither update the aabbs nor compute overlappung but i think this is no problem because i only have static objects in the scene except the player.
Well, i think i have an got an idea while writing this. I think the problem is that i assume that the returned hitpoints are sorted by range... I'll check that.
cbacon
Posts: 2
Joined: Thu Jan 30, 2014 10:07 pm

Re: rayTest - didn't get all rigid bodies

Post by cbacon »

Ok, apparently this was the problem. :D

Bullet works great, though!
Awesome work guys!
c6burns
Posts: 149
Joined: Fri May 24, 2013 6:08 am

Re: rayTest - didn't get all rigid bodies

Post by c6burns »

Yeah the results are definitely not sorted by distance :)
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: rayTest - didn't get all rigid bodies

Post by Erwin Coumans »

There might be some issue with the Bullet 2.82 ray caster, we'll look into it.
Filed here: https://github.com/erwincoumans/bullet3/issues/34