WorldQuery.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _WORLD_QUERY_H_
  23. #define _WORLD_QUERY_H_
  24. #ifndef _WORLD_QUERY_FILTER_H_
  25. #include "2d/scene/WorldQueryFilter.h"
  26. #endif
  27. #ifndef _WORLD_QUERY_RESULT_H_
  28. #include "2d/scene/WorldQueryResult.h"
  29. #endif
  30. ///-----------------------------------------------------------------------------
  31. class Scene;
  32. ///-----------------------------------------------------------------------------
  33. class WorldQuery :
  34. protected b2DynamicTree,
  35. public b2QueryCallback,
  36. public b2RayCastCallback,
  37. public SimObject
  38. {
  39. public:
  40. WorldQuery( Scene* pScene );
  41. virtual ~WorldQuery() {}
  42. /// Standard scope.
  43. S32 add( SceneObject* pSceneObject );
  44. void remove( SceneObject* pSceneObject );
  45. bool update( SceneObject* pSceneObject, const b2AABB& aabb, const b2Vec2& displacement );
  46. /// Always in scope.
  47. void addAlwaysInScope( SceneObject* pSceneObject );
  48. void removeAlwaysInScope( SceneObject* pSceneObject );
  49. //// World fixture queries.
  50. U32 fixtureQueryArea( const b2AABB& aabb );
  51. U32 fixtureQueryRay( const Vector2& point1, const Vector2& point2 );
  52. U32 fixtureQueryPoint( const Vector2& point );
  53. //// Render queries.
  54. U32 renderQueryArea( const b2AABB& aabb );
  55. U32 renderQueryRay( const Vector2& point1, const Vector2& point2 );
  56. U32 renderQueryPoint( const Vector2& point );
  57. /// World fixture & render queries.
  58. U32 anyQueryArea( const b2AABB& aabb );
  59. U32 anyQueryArea( const Vector2& lowerBound, const Vector2& upperBound );
  60. U32 anyQueryRay( const Vector2& point1, const Vector2& point2 );
  61. U32 anyQueryPoint( const Vector2& point );
  62. /// Filtering.
  63. inline void setQueryFilter( const WorldQueryFilter& queryFilter ) { mQueryFilter = queryFilter; }
  64. /// Results.
  65. void clearQuery( void );
  66. typeWorldQueryResultVector& getLayeredQueryResults( const U32 layer );
  67. typeWorldQueryResultVector& getQueryResults( void ) { return mQueryResults; }
  68. inline U32 getQueryResultsCount( void ) const { return mQueryResults.size(); }
  69. inline bool getIsRaycastQueryResult( void ) const { return mIsRaycastQueryResult; }
  70. void sortRaycastQueryResult( void );
  71. /// Callbacks.
  72. virtual bool ReportFixture( b2Fixture* fixture );
  73. virtual F32 ReportFixture( b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, F32 fraction );
  74. bool QueryCallback( S32 proxyId );
  75. F32 RayCastCallback( const b2RayCastInput& input, S32 proxyId );
  76. private:
  77. void injectAlwaysInScope( void );
  78. static S32 QSORT_CALLBACK rayCastFractionSort(const void* a, const void* b);
  79. private:
  80. Scene* mpScene;
  81. WorldQueryFilter mQueryFilter;
  82. bool mCheckFixturePoint;
  83. b2Vec2 mFixturePoint;
  84. typeWorldQueryResultVector mLayeredQueryResults[MAX_LAYERS_SUPPORTED];
  85. typeWorldQueryResultVector mQueryResults;
  86. bool mIsRaycastQueryResult;
  87. typeSceneObjectVector mAlwaysInScopeSet;
  88. U32 mMasterQueryKey;
  89. };
  90. #endif // _WORLD_QUERY_H_