SpriteBatchQuery.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include "2d/core/SpriteBatchQuery.h"
  23. #ifndef _SPRITE_BATCH_H_
  24. #include "2d/core/spriteBatch.h"
  25. #endif
  26. #ifndef _SPRITE_BATCH_ITEM_H_
  27. #include "2d/core/spriteBatchItem.h"
  28. #endif
  29. // Debug Profiling.
  30. #include "debug/profiler.h"
  31. //-----------------------------------------------------------------------------
  32. SpriteBatchQuery::SpriteBatchQuery( SpriteBatch* pSpriteBatch ) :
  33. mpSpriteBatch(pSpriteBatch),
  34. mIsRaycastQueryResult(false),
  35. mMasterQueryKey(0),
  36. mCheckPoint(false),
  37. mComparePoint(0.0f, 0.0f)
  38. {
  39. // Set debug associations.
  40. VECTOR_SET_ASSOCIATION( mQueryResults );
  41. // Clear the query.
  42. clearQuery();
  43. }
  44. //-----------------------------------------------------------------------------
  45. S32 SpriteBatchQuery::add( SpriteBatchItem* pSpriteBatchItem )
  46. {
  47. // Debug Profiling.
  48. PROFILE_SCOPE(SpriteBatchQuery_Add);
  49. return CreateProxy( pSpriteBatchItem->getLocalAABB(), pSpriteBatchItem );
  50. }
  51. //-----------------------------------------------------------------------------
  52. void SpriteBatchQuery::remove( SpriteBatchItem* pSpriteBatchItem )
  53. {
  54. // Debug Profiling.
  55. PROFILE_SCOPE(SpriteBatchQuery_Remove);
  56. DestroyProxy( pSpriteBatchItem->getProxyId() );
  57. }
  58. //-----------------------------------------------------------------------------
  59. bool SpriteBatchQuery::update( SpriteBatchItem* pSpriteBatchItem, const b2AABB& aabb, const b2Vec2& displacement )
  60. {
  61. // Debug Profiling.
  62. PROFILE_SCOPE(SpriteBatchQuery_Update);
  63. return MoveProxy( pSpriteBatchItem->getProxyId(), aabb, displacement );
  64. }
  65. //-----------------------------------------------------------------------------
  66. U32 SpriteBatchQuery::queryArea( const b2AABB& aabb, const bool targetOOBB )
  67. {
  68. // Debug Profiling.
  69. PROFILE_SCOPE(SpriteBatchQuery_QueryArea);
  70. mMasterQueryKey++;
  71. // Flag as not a ray-cast query result.
  72. mIsRaycastQueryResult = false;
  73. // Query.
  74. b2Vec2 verts[4];
  75. verts[0].Set( aabb.lowerBound.x, aabb.lowerBound.y );
  76. verts[1].Set( aabb.upperBound.x, aabb.lowerBound.y );
  77. verts[2].Set( aabb.upperBound.x, aabb.upperBound.y );
  78. verts[3].Set( aabb.lowerBound.x, aabb.upperBound.y );
  79. mComparePolygonShape.Set( verts, 4 );
  80. mCompareTransform.SetIdentity();
  81. mCheckOOBB = targetOOBB;
  82. Query( this, aabb );
  83. mCheckOOBB = false;
  84. return getQueryResultsCount();
  85. }
  86. //-----------------------------------------------------------------------------
  87. U32 SpriteBatchQuery::queryRay( const Vector2& point1, const Vector2& point2, const bool targetOOBB )
  88. {
  89. // Debug Profiling.
  90. PROFILE_SCOPE(SpriteBatchQuery_QueryRay);
  91. mMasterQueryKey++;
  92. // Flag as a ray-cast query result.
  93. mIsRaycastQueryResult = true;
  94. // Query.
  95. mCompareRay.p1 = point1;
  96. mCompareRay.p2 = point2;
  97. mCompareRay.maxFraction = 1.0f;
  98. mCompareTransform.SetIdentity();
  99. mCheckOOBB = targetOOBB;
  100. RayCast( this, mCompareRay );
  101. mCheckOOBB = false;
  102. return getQueryResultsCount();
  103. }
  104. //-----------------------------------------------------------------------------
  105. U32 SpriteBatchQuery::queryPoint( const Vector2& point, const bool targetOOBB )
  106. {
  107. // Debug Profiling.
  108. PROFILE_SCOPE(SpriteBatchQuery_QueryPoint);
  109. mMasterQueryKey++;
  110. // Flag as not a ray-cast query result.
  111. mIsRaycastQueryResult = false;
  112. // Query.
  113. b2AABB aabb;
  114. aabb.lowerBound = point;
  115. aabb.upperBound = point;
  116. mCompareTransform.SetIdentity();
  117. mCheckOOBB = targetOOBB;
  118. Query( this, aabb );
  119. mCheckOOBB = false;
  120. return getQueryResultsCount();
  121. }
  122. //-----------------------------------------------------------------------------
  123. void SpriteBatchQuery::clearQuery( void )
  124. {
  125. mQueryResults.clear();
  126. }
  127. //-----------------------------------------------------------------------------
  128. void SpriteBatchQuery::sortRaycastQueryResult( void )
  129. {
  130. // Debug Profiling.
  131. PROFILE_SCOPE(SpriteBatchQuery_SortRayCastQueryResult);
  132. // Ignore if not a ray-cast query result or there are not results to sort.
  133. if ( !getIsRaycastQueryResult() || getQueryResultsCount() == 0 )
  134. return;
  135. // Sort query results.
  136. dQsort( mQueryResults.address(), mQueryResults.size(), sizeof(SpriteBatchQueryResult), rayCastFractionSort );
  137. }
  138. //-----------------------------------------------------------------------------
  139. bool SpriteBatchQuery::QueryCallback( S32 proxyId )
  140. {
  141. // Debug Profiling.
  142. PROFILE_SCOPE(SpriteBatchQuery_QueryCallback);
  143. // Fetch sprite batch item.
  144. SpriteBatchItem* pSpriteBatchItem = static_cast<SpriteBatchItem*>(GetUserData( proxyId ));
  145. // Ignore if already tagged with the sprite batch query key.
  146. if ( pSpriteBatchItem->getSpriteBatchQueryKey() == mMasterQueryKey )
  147. return true;
  148. // Check OOBB.
  149. if ( mCheckOOBB )
  150. {
  151. // Fetch the shapes render OOBB.
  152. b2PolygonShape oobb;
  153. oobb.Set( pSpriteBatchItem->getRenderOOBB(), 4);
  154. if ( mCheckPoint )
  155. {
  156. if ( !oobb.TestPoint( mCompareTransform, mComparePoint ) )
  157. return true;
  158. }
  159. else
  160. {
  161. if ( !b2TestOverlap( &mComparePolygonShape, 0, &oobb, 0, mCompareTransform, mCompareTransform ) )
  162. return true;
  163. }
  164. }
  165. // Tag with world query key.
  166. pSpriteBatchItem->setSpriteBatchQueryKey( mMasterQueryKey );
  167. SpriteBatchQueryResult queryResult( pSpriteBatchItem );
  168. mQueryResults.push_back( queryResult );
  169. return true;
  170. }
  171. //-----------------------------------------------------------------------------
  172. F32 SpriteBatchQuery::RayCastCallback( const b2RayCastInput& input, S32 proxyId )
  173. {
  174. // Debug Profiling.
  175. PROFILE_SCOPE(SpriteBatchQuery_RayCastCallback);
  176. // Fetch sprite batch item.
  177. SpriteBatchItem* pSpriteBatchItem = static_cast<SpriteBatchItem*>(GetUserData( proxyId ));
  178. // Ignore if already tagged with the sprite batch query key.
  179. if ( pSpriteBatchItem->getSpriteBatchQueryKey() == mMasterQueryKey )
  180. return 1.0f;
  181. // Check OOBB.
  182. if ( mCheckOOBB )
  183. {
  184. // Fetch the shapes render OOBB.
  185. b2PolygonShape oobb;
  186. oobb.Set( pSpriteBatchItem->getRenderOOBB(), 4);
  187. b2RayCastOutput rayOutput;
  188. if ( !oobb.RayCast( &rayOutput, mCompareRay, mCompareTransform, 0 ) )
  189. return true;
  190. }
  191. // Tag with world query key.
  192. pSpriteBatchItem->setSpriteBatchQueryKey( mMasterQueryKey );
  193. SpriteBatchQueryResult queryResult( pSpriteBatchItem );
  194. mQueryResults.push_back( queryResult );
  195. return 1.0f;
  196. }
  197. //-----------------------------------------------------------------------------
  198. S32 QSORT_CALLBACK SpriteBatchQuery::rayCastFractionSort(const void* a, const void* b)
  199. {
  200. // Debug Profiling.
  201. PROFILE_SCOPE(SpriteBatchQuery_RayCastFractionSort);
  202. // Fetch scene objects.
  203. SpriteBatchQueryResult* pQueryResultA = (SpriteBatchQueryResult*)a;
  204. SpriteBatchQueryResult* pQueryResultB = (SpriteBatchQueryResult*)b;
  205. // Fetch fractions.
  206. const F32 queryFractionA = pQueryResultA->mFraction;
  207. const F32 queryFractionB = pQueryResultB->mFraction;
  208. if ( queryFractionA < queryFractionB )
  209. return -1;
  210. if ( queryFractionA > queryFractionB )
  211. return 1;
  212. return 0;
  213. }