RayTest btConvexHullShape fails
Posted: Sat Apr 17, 2021 9:19 am
I am developing a 3D space shooter game using bullet physics. I have a crosshair in the middle of the screen and I am ray testing from the center of the screen to the camera far vector in bullet to get the first collision object in that ray. If the object is a sphere or a box the test is very accurate and successful. If the object is a convex hull shape then the ray test fails 90%... Here is my code :
Am I doing something wrong with the convex hull shape or is it a bullet problem? Can I do something to improve it?
Code: Select all
bool BphysicsManager::rayHasHit(glm::vec3& rayFromWorld, glm::vec3& rayToWorld, btVector3* hitPointWorld, btRigidBody** body)
{
mMutex->lock();
bool ret;
rayFrom.setX(rayFromWorld.x);
rayFrom.setY(rayFromWorld.y);
rayFrom.setZ(rayFromWorld.z);
rayTo.setX(rayToWorld.x);
rayTo.setY(rayToWorld.y);
rayTo.setZ(rayToWorld.z);
btCollisionWorld::ClosestRayResultCallback rayCallback(rayFrom, rayTo);
mpDynamicsWorld->rayTest(rayFrom, rayTo, rayCallback);
if (rayCallback.m_collisionObject)
{
*body = (btRigidBody*)btRigidBody::upcast(rayCallback.m_collisionObject);
*hitPointWorld = rayCallback.m_hitPointWorld;
auto colShape = (*body)->getCollisionShape();
if (dynamic_cast<btConcaveShape*>(colShape) != nullptr)
{
*body = nullptr;
}
}
ret = rayCallback.hasHit();
mMutex->unlock();
return ret;
}