123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "2d/scene/WorldQuery.h"
- #ifndef _SCENE_H_
- #include "Scene.h"
- #endif
- #ifndef _SCENE_OBJECT_H_
- #include "2d/sceneobject/SceneObject.h"
- #endif
- // Debug Profiling.
- #include "debug/profiler.h"
- //-----------------------------------------------------------------------------
- WorldQuery::WorldQuery( Scene* pScene ) :
- mpScene(pScene),
- mIsRaycastQueryResult(false),
- mMasterQueryKey(0),
- mCheckPoint(false),
- mCheckAABB(false),
- mCheckOOBB(false),
- mCheckCircle(false)
- {
- // Set debug associations.
- for ( U32 n = 0; n < MAX_LAYERS_SUPPORTED; n++ )
- {
- VECTOR_SET_ASSOCIATION( mLayeredQueryResults[n] );
- }
- VECTOR_SET_ASSOCIATION( mQueryResults );
- // Clear the query.
- clearQuery();
- }
- //-----------------------------------------------------------------------------
- S32 WorldQuery::add( SceneObject* pSceneObject )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_Add);
- return CreateProxy( pSceneObject->getAABB(), static_cast<PhysicsProxy*>(pSceneObject) );
- }
- //-----------------------------------------------------------------------------
- void WorldQuery::remove( SceneObject* pSceneObject )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_Remove);
- DestroyProxy( pSceneObject->getWorldProxy() );
- }
- //-----------------------------------------------------------------------------
- bool WorldQuery::update( SceneObject* pSceneObject, const b2AABB& aabb, const b2Vec2& displacement )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_Update);
- return MoveProxy( pSceneObject->getWorldProxy(), aabb, displacement );
- }
- //-----------------------------------------------------------------------------
- void WorldQuery::addAlwaysInScope( SceneObject* pSceneObject )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AddAlwaysInScope);
- // Sanity!
- for( typeSceneObjectVector::iterator itr = mAlwaysInScopeSet.begin(); itr != mAlwaysInScopeSet.end(); ++itr )
- {
- AssertFatal( (*itr) != pSceneObject, "Object attempted to be in Always-in-Scope more than once." );
- }
- // Add to always-in-scope.
- mAlwaysInScopeSet.push_back( pSceneObject );
- // Set always in scope.
- pSceneObject->mAlwaysInScope = true;
- }
- //-----------------------------------------------------------------------------
- void WorldQuery::removeAlwaysInScope( SceneObject* pSceneObject )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_RemoveAlwaysInScope);
- // Remove from always-in-scope.
- for( typeSceneObjectVector::iterator itr = mAlwaysInScopeSet.begin(); itr != mAlwaysInScopeSet.end(); ++itr )
- {
- // Skip if not object.
- if ( (*itr) != pSceneObject )
- continue;
- mAlwaysInScopeSet.erase_fast( itr );
- // Reset always in scope.
- pSceneObject->mAlwaysInScope = false;
- return;
- }
- AssertFatal( false, "Object attempted to be removed from Always-in-Scope but wasn't present." );
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::collisionQueryAABB( const b2AABB& aabb )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_collisionQueryAABB);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2Vec2 verts[4];
- verts[0].Set( aabb.lowerBound.x, aabb.lowerBound.y );
- verts[1].Set( aabb.upperBound.x, aabb.lowerBound.y );
- verts[2].Set( aabb.upperBound.x, aabb.upperBound.y );
- verts[3].Set( aabb.lowerBound.x, aabb.upperBound.y );
- mComparePolygonShape.Set( verts, 4 );
- mCompareTransform.SetIdentity();
- mCheckAABB = true;
- mpScene->getWorld()->QueryAABB( this, aabb );
- mCheckAABB = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::collisionQueryRay( const Vector2& point1, const Vector2& point2 )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_CollisionQueryRay);
- mMasterQueryKey++;
- // Flag as a ray-cast query result.
- mIsRaycastQueryResult = true;
- // Query.
- mpScene->getWorld()->RayCast( this, point1, point2 );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::collisionQueryPoint( const Vector2& point )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_CollisionQueryPoint);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2AABB aabb;
- aabb.lowerBound = point;
- aabb.upperBound = point;
- mCheckPoint = true;
- mComparePoint = point;
- mpScene->getWorld()->QueryAABB( this, aabb );
- mCheckPoint = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::collisionQueryCircle( const Vector2& centroid, const F32 radius )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_CollisionQueryCircle);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2AABB aabb;
- mCompareTransform.SetIdentity();
- mCompareCircleShape.m_p = centroid;
- mCompareCircleShape.m_radius = radius;
- mCompareCircleShape.ComputeAABB( &aabb, mCompareTransform, 0 );
- mCheckCircle = true;
- mpScene->getWorld()->QueryAABB( this, aabb );
- mCheckCircle = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::aabbQueryAABB( const b2AABB& aabb )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_aabbQueryAABB);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- Query( this, aabb );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::aabbQueryRay( const Vector2& point1, const Vector2& point2 )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AABBQueryRay);
- mMasterQueryKey++;
- // Flag as a ray-cast query result.
- mIsRaycastQueryResult = true;
- // Query.
- mCompareRay.p1 = point1;
- mCompareRay.p2 = point2;
- mCompareRay.maxFraction = 1.0f;
- mCompareTransform.SetIdentity();
- RayCast( this, mCompareRay );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::aabbQueryPoint( const Vector2& point )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AABBQueryPoint);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2AABB aabb;
- aabb.lowerBound = point;
- aabb.upperBound = point;
- Query( this, aabb );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::aabbQueryCircle( const Vector2& centroid, const F32 radius )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AABBQueryCircle);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2AABB aabb;
- mCompareTransform.SetIdentity();
- mCompareCircleShape.m_p = centroid;
- mCompareCircleShape.m_radius = radius;
- mCompareCircleShape.ComputeAABB( &aabb, mCompareTransform, 0 );
- mCheckCircle = true;
- Query( this, aabb );
- mCheckCircle = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::oobbQueryAABB( const b2AABB& aabb )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_aabbQueryAABB);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2Vec2 verts[4];
- verts[0].Set( aabb.lowerBound.x, aabb.lowerBound.y );
- verts[1].Set( aabb.upperBound.x, aabb.lowerBound.y );
- verts[2].Set( aabb.upperBound.x, aabb.upperBound.y );
- verts[3].Set( aabb.lowerBound.x, aabb.upperBound.y );
- mComparePolygonShape.Set( verts, 4 );
- mCompareTransform.SetIdentity();
- mCheckOOBB = true;
- mCheckAABB = true;
- Query( this, aabb );
- mCheckAABB = false;
- mCheckOOBB = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::oobbQueryRay( const Vector2& point1, const Vector2& point2 )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AABBQueryRay);
- mMasterQueryKey++;
- // Flag as a ray-cast query result.
- mIsRaycastQueryResult = true;
- // Query.
- mCompareRay.p1 = point1;
- mCompareRay.p2 = point2;
- mCompareRay.maxFraction = 1.0f;
- mCompareTransform.SetIdentity();
- mCheckOOBB = true;
- RayCast( this, mCompareRay );
- mCheckOOBB = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::oobbQueryPoint( const Vector2& point )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AABBQueryPoint);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2AABB aabb;
- aabb.lowerBound = point;
- aabb.upperBound = point;
- mComparePoint = point;
- mCompareTransform.SetIdentity();
- mCheckOOBB = true;
- mCheckPoint = true;
- Query( this, aabb );
- mCheckPoint = false;
- mCheckOOBB = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::oobbQueryCircle( const Vector2& centroid, const F32 radius )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_OOBBQueryCircle);
- mMasterQueryKey++;
- // Flag as not a ray-cast query result.
- mIsRaycastQueryResult = false;
- // Query.
- b2AABB aabb;
- mCompareTransform.SetIdentity();
- mCompareCircleShape.m_p = centroid;
- mCompareCircleShape.m_radius = radius;
- mCompareCircleShape.ComputeAABB( &aabb, mCompareTransform, 0 );
- mCheckOOBB = true;
- mCheckCircle = true;
- Query( this, aabb );
- mCheckCircle = false;
- mCheckOOBB = false;
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::anyQueryAABB( const b2AABB& aabb )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_anyQueryAABBAABB);
- // Query.
- oobbQueryAABB( aabb );
- mMasterQueryKey--;
- collisionQueryAABB( aabb );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- U32 WorldQuery::anyQueryArea(const Vector2& lower, const Vector2& upper)
- {
- b2AABB aabb;
- aabb.lowerBound.Set(getMin(lower.x, lower.x), getMin(lower.x, upper.x));
- aabb.upperBound.Set(getMax(lower.x, upper.x), getMax(lower.x, upper.x));
- return anyQueryAABB(aabb);
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::anyQueryRay( const Vector2& point1, const Vector2& point2 )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AnyQueryRay);
- // Query.
- oobbQueryRay( point1, point2 );
- mMasterQueryKey--;
- collisionQueryRay( point1, point2 );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::anyQueryPoint( const Vector2& point )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AnyQueryPoint);
- // Query.
- oobbQueryPoint( point );
- mMasterQueryKey--;
- collisionQueryPoint( point );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- U32 WorldQuery::anyQueryCircle( const Vector2& centroid, const F32 radius )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_AnyQueryCircle);
- // Query.
- oobbQueryCircle( centroid, radius );
- mMasterQueryKey--;
- collisionQueryCircle( centroid, radius );
- // Inject always-in-scope.
- injectAlwaysInScope();
- return getQueryResultsCount();
- }
- //-----------------------------------------------------------------------------
- void WorldQuery::clearQuery( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_ClearQuery);
- for ( U32 n = 0; n < MAX_LAYERS_SUPPORTED; n++ )
- {
- mLayeredQueryResults[n].clear();
- }
- mQueryResults.clear();
- }
- //-----------------------------------------------------------------------------
- typeWorldQueryResultVector& WorldQuery::getLayeredQueryResults( const U32 layer )
- {
- // Sanity!
- AssertFatal( layer < MAX_LAYERS_SUPPORTED, "WorldQuery::getResults() - Layer out of range." );
- return mLayeredQueryResults[ layer ];
- }
- //-----------------------------------------------------------------------------
- void WorldQuery::sortRaycastQueryResult( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_SortRayCastQueryResult);
- // Ignore if not a ray-cast query result or there are not results to sort.
- if ( !getIsRaycastQueryResult() || getQueryResultsCount() == 0 )
- return;
- // Sort query results.
- dQsort( mQueryResults.address(), mQueryResults.size(), sizeof(WorldQueryResult), rayCastFractionSort );
- for ( U32 layer = 0; layer < MAX_LAYERS_SUPPORTED; ++layer )
- {
- // Fetch layer query results.
- typeWorldQueryResultVector& layerQueryResults = mLayeredQueryResults[layer];
- // Skip if nothing in the layer query results.
- if ( layerQueryResults.size() == 0 )
- continue;
- // Sort query results.
- dQsort( layerQueryResults.address(), layerQueryResults.size(), sizeof(WorldQueryResult), rayCastFractionSort );
- }
- }
- //-----------------------------------------------------------------------------
- bool WorldQuery::ReportFixture( b2Fixture* fixture )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_ReportFixture);
- // If not the correct proxy then ignore.
- PhysicsProxy* pPhysicsProxy = static_cast<PhysicsProxy*>(fixture->GetBody()->GetUserData());
- if ( pPhysicsProxy->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT )
- return true;
- // Fetch scene object.
- SceneObject* pSceneObject = static_cast<SceneObject*>(pPhysicsProxy);
- // Ignore if already tagged with the world query key.
- if ( pSceneObject->getWorldQueryKey() == mMasterQueryKey )
- return true;
- // Enabled filter.
- if ( mQueryFilter.mEnabledFilter && !pSceneObject->isEnabled() )
- return true;
- // Visible filter.
- if ( mQueryFilter.mVisibleFilter && !pSceneObject->getVisible() )
- return true;
- // Picking allowed filter.
- if ( mQueryFilter.mPickingAllowedFilter && !pSceneObject->getPickingAllowed() )
- return true;
- // Check collision point.
- if ( mCheckPoint && !fixture->TestPoint( mComparePoint ) )
- return true;
- // Check collision AABB.
- if ( mCheckAABB )
- if ( !b2TestOverlap( &mComparePolygonShape, 0, fixture->GetShape(), 0, mCompareTransform, fixture->GetBody()->GetTransform() ) )
- return true;
- // Check collision circle.
- if ( mCheckCircle )
- if ( !b2TestOverlap( &mCompareCircleShape, 0, fixture->GetShape(), 0, mCompareTransform, fixture->GetBody()->GetTransform() ) )
- return true;
- // Fetch layer and group masks.
- const U32 sceneLayerMask = pSceneObject->getSceneLayerMask();
- const U32 sceneGroupMask = pSceneObject->getSceneGroupMask();
- // Compare masks and report.
- if ( (mQueryFilter.mSceneLayerMask & sceneLayerMask) != 0 && (mQueryFilter.mSceneGroupMask & sceneGroupMask) != 0 )
- {
- WorldQueryResult queryResult( pSceneObject );
- mLayeredQueryResults[pSceneObject->getSceneLayer()].push_back( queryResult );
- mQueryResults.push_back( queryResult );
- // Tag with world query key.
- pSceneObject->setWorldQueryKey( mMasterQueryKey );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- F32 WorldQuery::ReportFixture( b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, F32 fraction )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_ReportFixtureRay);
- // If not the correct proxy then ignore.
- PhysicsProxy* pPhysicsProxy = static_cast<PhysicsProxy*>(fixture->GetBody()->GetUserData());
- if ( pPhysicsProxy->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT )
- return 1.0f;
- // Fetch scene object.
- SceneObject* pSceneObject = static_cast<SceneObject*>(pPhysicsProxy);
- // Ignore if already tagged with the world query key.
- if ( pSceneObject->getWorldQueryKey() == mMasterQueryKey )
- return 1.0f;
- // Enabled filter.
- if ( mQueryFilter.mEnabledFilter && !pSceneObject->isEnabled() )
- return 1.0f;
- // Visible filter.
- if ( mQueryFilter.mVisibleFilter && !pSceneObject->getVisible() )
- return 1.0f;
- // Picking allowed filter.
- if ( mQueryFilter.mPickingAllowedFilter && !pSceneObject->getPickingAllowed() )
- return 1.0f;
- // Fetch layer and group masks.
- const U32 sceneLayerMask = pSceneObject->getSceneLayerMask();
- const U32 sceneGroupMask = pSceneObject->getSceneGroupMask();
- // Fetch collision shape index.
- const S32 shapeIndex = pSceneObject->getCollisionShapeIndex( fixture );
- // Sanity!
- AssertFatal( shapeIndex >= 0, "WorldQuery::ReportFixture() - Cannot find shape index reported on physics proxy of a fixture." );
- // Compare masks and report.
- if ( (mQueryFilter.mSceneLayerMask & sceneLayerMask) != 0 && (mQueryFilter.mSceneGroupMask & sceneGroupMask) != 0 )
- {
- WorldQueryResult queryResult( pSceneObject, point, normal, fraction, (U32)shapeIndex );
- mLayeredQueryResults[pSceneObject->getSceneLayer()].push_back( queryResult );
- mQueryResults.push_back( queryResult );
- // Tag with world query key.
- pSceneObject->setWorldQueryKey( mMasterQueryKey );
- }
- return 1.0f;
- }
- //-----------------------------------------------------------------------------
- bool WorldQuery::QueryCallback( S32 proxyId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_QueryCallback);
- // If not the correct proxy then ignore.
- PhysicsProxy* pPhysicsProxy = static_cast<PhysicsProxy*>(GetUserData( proxyId ));
- if ( pPhysicsProxy->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT )
- return true;
- // Fetch scene object.
- SceneObject* pSceneObject = static_cast<SceneObject*>(pPhysicsProxy);
- // Ignore if already tagged with the world query key.
- if ( pSceneObject->getWorldQueryKey() == mMasterQueryKey )
- return true;
- // Enabled filter.
- if ( mQueryFilter.mEnabledFilter && !pSceneObject->isEnabled() )
- return true;
- // Visible filter. If an object has a size x or y value of zero then they are treated here as invisible.
- if (mQueryFilter.mVisibleFilter && (!pSceneObject->getVisible() || pSceneObject->getSize().isXZero() || pSceneObject->getSize().isYZero()))
- return true;
- // Picking allowed filter.
- if ( mQueryFilter.mPickingAllowedFilter && !pSceneObject->getPickingAllowed() )
- return true;
- // Check OOBB.
- if ( mCheckOOBB )
- {
- // Fetch the shapes render OOBB.
- b2PolygonShape oobb;
- oobb.Set( pSceneObject->getRenderOOBB(), 4);
- // Check point.
- if ( mCheckPoint )
- {
- if ( !oobb.TestPoint( mCompareTransform, mComparePoint ) )
- return true;
- }
- // Check AABB.
- else if ( mCheckAABB )
- {
- if ( !b2TestOverlap( &mComparePolygonShape, 0, &oobb, 0, mCompareTransform, mCompareTransform ) )
- return true;
- }
- // Check circle.
- else if ( mCheckCircle )
- {
- if ( !b2TestOverlap( &mCompareCircleShape, 0, &oobb, 0, mCompareTransform, mCompareTransform ) )
- return true;
- }
- }
- // Check circle.
- else if ( mCheckCircle )
- {
- // Fetch the shapes AABB.
- b2AABB aabb = pSceneObject->getAABB();
- b2Vec2 verts[4];
- verts[0].Set( aabb.lowerBound.x, aabb.lowerBound.y );
- verts[1].Set( aabb.upperBound.x, aabb.lowerBound.y );
- verts[2].Set( aabb.upperBound.x, aabb.upperBound.y );
- verts[3].Set( aabb.lowerBound.x, aabb.upperBound.y );
- b2PolygonShape shapeAABB;
- shapeAABB.Set( verts, 4);
- if ( !b2TestOverlap( &mCompareCircleShape, 0, &shapeAABB, 0, mCompareTransform, mCompareTransform ) )
- return true;
- }
- // Fetch layer and group masks.
- const U32 sceneLayerMask = pSceneObject->getSceneLayerMask();
- const U32 sceneGroupMask = pSceneObject->getSceneGroupMask();
- // Compare masks and report.
- if ( (mQueryFilter.mSceneLayerMask & sceneLayerMask) != 0 && (mQueryFilter.mSceneGroupMask & sceneGroupMask) != 0 )
- {
- WorldQueryResult queryResult( pSceneObject );
- mLayeredQueryResults[pSceneObject->getSceneLayer()].push_back( queryResult );
- mQueryResults.push_back( queryResult );
- // Tag with world query key.
- pSceneObject->setWorldQueryKey( mMasterQueryKey );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- F32 WorldQuery::RayCastCallback( const b2RayCastInput& input, S32 proxyId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_RayCastCallback);
- // If not the correct proxy then ignore.
- PhysicsProxy* pPhysicsProxy = static_cast<PhysicsProxy*>(GetUserData( proxyId ));
- if ( pPhysicsProxy->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT )
- return 1.0f;
- // Fetch scene object.
- SceneObject* pSceneObject = static_cast<SceneObject*>(pPhysicsProxy);
- // Ignore if already tagged with the world query key.
- if ( pSceneObject->getWorldQueryKey() == mMasterQueryKey )
- return 1.0f;
- // Enabled filter.
- if ( mQueryFilter.mEnabledFilter && !pSceneObject->isEnabled() )
- return 1.0f;
- // Visible filter.
- if ( mQueryFilter.mVisibleFilter && !pSceneObject->getVisible() )
- return 1.0f;
- // Picking allowed filter.
- if ( mQueryFilter.mPickingAllowedFilter && !pSceneObject->getPickingAllowed() )
- return 1.0f;
- // Check OOBB.
- if ( mCheckOOBB )
- {
- // Fetch the shapes render OOBB.
- b2PolygonShape oobb;
- oobb.Set( pSceneObject->getRenderOOBB(), 4);
- b2RayCastOutput rayOutput;
- if ( !oobb.RayCast( &rayOutput, mCompareRay, mCompareTransform, 0 ) )
- return true;
- }
- // Fetch layer and group masks.
- const U32 sceneLayerMask = pSceneObject->getSceneLayerMask();
- const U32 sceneGroupMask = pSceneObject->getSceneGroupMask();
- // Compare masks and report.
- if ( (mQueryFilter.mSceneLayerMask & sceneLayerMask) != 0 && (mQueryFilter.mSceneGroupMask & sceneGroupMask) != 0 )
- {
- WorldQueryResult queryResult( pSceneObject );
- mLayeredQueryResults[pSceneObject->getSceneLayer()].push_back( queryResult );
- mQueryResults.push_back( queryResult );
- // Tag with world query key.
- pSceneObject->setWorldQueryKey( mMasterQueryKey );
- }
- return 1.0f;
- }
- //-----------------------------------------------------------------------------
- void WorldQuery::injectAlwaysInScope( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_InjectAlwaysInScope);
- // Finish if filtering always-in-scope.
- if ( mQueryFilter.mAlwaysInScopeFilter )
- return;
- // Iterate always-in-scope.
- for( typeSceneObjectVector::iterator itr = mAlwaysInScopeSet.begin(); itr != mAlwaysInScopeSet.end(); ++itr )
- {
- // Fetch scene object.
- SceneObject* pSceneObject = (*itr);
- // Ignore if already tagged with the world query key.
- if ( pSceneObject->getWorldQueryKey() == mMasterQueryKey )
- continue;
- // Enabled filter.
- if ( mQueryFilter.mEnabledFilter && !pSceneObject->isEnabled() )
- continue;
- // Visible filter.
- if ( mQueryFilter.mVisibleFilter && !pSceneObject->getVisible() )
- continue;
- // Picking allowed filter.
- if ( mQueryFilter.mPickingAllowedFilter && !pSceneObject->getPickingAllowed() )
- continue;
- // Fetch layer and group masks.
- const U32 sceneLayerMask = pSceneObject->getSceneLayerMask();
- const U32 sceneGroupMask = pSceneObject->getSceneGroupMask();
- // Compare masks and report.
- if ( (mQueryFilter.mSceneLayerMask & sceneLayerMask) != 0 && (mQueryFilter.mSceneGroupMask & sceneGroupMask) != 0 )
- {
- WorldQueryResult queryResult( pSceneObject );
- mLayeredQueryResults[pSceneObject->getSceneLayer()].push_back( queryResult );
- mQueryResults.push_back( queryResult );
- // Tag with world query key.
- pSceneObject->setWorldQueryKey( mMasterQueryKey );
- }
- }
- }
- //-----------------------------------------------------------------------------
- S32 QSORT_CALLBACK WorldQuery::rayCastFractionSort(const void* a, const void* b)
- {
- // Debug Profiling.
- PROFILE_SCOPE(WorldQuery_RayCastFractionSort);
- // Fetch scene objects.
- WorldQueryResult* pQueryResultA = (WorldQueryResult*)a;
- WorldQueryResult* pQueryResultB = (WorldQueryResult*)b;
- // Fetch fractions.
- const F32 queryFractionA = pQueryResultA->mFraction;
- const F32 queryFractionB = pQueryResultB->mFraction;
- if ( queryFractionA < queryFractionB )
- return -1;
- if ( queryFractionA > queryFractionB )
- return 1;
- return 0;
- }
|