Direct2D Collision Sample

Shows a way to perform collision testing between multiple Direct2D Geometries. Uses the CreateTransformedGeometry method to transform two geometries into their display context and then uses the CompareWithGeometry function to perform hit testing.

C++ (71.9 KB)
5 Star
4,301 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Great Example, thanks, but had some funky results!
    1 Posts | Last post January 20, 2016
    • Put this code into my program and got it to work, pretty straight forward.
      However, I was getting collisions when the two object where not touching,
      at random positions around the screen.
      What I think it was, was declaring thisTransform and not flushing its memory
      was giving me an odd transformation on the geometries I was comparing.
      To resolve this, I changed the following lines:
      from: D2D1_MATRIX_3X2_F thisTransform;
      to:   D2D1_MATRIX_3X2_F thisTransform = D2D1::Matrix3x2F::Translation(0,0);
      also change this for currentTransform.
      Was this the correct fix, a possible fix, how would you have addressed the
      random collisions?
  • GetFactory
    3 Posts | Last post October 31, 2012
    • Hi Shawn-
      Nice sample!
      Looking at your Description for the sample, I see that the code snippet for TransformGeometry calls GetFactory, but then doesn't call pFactory->Release() at the end of the method. This can result in a leak since GetFactory increments the reference count of the ID2D1Factory.
      Also, in CollisionObject::renderCO you have a note saying that you have to return NULL otherwise the sample won't work. This is because NULL happens to have the same value as S_OK (which is the HRESULT you probably meant to return).
    • treckle:  
      Thanks for taking the time to correct me and inform the community.  I really appreciate quality feedback like this.  I hope to be able to address your concerns shortly after I complete my retail upgrade to Windows 8.  I'm looking forward to 26-OCT-2012.
      Thanks a Bunch!
    • treckle:
      I think I addressed your concerns.  Let me know if there is anything else.  I am hoping to start working on a simple Direct3D Win8 app soon. 
      Thanks a Bunch!
  • why compiler error?
    3 Posts | Last post September 30, 2012
    • Error	1	error C2035: a non-virtual destructor with 'internal' accessibility is not allowed for this type	d:\evgeniy\visual studio 2012\projects\direct2d collision s1\d2dcollision.h	35	1	D2DCollision
    • Sorry for the inconvenience.  I wrote the tutorial for VS2012 RC and time constraints haven't let me get back to it since VS2012 and Win8 went RTM.  
      I'm just now getting the chance to look at the code again and I have duplicated the error you mentioned in (VS2012 Express for Windows 8) running on Windows 8 Enterprise Evaluation Build 9200.  I "should" be able to resolve the issues in the next few days. 
    • I moved some deallocation code out of a destructor and into the implementation of an uninitialize virtual method.  I then re-uploaded the sample.  I'm sure there are other things that can be improved, but the sample *appears* to be working in (VS2012 Express for Windows 8) on Windows 8 Enterprise Evaluation Build 9200 now.
      I'm sorry I missed this.  I've just been really busy lately.