NOTE

The purpose of this sample is to demonstrate a specific concept in Direct2D for Windows 8.1. The example does *not* demonstrate correct design of Windows Store apps. If your goal is to rapidly write Windows Store Apps, then you should check out these great HTML/CSS3/JavaScript courses which are *currently* available *free* of charge at the Microsoft Virtual Academy:

  1. Developing in HTML5 with JavaScript and CSS3 Jump Start
  2. Developing Windows Store Apps with HTML5 Jump Start
  3. Gaming Engines for Windows 8 Jump Start

While I am personally a power freak and like playing with DirectX (when I can), you *may* be surprised at how easy it is to quickly write a fast, fluid, rich and immersive hardware accelerated Windows Store App using the ""web"" techniques that Jeremy Foster and Michael Palermo show off in the above mentioned classes :-)

Introduction

Shows a way to perform collision testing between multiple Direct2D Geometries.

Building the Sample

Build with VS2013 and Windows 8.1 in the usual manner.

Description

This sample uses the CreateTransformedGeometry method to transform two geometries into their display context and then uses the CompareWithGeometry function to perform hit testing.  While awkward, using the CompareWithGeometry function is more desirable for hit testing of geometries than using your own math since D2D Geometries can have complex shapes. 

Using ComareWithGeometry by itself may not be straightforward because it only seems to allow application of a transform to one of the two geometries.  On the other hand, if CreateTransformedGeometry is used in conjunction with
CompareWithGeometry a transform can be applied to both geometries *before* CompareWithGeometry is called and NULL can be passed in for the respective CompareWithGeometry parameter.

 

C++
Edit|Remove
// See 
// ID2D1Factory::CreateTransformedGeometry 
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd742730(v=vs.85).aspx 
//  
// CD2DGeometry::CompareWithGeometry 
// http://msdn.microsoft.com/en-us/library/gg458172.aspx 
// 
// and  
// http://gamedev.stackexchange.com/questions/29639/what-do-i-pass-for-inputgeometrytransform-parameter-of-id2d1geometrycomparewit/29732#29732 
HRESULT TransformGeometry( 
    ID2D1Geometry *ipGeometryToTransform, 
    ID2D1Geometry **oppTranformedGeometry,  
    D2D1_MATRIX_3X2_F const & transform 
    ) 
{ 
    HRESULT hr; 
    ID2D1Factory *pFactory = NULL; 
    ipGeometryToTransform->GetFactory(&pFactory); 
 
    ID2D1TransformedGeometry *pTransformedGeometry = NULL; 
    hr = pFactory->CreateTransformedGeometry( 
        ipGeometryToTransform, 
        transform, 
        &pTransformedGeometry 
    ); 
    // See Q&A Explanation from user treckle to understand why I should release the pFactory here 
    pFactory->Release();  
     
    *oppTranformedGeometry = pTransformedGeometry; 
    (*oppTranformedGeometry)->AddRef(); 
    pTransformedGeometry->Release(); 
 
    return hr; 
} 
 
void someHitTestingFuncPseudoCode() 
{ 
    ... 
    D2D1_GEOMETRY_RELATION theRelation;  
    ID2D1Geometry * thisTGP;  
    TransformGeometry(thisGP, &thisTGP, thisTransform);  
    ID2D1Geometry * currentTGP;  
    TransformGeometry(curGP, &currentTGP, currentTransform);  
    thisTGP->CompareWithGeometry( 
        (ID2D1Geometry *)currentTGP,  
        NULL,  
        &theRelation 
    );             
    thisTGP->Release();  
    currentTGP->Release();  
    if (theRelation != D2D1_GEOMETRY_RELATION_DISJOINT) { 
        // A Collision has occured. 
    } 
    ... 
}

Source Code Files

This sample used Microsoft's Direct2D Collision Sample as a template and then expanded it to create an animated collision demo.  You can find Microsoft's Direct2D Collision Sample here:
http://code.msdn.microsoft.com/windowsapps/Direct2D-Basic-Animation-d0a5a4d1