From ada4616fd3932ee6e8883d420108a203014573f3 Mon Sep 17 00:00:00 2001 From: gugus Date: Tue, 22 Feb 2011 20:53:02 +0100 Subject: [PATCH] add raycasting --- bullet/physic.cpp | 37 +++++++++++++++++++++++++++++++++---- bullet/physic.hpp | 25 +++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/bullet/physic.cpp b/bullet/physic.cpp index d5c504cfd..76dabce5d 100644 --- a/bullet/physic.cpp +++ b/bullet/physic.cpp @@ -31,7 +31,7 @@ namespace Physic transform.setIdentity(); // External capsule - externalGhostObject = new btPairCachingGhostObject(); + externalGhostObject = new PairCachingGhostObject(name); externalGhostObject->setWorldTransform( transform ); btScalar externalCapsuleHeight = 50; @@ -44,7 +44,7 @@ namespace Physic externalGhostObject->setCollisionFlags( btCollisionObject::CF_CHARACTER_OBJECT ); // Internal capsule - internalGhostObject = new btPairCachingGhostObject(); + internalGhostObject = new PairCachingGhostObject(name); internalGhostObject->setWorldTransform( transform ); //internalGhostObject->getBroadphaseHandle()->s btScalar internalCapsuleHeight = 20; @@ -196,13 +196,20 @@ namespace Physic //create the real body btRigidBody::btRigidBodyConstructionInfo CI = btRigidBody::btRigidBodyConstructionInfo(0,newMotionState,shape->Shape); RigidBody* body = new RigidBody(CI,name); - + body->collide = shape->collide; return 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); RigidBodyMap[body->mName] = body; } @@ -272,4 +279,26 @@ namespace Physic void PhysicEngine::emptyEventLists(void) { } + + std::pair 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(resultCallback.m_collisionObject)->mName; + } + if(resultCallback.m_collisionFilterGroup == COL_ACTOR_EXTERNAL || resultCallback.m_collisionFilterGroup == COL_ACTOR_INTERNAL) + { + name = static_cast(resultCallback.m_collisionObject)->mName; + } + d = resultCallback.m_closestHitFraction; + } + return std::pair(name,d); + } }}; \ No newline at end of file diff --git a/bullet/physic.hpp b/bullet/physic.hpp index 5fdc03e7d..062e69e02 100644 --- a/bullet/physic.hpp +++ b/bullet/physic.hpp @@ -34,6 +34,19 @@ namespace Physic class CMotionState; 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. */ @@ -63,10 +76,10 @@ namespace Physic btKinematicCharacterController* mCharacter; - btPairCachingGhostObject* internalGhostObject; + PairCachingGhostObject* internalGhostObject; btCollisionShape* internalCollisionShape; - btPairCachingGhostObject* externalGhostObject; + PairCachingGhostObject* externalGhostObject; btCollisionShape* externalCollisionShape; std::string mName; @@ -82,6 +95,9 @@ namespace Physic public: RigidBody(btRigidBody::btRigidBodyConstructionInfo& CI,std::string name); std::string mName; + + //is this body used for raycasting only? + bool collide; }; /** @@ -161,6 +177,11 @@ namespace Physic */ void setDebugRenderingMode(int mode); + /** + *Return the closest object hit by a ray. If there are no objects, it will return ("",-1). + */ + std::pair rayTest(btVector3& from,btVector3& to); + //event list of non player object std::list NPEventList;