Raycast does not hit correct triangle

hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia

Raycast does not hit correct triangle

Post by hiker »

Hi,

I am using btTriangleMeshShape with quantized Aabb-compression to store the track of my racing game. But I recently noticed that when I do (nearest hit) raycasts onto the track, sometimes the ray would not hit the expected triangle - it would hit some other triangles far below. This apparently happens when the ray is close to the common edge between two triangles - one of which it is supposed to hit (the triangles have identical vertices, so the ray should hit one or the other, it can't go 'through' the two triangles).

If I disable quantization, the expected triangle is hit (with bullet 2.66).

Is this a known problem? Should I try the latest SVN, or do you want to have a test case?

Cheers,
Joerg
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Raycast does not hit correct triangle

Post by Erwin Coumans »

hiker wrote:Is this a known problem? Should I try the latest SVN, or do you want to have a test case?
This is new to me. It would help a lot if you can reproduce the problem in a Bullet demo (with ray and trimesh).
Can you do this?

Thanks,
Erwin
hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia

Re: Raycast does not hit correct triangle

Post by hiker »

Erwin Coumans wrote: This is new to me. It would help a lot if you can reproduce the problem in a Bullet demo (with ray and trimesh).
Can you do this?
I'll try - the problem is that just a single (or few) triangles apparently do not trigger the problem, I might have to load the whole track :(

Cheers,
Joerg
hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia

Re: Raycast does not hit correct triangle

Post by hiker »

OK, here a testcase - I was finally able to reproduce the problem with just two triangles (it helped when I picked the correct triangles :oops: ). This program just adds two triangles to the dynamics world, and does a single 'closest result' raycast.:

Code: Select all

#include "btBulletDynamicsCommon.h"

int main(int argc, char **argv) {

  // Create the world
  // ================
    btDynamicsWorld                 *m_dynamics_world;
    btCollisionDispatcher           *m_dispatcher;
    btBroadphaseInterface           *m_axis_sweep;
    btDefaultCollisionConfiguration *m_collision_conf;
    btSequentialImpulseConstraintSolver *m_solver;

    m_collision_conf    = new btDefaultCollisionConfiguration();
    m_dispatcher        = new btCollisionDispatcher(m_collision_conf);
    
    btVector3 worldMin(-1000, -1000, -1000);
    btVector3 worldMax( 1000,  1000,  1000);
    m_axis_sweep        = new btAxisSweep3(worldMin, worldMax);
    m_solver = new btSequentialImpulseConstraintSolver();
    m_dynamics_world    = new btDiscreteDynamicsWorld(m_dispatcher, 
                                                      m_axis_sweep, 
                                                      m_solver,
                                                      m_collision_conf);
    m_dynamics_world->setGravity(btVector3(0.0f, 0.0f, -10.0));


    // Create triangle mesh
    // ====================
    btTriangleMesh               m_mesh;
    btDefaultMotionState        *m_motion_state;
    btCollisionShape            *m_collision_shape;

    // This triangle should be hit
    btVector3 t1( 5.275304, 0.234100, -0.610112);
    btVector3 t2(-5.493612, 2.414262, -0.610112);
    btVector3 t3(-5.496377, 0.224400, -0.610112);
    m_mesh.addTriangle(t1,t2,t3);

    // This triangle below the first one is hit
    btVector3 w1( 2481.443848,  2442.308350, -14.738982);
    btVector3 w2(-2419.639404,  2442.309814, -14.738982);
    btVector3 w3(-2419.640381, -2458.775146, -14.738612);
    m_mesh.addTriangle(w1,w2,w3);

    m_collision_shape = new btBvhTriangleMeshShape(&m_mesh, true);
    btTransform startTransform;
    startTransform.setIdentity();
    m_motion_state = new btDefaultMotionState(startTransform);
    btRigidBody* m_body=new btRigidBody(0.0f, m_motion_state, m_collision_shape);

    m_dynamics_world->addRigidBody(m_body);

    // The raycast
    // ===========
    btVector3 from(1.5000964, 0.23261292, -0.01);
    btVector3 to(from);
    to.setZ(-10000);
    btCollisionWorld::ClosestRayResultCallback raycallback(from, to);
    m_dynamics_world->rayTest(from, to, raycallback);
    printf("resulting hit is at %f %f %f\n",
	   raycallback.m_hitPointWorld.getX(),
	   raycallback.m_hitPointWorld.getY(),
	   raycallback.m_hitPointWorld.getZ());

}
Compiled with:
g++ test.cpp -I bullet/src/ -L bullet/src/ -lbulletdynamics -lbulletcollision -lbulletmath
and bullet 2.66.

With quantization a hit at the lower (2nd) triangle is reported (z= -14.738799), while
a) without quantization or
b) without the 2nd triangle at all
a hit with the first triangle is reported (z=-0.610112)

Output without quantization (correct):

Code: Select all

resulting hit is at 1.500096 0.232613 -0.610112
i.e. a hit with the first (higher) triangle. The same result is reported when just removing the 2nd triangle.
Output with quantization (as above, i.e. with both triangles):

Code: Select all

resulting hit is at 1.500096 0.232613 -14.738799
Cheers,
Joerg
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Raycast does not hit correct triangle

Post by Erwin Coumans »

Thanks a lot for the reproduction case. We added some ray test optimizations that have some issues, related to dequantization.

Until we fix them, the optimizations are now disabled in Subversion.

Thanks again,
Erwin
hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia

Re: Raycast does not hit correct triangle

Post by hiker »

Hi Erwin,

thanks for your quick response. When do you plan the next release? I would prefer updating to an official bullet version, and not SVN :)

Cheers,
Joerg
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Raycast does not hit correct triangle

Post by Erwin Coumans »

There is a alpha release that disables the broken optimization.

http://sourceforge.net/project/showfile ... _id=260898

We plan on releasing Bullet 2.67 before the Game Developers Conference in February 18.

Thanks,
Erwin