add raycasting

This commit is contained in:
gugus 2011-02-22 20:53:02 +01:00
parent e84a02546e
commit ada4616fd3
2 changed files with 56 additions and 6 deletions

View file

@ -31,7 +31,7 @@ namespace Physic
transform.setIdentity(); transform.setIdentity();
// External capsule // External capsule
externalGhostObject = new btPairCachingGhostObject(); externalGhostObject = new PairCachingGhostObject(name);
externalGhostObject->setWorldTransform( transform ); externalGhostObject->setWorldTransform( transform );
btScalar externalCapsuleHeight = 50; btScalar externalCapsuleHeight = 50;
@ -44,7 +44,7 @@ namespace Physic
externalGhostObject->setCollisionFlags( btCollisionObject::CF_CHARACTER_OBJECT ); externalGhostObject->setCollisionFlags( btCollisionObject::CF_CHARACTER_OBJECT );
// Internal capsule // Internal capsule
internalGhostObject = new btPairCachingGhostObject(); internalGhostObject = new PairCachingGhostObject(name);
internalGhostObject->setWorldTransform( transform ); internalGhostObject->setWorldTransform( transform );
//internalGhostObject->getBroadphaseHandle()->s //internalGhostObject->getBroadphaseHandle()->s
btScalar internalCapsuleHeight = 20; btScalar internalCapsuleHeight = 20;
@ -196,13 +196,20 @@ namespace Physic
//create the real body //create the real body
btRigidBody::btRigidBodyConstructionInfo CI = btRigidBody::btRigidBodyConstructionInfo(0,newMotionState,shape->Shape); btRigidBody::btRigidBodyConstructionInfo CI = btRigidBody::btRigidBodyConstructionInfo(0,newMotionState,shape->Shape);
RigidBody* body = new RigidBody(CI,name); RigidBody* body = new RigidBody(CI,name);
body->collide = shape->collide;
return body; return body;
} }
void PhysicEngine::addRigidBody(RigidBody* body) void PhysicEngine::addRigidBody(RigidBody* body)
{ {
dynamicsWorld->addRigidBody(body,COL_WORLD,COL_WORLD|COL_ACTOR_INTERNAL|COL_ACTOR_EXTERNAL); if(body->collide)
{
dynamicsWorld->addRigidBody(body,COL_WORLD,COL_WORLD|COL_ACTOR_INTERNAL|COL_ACTOR_EXTERNAL);
}
else
{
dynamicsWorld->addRigidBody(body,COL_WORLD,COL_NOTHING);
}
body->setActivationState(DISABLE_DEACTIVATION); body->setActivationState(DISABLE_DEACTIVATION);
RigidBodyMap[body->mName] = body; RigidBodyMap[body->mName] = body;
} }
@ -272,4 +279,26 @@ namespace Physic
void PhysicEngine::emptyEventLists(void) void PhysicEngine::emptyEventLists(void)
{ {
} }
std::pair<std::string,float> PhysicEngine::rayTest(btVector3& from,btVector3& to)
{
std::string name = "";
float d = -1.;
btCollisionWorld::ClosestRayResultCallback resultCallback(from, to);
dynamicsWorld->rayTest(from, to, resultCallback);
if (resultCallback.hasHit())
{
if(resultCallback.m_collisionFilterGroup == COL_WORLD)
{
name = static_cast<RigidBody*>(resultCallback.m_collisionObject)->mName;
}
if(resultCallback.m_collisionFilterGroup == COL_ACTOR_EXTERNAL || resultCallback.m_collisionFilterGroup == COL_ACTOR_INTERNAL)
{
name = static_cast<PairCachingGhostObject*>(resultCallback.m_collisionObject)->mName;
}
d = resultCallback.m_closestHitFraction;
}
return std::pair<std::string,float>(name,d);
}
}}; }};

View file

@ -34,6 +34,19 @@ namespace Physic
class CMotionState; class CMotionState;
struct PhysicEvent; struct PhysicEvent;
/**
*This is just used to be able to name objects.
*/
class PairCachingGhostObject : public btPairCachingGhostObject
{
public:
PairCachingGhostObject(std::string name)
:btPairCachingGhostObject(),mName(name)
{
}
std::string mName;
};
/** /**
*A physic Actor use a modifed KinematicCharacterController taken in the bullet forum. *A physic Actor use a modifed KinematicCharacterController taken in the bullet forum.
*/ */
@ -63,10 +76,10 @@ namespace Physic
btKinematicCharacterController* mCharacter; btKinematicCharacterController* mCharacter;
btPairCachingGhostObject* internalGhostObject; PairCachingGhostObject* internalGhostObject;
btCollisionShape* internalCollisionShape; btCollisionShape* internalCollisionShape;
btPairCachingGhostObject* externalGhostObject; PairCachingGhostObject* externalGhostObject;
btCollisionShape* externalCollisionShape; btCollisionShape* externalCollisionShape;
std::string mName; std::string mName;
@ -82,6 +95,9 @@ namespace Physic
public: public:
RigidBody(btRigidBody::btRigidBodyConstructionInfo& CI,std::string name); RigidBody(btRigidBody::btRigidBodyConstructionInfo& CI,std::string name);
std::string mName; std::string mName;
//is this body used for raycasting only?
bool collide;
}; };
/** /**
@ -161,6 +177,11 @@ namespace Physic
*/ */
void setDebugRenderingMode(int mode); void setDebugRenderingMode(int mode);
/**
*Return the closest object hit by a ray. If there are no objects, it will return ("",-1).
*/
std::pair<std::string,float> rayTest(btVector3& from,btVector3& to);
//event list of non player object //event list of non player object
std::list<PhysicEvent> NPEventList; std::list<PhysicEvent> NPEventList;