'Provide: the transform matrix for each sprite, the width and height of each ' color map, and the color map (here called the "alpha map") Public Shared Function PixelIntersects(ByVal transformA As Microsoft.DirectX.Matrix, ByVal widthA As Integer, ByVal heightA As Integer, ByVal alphaMapA() As Byte, _ ByVal transformB As Microsoft.DirectX.Matrix, ByVal widthB As Integer, ByVal heightB As Integer, ByVal alphaMapB() As Byte) As Boolean 'Create a matrix that transforms matrix A relative to matrix B Dim transformAToB As Microsoft.DirectX.Matrix = transformA * Microsoft.DirectX.Matrix.Invert(transformB) 'for each row in A For yA As Integer = 0 To heightA - 1 'for each pixel in row in A For xA As Integer = 0 To widthA - 1 ' Get the pixel position of the B object relative to the A object Dim posInB As Microsoft.DirectX.Vector2 = Microsoft.DirectX.Vector2.TransformCoordinate(New Microsoft.DirectX.Vector2(xA, yA), transformAToB) If Single.IsNaN(posInB.X) Then Continue For If Single.IsNaN(posInB.Y) Then Continue For ' Convert to an integer position Dim xB As Integer = Convert.ToInt32(Math.Round(posInB.X)) Dim yB As Integer = Convert.ToInt32(Math.Round(posInB.Y)) ' First determine if xB, yB is within the rectangle bounds of the A object If xB >= 0 And xB < widthB And yB >= 0 And yB < heightB Then ' Get the alpha value of the A and B object Dim alphaA As Byte = alphaMapA(yA * widthA + xA) Dim alphaB As Byte = alphaMapB(yB * widthB + xB) ' If the alpha value of the point in the A object and the B object is greater than zero ' (zero = completely transparent) then a pixel intersection has occured and return true If alphaA > 0 And alphaB > 0 Then Return True End If End If Next Next ' Looped through the objects with no pixel intersections Return False End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)