RayTest randomly failing

Post Reply
gamersg
Posts: 3
Joined: Tue May 14, 2019 3:47 pm

RayTest randomly failing

Post by gamersg » 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

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);            
        }
      }
Raytest as follows:

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();       
    }
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?

steven
Posts: 57
Joined: Mon Nov 05, 2018 8:16 am
Location: China

Re: RayTest randomly failing

Post by steven » Thu May 16, 2019 1:52 am

Could you provide the positions of your rigidbody? i want to reproduce this issue and find the root cause with you. thanks.

gamersg
Posts: 3
Joined: Tue May 14, 2019 3:47 pm

Re: RayTest randomly failing

Post by gamersg » Thu May 16, 2019 4:41 am

Hi steven, thanks for offering your help.

This is the box shape
vec3(5.046927, 19.099869, 4.317809)

This is the position
quat(1.000000, {0.000000, 0.000000, 0.000000}) //orientation (w,x,y,z)
vec3(0.000000, 0.000000, 40.000000) //position

I am testing this on Ubuntu 18.04, will try on Windows later today.

steven
Posts: 57
Joined: Mon Nov 05, 2018 8:16 am
Location: China

Re: RayTest randomly failing

Post by steven » Thu May 16, 2019 7:37 am

i just do a test with your data, all the result is as expected.

Code: Select all

from(-5.087273,0.000000,39.472855)
 to(-5.025157,0.000000,39.472855)
        hasHit ..........-5.046927:1.000000:39.472855
from(-5.099017,0.000000,39.472855)
 to(-5.037612,0.000000,39.472855)
        hasHit ..........-5.046927:1.000000:39.472855
from(-5.171562,0.000000,39.472855)
 to(-5.110108,0.000000,39.472855)
       don't hasHit .........................
from(-5.110108,0.000000,39.472855)
 to(-5.048937,0.000000,39.472855)
       don't hasHit .........................
from(-5.048937,0.000000,39.472855)
 to(-4.987854,0.000000,39.472855)
        hasHit ..........-5.046927:1.000000:39.472855
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!
why do you said that this should have a collision? the x half-extention of the box shape is 5.046927. i think there should not have a collison.

gamersg
Posts: 3
Joined: Tue May 14, 2019 3:47 pm

Re: RayTest randomly failing

Post by gamersg » Thu May 16, 2019 7:56 am

Thanks for testing

If you see my log:

S vec3(-5.087273, 0.000000, 39.472855)
E vec3(-5.025157, 0.000000, 39.472855)
Collided at-5.04925 1 39.4729 //Hit 1

S vec3(-5.099017, 0.000000, 39.472855)
E vec3(-5.037612, 0.000000, 39.472855)
Collided at-5.04872 1 39.4729 //Hit 2

Hit1 and Hit2 have different collission point, why??
why do you said that this should have a collision? the x half-extention of the box shape is 5.046927. i think there should not have a collison.
Because it originally hit at -5.04925. Even if we assume that the correct hit point is -5.04872 on my machine, it did not hit in the last test,
S vec3(-5.048937, 0.000000, 39.472855)
E vec3(-4.987854, 0.000000, 39.472855)
//No collission again

This makes the library useless for collision detection if the hit point can change randomly, allowing my character to go through the box.
If i make the rays longer, the problem occurs much less frequently so i think that the library has a hardcoded limit or rounding issues where it cannot work reliably for smaller float values?

I am using it without double precision. For some reason compiling the library with double precision causes crashes on runtime.

Note that i was running these tests 800 times a second with different start and end points, so if you tested just a few times, it may not reveal the problem.

steven
Posts: 57
Joined: Mon Nov 05, 2018 8:16 am
Location: China

Re: RayTest randomly failing

Post by steven » Thu May 16, 2019 8:24 am

i also run it with very high fps with different parameters, but my hit point always is (-5.046927:1.000000:39.472855).

Code: Select all

while(1)
        castRays();
i compile it with double precision enabled, maybe this is the main difference between us.

good luck!

Post Reply