1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 07:23:51 +00:00

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();
// 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<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;
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<std::string,float> rayTest(btVector3& from,btVector3& to);
//event list of non player object
std::list<PhysicEvent> NPEventList;