Jump to content
  • Advertisement
Sign in to follow this  
amigagamedev

Pixel Perfect Collision Detection

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need a Vb.net directx9 collision detection example. I encountered problem with locking texture (texture.lockrectangle) and reading. Any example? Thank you

Share this post


Link to post
Share on other sites
Advertisement
You should not lock textures and read back data to use with collision detection. Reading data from video card is very slow. Instead, use a "collision image" kept in system memory. This image can be black & white image, where black pixels are solid. Then you just use this image to do pixel-perfect collision detection. I'm not sure which is the best method for doing that, but I'm sure others can help you out.

Btw, wasn't there a thread about this already, just a couple of days ago? [rolleyes]

Share this post


Link to post
Share on other sites
How can i realize a collision image? Can you help me?

Yes, i've posted another message two days ago but i have not yet solved my problem.
I'm using direct3d sprite for a 2d game and i can't find function to perform a pixel collision detection.

This code work fine on vb6 DX7 but not in vb.net and directx 9
******************************************************************************************************
Public Function CheckForPPCollision(DDSurf1 As DirectDrawSurface7, DDSurf2 As DirectDrawSurface7, RECT1 As RECT, RECT2 As RECT, BlitCOLORKEY As Integer) As Boolean

Dim RECTOverlap As RECT 'Used to record the overlap from RECT1 and RECT2
Dim RECT1Overlap As RECT 'Overlaped portions of RECT1
Dim RECT2Overlap As RECT 'Overlaped portions of RECT2
Dim OverlapWidth As Integer 'Determine the width of the overlap
Dim OverlapHeight As Integer 'Determine the height of the overlap
Dim ByteObj1() As Byte 'Used to analyse a pixel
Dim ByteObj2() As Byte 'Used to analyse a pixel
Dim DDSDBlank As DDSURFACEDESC2 'For use in (DDSurf1.Lock) (DDSurf2.Lock)
Dim i As Integer, j As Integer 'Just for use in loops
Dim PPCollision As Boolean 'States whether we have PixelPerfect collision
'Check for rectangular collisions
If IntersectRect(RECTOverlap, RECT1, RECT2) Then
'RECTANGULAR COLLISION

'Get the RECT structures for the overlapped portions of both surfaces
With RECT1Overlap 'Find the overlap difference in the first RECT
.Top = RECTOverlap.Top - RECT1.Top
.Bottom = RECTOverlap.Bottom - RECT1.Top
.Right = RECTOverlap.Right - RECT1.Left
.Left = RECTOverlap.Left - RECT1.Left
End With

With RECT2Overlap 'Find the overlap difference in the second RECT
.Top = RECTOverlap.Top - RECT2.Top
.Bottom = RECTOverlap.Bottom - RECT2.Top
.Right = RECTOverlap.Right - RECT2.Left
.Left = RECTOverlap.Left - RECT2.Left
End With

'Determine the width and height of the ovrelas (we will use this information for the loop)
OverlapWidth = RECTOverlap.Right - RECTOverlap.Left - 1
OverlapHeight = RECTOverlap.Bottom - RECTOverlap.Top - 1

'Use Lock and GetLockedArray on each surface
DDSurf1.Lock RECT1Overlap, DDSDBlank, DDLOCK_READONLY Or DDLOCK_WAIT, 0
DDSurf1.GetLockedArray ByteObj1
DDSurf2.Lock RECT2Overlap, DDSDBlank, DDLOCK_READONLY Or DDLOCK_WAIT, 0
DDSurf2.GetLockedArray ByteObj2

'Compare the surface data from the overlapping portions of the rectangles
For i = 0 To OverlapWidth
For j = 0 To OverlapHeight
'If BOTH surfaces are non-tranparent at this pixel...
If (ByteObj1(i + RECT1Overlap.Left, j + RECT1Overlap.Top) <> BlitCOLORKEY) And (ByteObj2(i + RECT2Overlap.Left, j + RECT2Overlap.Top) <> BlitCOLORKEY) Then PPCollision = True
'We have Pixel Perfect Collision
If PPCollision = True Then
CheckForPPCollision = True
Exit For 'Exit because we don't need to check anymore, we already have pixel perfect collision
End If
Next j
If PPCollision = True Then
CheckForPPCollision = True
Exit For 'Exit because we don't need to check anymore, we already have pixel perfect collision
End If
Next i

'Unlock the sufaces
DDSurf1.Unlock RECT1Overlap 'unlock DDsurf1
DDSurf2.Unlock RECT2Overlap 'Unlock DDSurf2
End If
End Function
:(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!