mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-11 00:39:59 +00:00
add raycast method that returns _all_ objects that were hit
This commit is contained in:
parent
9d30a139cc
commit
17a4adfe88
@ -372,4 +372,35 @@ namespace Physic
|
||||
|
||||
return std::pair<std::string,float>(name,d);
|
||||
}
|
||||
|
||||
std::vector< std::pair<float, std::string> > PhysicEngine::rayTest2(btVector3& from, btVector3& to)
|
||||
{
|
||||
MyRayResultCallback resultCallback1;
|
||||
resultCallback1.m_collisionFilterMask = COL_WORLD;
|
||||
dynamicsWorld->rayTest(from, to, resultCallback1);
|
||||
resultCallback1.sort();
|
||||
std::vector< std::pair<float, btCollisionObject*> > results = resultCallback1.results;
|
||||
|
||||
MyRayResultCallback resultCallback2;
|
||||
resultCallback2.m_collisionFilterMask = COL_ACTOR_INTERNAL|COL_ACTOR_EXTERNAL;
|
||||
dynamicsWorld->rayTest(from, to, resultCallback2);
|
||||
resultCallback2.sort();
|
||||
std::vector< std::pair<float, btCollisionObject*> > actorResults = resultCallback2.results;
|
||||
|
||||
std::vector< std::pair<float, std::string> > results2;
|
||||
|
||||
for (std::vector< std::pair<float, btCollisionObject*> >::iterator it=results.begin();
|
||||
it != results.end(); ++it)
|
||||
{
|
||||
results2.push_back( std::make_pair( (*it).first, static_cast<RigidBody&>(*(*it).second).mName ) );
|
||||
}
|
||||
|
||||
for (std::vector< std::pair<float, btCollisionObject*> >::iterator it=actorResults.begin();
|
||||
it != actorResults.end(); ++it)
|
||||
{
|
||||
results2.push_back( std::make_pair( (*it).first, static_cast<PairCachingGhostObject&>(*(*it).second).mName ) );
|
||||
}
|
||||
|
||||
return results2;
|
||||
}
|
||||
}};
|
||||
|
@ -201,6 +201,11 @@ namespace Physic
|
||||
*/
|
||||
std::pair<std::string,float> rayTest(btVector3& from,btVector3& to);
|
||||
|
||||
/**
|
||||
* Return all objects hit by a ray.
|
||||
*/
|
||||
std::vector< std::pair<float, std::string> > rayTest2(btVector3& from, btVector3& to);
|
||||
|
||||
//event list of non player object
|
||||
std::list<PhysicEvent> NPEventList;
|
||||
|
||||
@ -225,6 +230,30 @@ namespace Physic
|
||||
bool isDebugCreated;
|
||||
};
|
||||
|
||||
|
||||
struct MyRayResultCallback : public btCollisionWorld::RayResultCallback
|
||||
{
|
||||
virtual btScalar addSingleResult( btCollisionWorld::LocalRayResult& rayResult, bool bNormalInWorldSpace)
|
||||
{
|
||||
results.push_back( std::make_pair(rayResult.m_hitFraction, rayResult.m_collisionObject) );
|
||||
return rayResult.m_hitFraction;
|
||||
}
|
||||
|
||||
static bool cmp( const std::pair<float, btCollisionObject*>& i, const std::pair<float, btCollisionObject*>& j )
|
||||
{
|
||||
if( i.first < j.first ) return false;
|
||||
if( j.first < i.first ) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void sort()
|
||||
{
|
||||
std::sort(results.begin(), results.end(), cmp);
|
||||
}
|
||||
|
||||
std::vector < std::pair<float, btCollisionObject*> > results;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user