bool gkRayTest::collides(const gkVector3& from, const gkVector3& to, gkRayTest::gkRayTestFilter& rayCallback) { btVector3 rayFrom(from.x, from.y, from.z); btVector3 rayTo(to.x, to.y, to.z); btVector3 hitPointWorld; rayCallback.m_collisionFilterGroup = btBroadphaseProxy::AllFilter; rayCallback.m_collisionFilterMask = btBroadphaseProxy::AllFilter; GK_ASSERT(m_scene); btDynamicsWorld* pWorld = m_scene->getDynamicsWorld()->getBulletWorld(); GK_ASSERT(pWorld); pWorld->rayTest(rayFrom, rayTo, rayCallback); if (rayCallback.hasHit()) { m_hitNormalWorld = gkVector3(rayCallback.m_hitNormalWorld); m_collisionObject = rayCallback.m_collisionObject; m_hitFraction = rayCallback.m_closestHitFraction; hitPointWorld.setInterpolate3(rayFrom, rayTo, m_hitFraction); m_hitPointWorld = gkVector3(hitPointWorld); return true; } return false; }
glm::vec3 GameWorld::getGroundAtPosition(const glm::vec3 &pos) const { btVector3 rayFrom(pos.x, pos.y, 100.f); btVector3 rayTo(pos.x, pos.y, -100.f); btDynamicsWorld::ClosestRayResultCallback rr(rayFrom, rayTo); dynamicsWorld->rayTest( rayFrom, rayTo, rr ); if(rr.hasHit()) { auto& ws = rr.m_hitPointWorld; return { ws.x(), ws.y(), ws.z() }; } return pos; }
PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ) { float minFraction = 1.f; SimdVector3 rayFrom(fromX,fromY,fromZ); SimdVector3 rayTo(toX,toY,toZ); SimdVector3 hitPointWorld,normalWorld; //Either Ray Cast with or without filtering //CollisionWorld::ClosestRayResultCallback rayCallback(rayFrom,rayTo); FilterClosestRayResultCallback rayCallback(ignoreClient,rayFrom,rayTo); PHY_IPhysicsController* nearestHit = 0; m_collisionWorld->RayTest(rayFrom,rayTo,rayCallback); if (rayCallback.HasHit()) { nearestHit = static_cast<CcdPhysicsController*>(rayCallback.m_collisionObject->m_userPointer); hitX = rayCallback.m_hitPointWorld.getX(); hitY = rayCallback.m_hitPointWorld.getY(); hitZ = rayCallback.m_hitPointWorld.getZ(); normalX = rayCallback.m_hitNormalWorld.getX(); normalY = rayCallback.m_hitNormalWorld.getY(); normalZ = rayCallback.m_hitNormalWorld.getZ(); } return nearestHit; }