WorldQuery.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 collision-shape queries.
  50. U32 collisionQueryAABB( const b2AABB& aabb );
  51. U32 collisionQueryRay( const Vector2& point1, const Vector2& point2 );
  52. U32 collisionQueryPoint( const Vector2& point );
  53. U32 collisionQueryCircle( const Vector2& centroid, const F32 radius );
  54. /// AABB queries.
  55. U32 aabbQueryAABB( const b2AABB& aabb );
  56. U32 aabbQueryRay( const Vector2& point1, const Vector2& point2 );
  57. U32 aabbQueryPoint( const Vector2& point );
  58. U32 aabbQueryCircle( const Vector2& centroid, const F32 radius );
  59. /// OOBB queries.
  60. U32 oobbQueryAABB( const b2AABB& aabb );
  61. U32 oobbQueryRay( const Vector2& point1, const Vector2& point2 );
  62. U32 oobbQueryPoint( const Vector2& point );
  63. U32 oobbQueryCircle( const Vector2& centroid, const F32 radius );
  64. /// Any queries.
  65. U32 anyQueryAABB( const b2AABB& aabb );
  66. U32 anyQueryArea(const Vector2 & lower, const Vector2 & upper);
  67. U32 anyQueryRay( const Vector2& point1, const Vector2& point2 );
  68. U32 anyQueryPoint( const Vector2& point );
  69. U32 anyQueryCircle( const Vector2& centroid, const F32 radius );
  70. /// Filtering.
  71. inline void setQueryFilter( const WorldQueryFilter& queryFilter ) { mQueryFilter = queryFilter; }
  72. /// Results.
  73. void clearQuery( void );
  74. typeWorldQueryResultVector& getLayeredQueryResults( const U32 layer );
  75. typeWorldQueryResultVector& getQueryResults( void ) { return mQueryResults; }
  76. inline U32 getQueryResultsCount( void ) const { return mQueryResults.size(); }
  77. inline bool getIsRaycastQueryResult( void ) const { return mIsRaycastQueryResult; }
  78. void sortRaycastQueryResult( void );
  79. /// Callbacks.
  80. virtual bool ReportFixture( b2Fixture* fixture );
  81. virtual F32 ReportFixture( b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, F32 fraction );
  82. bool QueryCallback( S32 proxyId );
  83. F32 RayCastCallback( const b2RayCastInput& input, S32 proxyId );
  84. private:
  85. void injectAlwaysInScope( void );
  86. static S32 QSORT_CALLBACK rayCastFractionSort(const void* a, const void* b);
  87. private:
  88. Scene* mpScene;
  89. WorldQueryFilter mQueryFilter;
  90. b2PolygonShape mComparePolygonShape;
  91. b2CircleShape mCompareCircleShape;
  92. b2RayCastInput mCompareRay;
  93. b2Vec2 mComparePoint;
  94. b2Transform mCompareTransform;
  95. bool mCheckPoint;
  96. bool mCheckAABB;
  97. bool mCheckOOBB;
  98. bool mCheckCircle;
  99. typeWorldQueryResultVector mLayeredQueryResults[MAX_LAYERS_SUPPORTED];
  100. typeWorldQueryResultVector mQueryResults;
  101. bool mIsRaycastQueryResult;
  102. typeSceneObjectVector mAlwaysInScopeSet;
  103. U32 mMasterQueryKey;
  104. };
  105. #endif // _WORLD_QUERY_H_