I have imported a mesh from an FBX file and created a btBvhTriangleMeshShape from it. I turn debug line drawing on and there in the world is the nice blue wireframe of the physics shape in all it's glory. I'm attaching the bvh tm to a btCompoundShape and everything still looks good. Next I pick/rayTest from screenspace through the model BUT nothing happens. The hasHit() call always returns false. I have verified that the ray is correct.
Now here's the interesting thing.
1) If I load models with only one submesh thus creating only one btBvhTriangleMeshShape in my compound shape then ray testing never works. If on the other hand I load a model that has more than one submesh and produces multiple triangle mesh shapes then it works!
2) And yet if the camera angle is looking down +X and I cast my ray through the object even on the working model the rayTest fails.
Here's some code. The swap of z and y is because my coordinate system is +X right, +Y forward and +Z up. Has that anything to do with it?
Code: Select all
void rayTest( btDiscreteDynamicsWorld* world, const ray3& ray, const Entity::RaycastResult& lambda ){
btVector3 rayTo(
btScalar( ray.p.x + ray.d.x ),
btScalar( ray.p.z + ray.d.z ),
btScalar( ray.p.y + ray.d.y ));
btVector3 rayFrom(
btScalar( ray.p.x ),
btScalar( ray.p.z ),
btScalar( ray.p.y ));
btCollisionWorld::ClosestRayResultCallback cb( rayFrom, rayTo );
world->rayTest( rayFrom, rayTo, cb );
ray3 rays[2];
rays[1]=ray;
if( cb.hasHit() ){
btRigidBody* bt = (btRigidBody*)cb.m_collisionObject;
pt3 P;
P.x = cb.m_hitPointWorld.x();
P.y = cb.m_hitPointWorld.z();
P.z = cb.m_hitPointWorld.y();
vec3 N;
N.x = cb.m_hitNormalWorld.x();
N.y = cb.m_hitNormalWorld.z();
N.z = cb.m_hitNormalWorld.y();
if( !N.valid() ){
N = vec3( 0, 1, 0 );
}
Entity::Instance& instance = *(Entity::Instance*)bt->getUserPointer();
rays[0] = ray3( P, N );
lambda( rays, &instance );
}else{
lambda( rays, nullptr );
}
}
BpH