[SOLVED]Raycasting nondynamic rigid bodies of CompoundShapes

hs1
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

[SOLVED]Raycasting nondynamic rigid bodies of CompoundShapes

Post by hs1 »

Hi there,

first of all thanks for this great library!
I'm currently developing a first-person-zombie-survival-shooter where I'm using bullet as physicsengine and I'm having problems with raycasting against the zombie-representations.

This is how I set up my btDiscreteDynamicsWorld:
1) I add the static rigid bodies of a Quake3-Map.
2) I create convex-hulls for the bones of my zombie-model.
3) For each currently living zombie I create a btCompoundShape with the convex-hulls of the bones as children and add that shape with a kinematic rigid body.
4) Each frame I'm synchronizing the bone-matrices of the model-instances with the btCompoundShapes of the physical zombie-representations over it's btDefaultMotionState.
5) If the player shoots, then I use the btDiscreteDynamicWorld's raytest-method to check if he has hit the zombie.

But for some reason if I aim and shoot at a zombie, the casted ray always ignores the rigid-bodies of the compound-shapes and goes straight for the static rigid-bodies of the map which are located behind the zombie.

This happens if I use kinematic or static rigidbodies for the zombies.
If I use dynamic rigid bodies the raycasting works, but the world-transformations of the bodies won't take rotation and scaling through calling the setFromGLMatrix()-method.
But that's no solution anyway, because if I use dynamic bodies, I get problems with my pathfinding- and crowd-management-library which can't handle external changes of the locations of it's agents very well.

If I debug-draw the btCompoundShapes of my zombies and the ray-direction everything looks fine.

I'm using the following code for raycasting:

Code: Select all

btCollisionWorld::ClosestRayResultCallback callback(start, end);
m_dynamicsWorld->rayTest(start, end, callback);
if (callback.hasHit()) ...
Anyone knows what might be the reason of this problem?

Thanks for your time!
Last edited by hs1 on Mon Dec 05, 2011 8:37 pm, edited 9 times in total.
hs1
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

Re: Raycasting non-dynamic rigid bodies of compound-shapes

Post by hs1 »

*updated* maybe I wasn't specific enough ;)
hs1
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

Re: Raycasting non-dynamic rigid bodies of compound-shapes

Post by hs1 »

It seems that it has something to do with the way I'm creating the btCompoundShape.
If I substitute the btCompoundShape with a simple btSphereShape everything works fine.
I would really appreciate it if someone could check my code that creates the btCompoundShapes and the corresponding convex hulls.
I just can't find the problem.

edit: Just discovered something strange. While the physical representation of the zombie works fine (I can jump on his head and get moved around if he moves his foot) and also matches the debug-drawing, the spot that is detectable by a raycast seems to have collapsed to a small area inside his right knee o_O. Are there different structures used for raytesting and testing physical collision?

Code: Select all

// =================================================
// Create a convex hull which approximates a bone of the models skeleton.
// This code get's executed for each bone of each model while loading a map.
// =================================================
btTriangleMesh* trimesh = new btTriangleMesh();
for(all triangles in the current bone)
  trimesh->addTriangle(v1, v2, v3);

btConvexTriangleMeshShape* shape = new btConvexTriangleMeshShape(trimesh);
btShapeHull* hull = new btShapeHull(shape);
btScalar margin = shape->getMargin();

hull->buildHull(margin);
shape->setUserPointer(hull);

// =================================================
// Create a btCompoundShape for a model-instance using the convex-hulls
// we've created at model-initialization and add a rigid-body for this shape
// to the world. This code get's called each time a zombie spawns.
// =================================================
btTransform trans;
trans.setFromOpenGLMatrix(modelMatrix);
btDefaultMotionState* modelMotionState = new btDefaultMotionState(trans);

// Create a compound-shape using the btConvexTriangleMeshShapes we've
// created during model-initialization. If I use a btSphere(1) instead, rayTest works.
btCompoundShape* m_shape = new btCompoundShape();
for (size_t i = 0; i < shapes.size(); i++)
{
  btTransform transform;
  transform.setIdentity();
  m_shape->addChildShape(transform, shapes[i]);
}

btScalar mass = 0.0f;
m_inertia = btVector3(0.0f, 0.0f, 0.0f);
btRigidBody::btRigidBodyConstructionInfo ci(mass, modelMotionState, m_shape, inertia);

m_body = new btRigidBody(ci);
m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
m_body->setActivationState(DISABLE_DEACTIVATION);

m_dynamicsWorld->addRigidBody(m_body); 
I've left out the update-function, which synchronizes the btCompoundShape-transformations with the model-instance because the raycasting also doesn't work correctly if I don't comment the function out.

Thanks for your time!
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Raycasting non-dynamic rigid bodies of compound-shapes

Post by xexuxjy »

Not sure it will help, but there are a couple of #defs in btCollisionWorld.cpp

//#define DISABLE_DBVT_COMPOUNDSHAPE_RAYCAST_ACCELERATION
//#define USE_BRUTEFORCE_RAYBROADPHASE 1

that might be worth enabling/disabling and see if you get the same issue?
hs1
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

Re: Raycasting non-dynamic rigid bodies of compound-shapes

Post by hs1 »

Thanks for the hint, I will recompile bullet with these macros and post my results.
hs1
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

Re: Raycasting non-dynamic rigid bodies of compound-shapes

Post by hs1 »

yay, #define DISABLE_DBVT_COMPOUNDSHAPE_RAYCAST_ACCELERATION did it.
finally, it works! I nearly went crazy xD

would be nice to add a comment about that in the doxygen-documentation of btCollisionWorld::rayTest() or moving the acceleration in a development-version until it's stable.

thanks again for your help.