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 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.