RayTest randomly failing
Posted: Tue May 14, 2019 3:57 pm
I am new to bullet and am trying to perform raytests on simple box objects in my scene.
Most of the time, the test succeeds but will randomly not detect a box.
My setup code as follows
Raytest as follows:
Debug output in Console (select lines)
S vec3(-5.087273, 0.000000, 39.472855)
E vec3(-5.025157, 0.000000, 39.472855)
Collided at-5.04925 1 39.4729
.
.
.
.
.
S vec3(-5.099017, 0.000000, 39.472855)
E vec3(-5.037612, 0.000000, 39.472855)
Collided at-5.04872 1 39.4729 //Different collission point, why??
S vec3(-5.171562, 0.000000, 39.472855)
E vec3(-5.110108, 0.000000, 39.472855)
MD vec3(0.061170, 0.000000, -0.000000)
S vec3(-5.110108, 0.000000, 39.472855)
E vec3(-5.048937, 0.000000, 39.472855)
//There should have been a collission here but it did not happen!
MD vec3(0.061084, 0.000000, -0.000000)
S vec3(-5.048937, 0.000000, 39.472855)
E vec3(-4.987854, 0.000000, 39.472855)
MD vec3(0.060863, 0.000000, -0.000000)
I should also mention that this demo was running at 800+fps, when turning on vsync, the Start and end points will be further apart and this problem happens much less often, but still happens. If this is a floating point rounding issue, how do games using Bullet deal with it?
Most of the time, the test succeeds but will randomly not detect a box.
My setup code as follows
Code: Select all
void Game::initPhysics()
{
// Build the broadphase
broadphase = new btDbvtBroadphase();
// Set up the collision configuration and dispatcher
collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
// The actual physics solver
solver = new btSequentialImpulseConstraintSolver();
// The world.
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0,-9.81f,0));
glm::vec3& bb = scene->treeMesh->boundingBox;
btCollisionShape* boxCollisionShape = new btBoxShape(btVector3(bb.x, bb.y, bb.z));
for(auto& tree : scene->treeMI)
{
btDefaultMotionState* motionstate = new btDefaultMotionState(btTransform(
btQuaternion(tree.getOrientationRef().x, tree.getOrientationRef().y, tree.getOrientationRef().z, tree.getOrientationRef().w),
btVector3(tree.getPosRef().x, tree.getPosRef().y, tree.getPosRef().z)
));
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(
0, // mass, in kg. 0 -> Static object, will never move.
motionstate,
boxCollisionShape, // collision shape of body
btVector3(0,0,0) // local inertia
);
btRigidBody *rigidBody = new btRigidBody(rigidBodyCI);
dynamicsWorld->addRigidBody(rigidBody);
}
}
Code: Select all
bool Game::movePlayer(const glm::vec3& moveDir)
{
glm::vec3& start = playerMI->getPosRef();
glm::vec3 end = start + moveDir;
std::cout<<"MD "<<glm::to_string(moveDir)<<std::endl;
std::cout<<"S "<<glm::to_string(start)<<std::endl;
std::cout<<"E "<<glm::to_string(end)<<std::endl;
btCollisionWorld::ClosestRayResultCallback RayCallback(
btVector3(start.x, start.y+1.0f, start.z),
btVector3(end.x, end.y+1.0f, end.z)
);
dynamicsWorld->rayTest(
btVector3(start.x, start.y+1.0f, start.z),
btVector3(end.x, end.y+1.0f, end.z),
RayCallback
);
if(RayCallback.hasHit())
{
std::cout<<"Collided at"<<RayCallback.m_hitPointWorld.x()<<" "<<RayCallback.m_hitPointWorld.y()<<" "<<RayCallback.m_hitPointWorld.z()<<std::endl;
}
return !RayCallback.hasHit();
}
S vec3(-5.087273, 0.000000, 39.472855)
E vec3(-5.025157, 0.000000, 39.472855)
Collided at-5.04925 1 39.4729
.
.
.
.
.
S vec3(-5.099017, 0.000000, 39.472855)
E vec3(-5.037612, 0.000000, 39.472855)
Collided at-5.04872 1 39.4729 //Different collission point, why??
S vec3(-5.171562, 0.000000, 39.472855)
E vec3(-5.110108, 0.000000, 39.472855)
MD vec3(0.061170, 0.000000, -0.000000)
S vec3(-5.110108, 0.000000, 39.472855)
E vec3(-5.048937, 0.000000, 39.472855)
//There should have been a collission here but it did not happen!
MD vec3(0.061084, 0.000000, -0.000000)
S vec3(-5.048937, 0.000000, 39.472855)
E vec3(-4.987854, 0.000000, 39.472855)
MD vec3(0.060863, 0.000000, -0.000000)
I should also mention that this demo was running at 800+fps, when turning on vsync, the Start and end points will be further apart and this problem happens much less often, but still happens. If this is a floating point rounding issue, how do games using Bullet deal with it?