Ver Fonte

Merge pull request #69 from capnlove/CompositeSprite_Fix

CompositeSprite Fix - All Picking modes functional
Simon Love há 12 anos atrás
pai
commit
0cf42ee45a

+ 1 - 0
engine/source/2d/core/SpriteBatchItem.h

@@ -291,6 +291,7 @@ public:
 
     virtual void copyTo( SpriteBatchItem* pSpriteBatchItem ) const;
 
+    inline const Vector2* getLocalOOBB( void ) const { return mLocalOOBB; }
     inline const Vector2* getRenderOOBB( void ) const { return mRenderOOBB; }
 
     void prepareRender( SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId );

+ 26 - 2
engine/source/2d/core/SpriteBatchQuery.cc

@@ -81,6 +81,30 @@ bool SpriteBatchQuery::update( SpriteBatchItem* pSpriteBatchItem, const b2AABB&
 
 //-----------------------------------------------------------------------------
 
+U32 SpriteBatchQuery::queryOOBB( const b2AABB& aabb, b2PolygonShape& oobb, const bool targetOOBB )
+{
+  // This function is used exclusively when picking rectangular areas using CompositeSprite's pickArea ConsoleMethod
+  // For rendering, SpriteBatchQuery::queryArea is used instead
+
+  // Debug Profiling.
+    PROFILE_SCOPE(SpriteBatchQuery_QueryArea);
+
+    mMasterQueryKey++;
+
+    // Flag as not a ray-cast query result.
+    mIsRaycastQueryResult = false;
+
+  mComparePolygonShape.Set(oobb.m_vertices,4);
+  mComparePolygonShape.m_centroid = oobb.m_centroid;
+
+    mCompareTransform.SetIdentity();
+    mCheckOOBB = targetOOBB;
+    Query( this, aabb );
+    mCheckOOBB = false;
+
+    return getQueryResultsCount();
+}
+
 U32 SpriteBatchQuery::queryArea( const b2AABB& aabb, const bool targetOOBB )
 {
     // Debug Profiling.
@@ -195,7 +219,7 @@ bool SpriteBatchQuery::QueryCallback( S32 proxyId )
     {
             // Fetch the shapes render OOBB.
         b2PolygonShape oobb;
-        oobb.Set( pSpriteBatchItem->getRenderOOBB(), 4);
+        oobb.Set( pSpriteBatchItem->getLocalOOBB(), 4);
 
         if ( mCheckPoint )
         {
@@ -237,7 +261,7 @@ F32 SpriteBatchQuery::RayCastCallback( const b2RayCastInput& input, S32 proxyId
     {
         // Fetch the shapes render OOBB.
         b2PolygonShape oobb;
-        oobb.Set( pSpriteBatchItem->getRenderOOBB(), 4);
+        oobb.Set( pSpriteBatchItem->getLocalOOBB(), 4);
         b2RayCastOutput rayOutput;
         if ( !oobb.RayCast( &rayOutput, mCompareRay, mCompareTransform, 0 ) )
             return true;

+ 1 - 0
engine/source/2d/core/SpriteBatchQuery.h

@@ -50,6 +50,7 @@ public:
 
     //// Spatial queries.
     U32             queryArea( const b2AABB& aabb, const bool targetOOBB );
+    U32             queryOOBB( const b2AABB& aabb, b2PolygonShape& oobb, const bool targetOOBB );
     U32             queryRay( const Vector2& point1, const Vector2& point2, const bool targetOOBB );
     U32             queryPoint( const Vector2& point, const bool targetOOBB );
  

+ 14 - 10
engine/source/2d/sceneobject/CompositeSprite_ScriptBinding.h

@@ -1021,13 +1021,6 @@ ConsoleMethod(CompositeSprite, pickArea, const char*, 4, 6, "(startx/y, endx/y )
         return NULL;
     }
 
-    // Fetch the render transform.
-    const b2Transform& renderTransform = object->getRenderTransform();
-    
-    // Translate into local space.
-    v1 -= renderTransform.p;
-    v2 -= renderTransform.p;
-
     // Calculate normalized AABB.
     b2AABB aabb;
     aabb.lowerBound.x = getMin( v1.x, v2.x );
@@ -1035,11 +1028,22 @@ ConsoleMethod(CompositeSprite, pickArea, const char*, 4, 6, "(startx/y, endx/y )
     aabb.upperBound.x = getMax( v1.x, v2.x );
     aabb.upperBound.y = getMax( v1.y, v2.y );
 
-    // Rotate the AABB into local space.
-    CoreMath::mRotateAABB( aabb, -renderTransform.q.GetAngle(), aabb );
+	// Calculate local OOBB.
+    b2Vec2 localOOBB[4];
+    CoreMath::mAABBtoOOBB( aabb, localOOBB );
+    CoreMath::mCalculateInverseOOBB( localOOBB, object->getRenderTransform(), localOOBB );
+
+	// Calculate local AABB.
+    b2AABB localAABB;
+    CoreMath::mOOBBtoAABB( localOOBB, localAABB );
+  
+	// Convert OOBB to a PolygonShape
+    b2PolygonShape oobb_polygon;
+    oobb_polygon.Set(localOOBB, 4);
 
     // Perform query.
-    pSpriteBatchQuery->queryArea( aabb, true );
+    pSpriteBatchQuery->queryOOBB( localAABB, oobb_polygon, true );
+
 
     // Fetch result count.
     const U32 resultCount = pSpriteBatchQuery->getQueryResultsCount();