Code-Beispiel
FBImage (Sprite) PixelKollision
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
k. A. | Eternal_Pain | 15.05.2013 |
Ein relativ einfaches Beispiel für eine Pixelperfect Sprite Kollisions-Funktion
Die Funktion überprüft zunächst ob die Rects (Boundingbox) der beiden Images miteinander Kollidieren und dann ob sich nicht-transparente Pixel 'überlappen' und somit eben kollidieren...
Function FBImageCollision(byref Image1 as any ptr, byref ScreenPos1X as Integer, byref ScreenPos1Y as Integer, _
byref Image2 as any ptr, byref ScreenPos2X as Integer, byref ScreenPos2Y as Integer, _
byval TransparentColor as Integer = &hFF00FF) as Integer'Bool
Dim as Integer Image1Width, Image1Height
Dim as Integer Image2Width, Image2Height
ImageInfo Image1, Image1Width, Image1Height
ImageInfo Image2, Image2Width, Image2Height
Dim as Integer Xl, Yo, Xr, Yu
Xl = ScreenPos1X
Yo = ScreenPos1Y
Xr = ScreenPos2X + Image2Width
Yu = ScreenPos2Y + Image2Height
If (ScreenPos2X < ScreenPos1X) Then Xl = ScreenPos2X
If (ScreenPos2Y < ScreenPos1Y) Then Yo = ScreenPos2Y
If ((ScreenPos1X+Image1Width ) > (ScreenPos2X + Image2Width )) Then Xr = ScreenPos1X + Image1Width
If ((ScreenPos1Y+Image1Height) > (ScreenPos2Y + Image2Height)) Then Yu = ScreenPos1Y + Image1Height
Dim as Integer PosX, PosY
Dim as Integer Col1, Col2
If ((Image1Width+Image2Width) > (Xr-Xl)) and ((Image1Height + Image2Height) > (Yu - Yo)) Then
'RectCollision...
For Y as Integer = 0 to (Yu-Yo)
For X as Integer = 0 to (Xr-Xl)
PosX = Xl + X
PosY = Yo + Y
If (PosX >= ScreenPos1X) and (PosY >= ScreenPos1Y) and (PosX < (ScreenPos1X+Image1Width)) and (PosY < (ScreenPos1Y+Image1Height)) _
and (PosX >= ScreenPos2X) and (PosY >= ScreenPos2Y) and (PosX < (ScreenPos2X+Image2Width)) and (PosY < (ScreenPos2Y+Image2Height)) Then
If (((Xl+X) - ScreenPos1X) > -1) and (((Xl+X) - ScreenPos1X) < Image1Width ) _
and (((Yo+Y) - ScreenPos1Y) > -1) and (((Yo+Y) - ScreenPos1Y) < Image1Height) Then
Col1 = Point((Xl+X) - ScreenPos1X,(Yo+Y) - ScreenPos1Y,Image1) and &hFFFFFF
Else
Col1 = TransparentColor and &hFFFFFF
End If
If (((Xl+X) - ScreenPos2X) > -1) and (((Xl+X) - ScreenPos2X) < Image2Width ) _
and (((Yo+Y) - ScreenPos2Y) > -1) and (((Yo+Y) - ScreenPos2Y) < Image2Height) Then
Col2 = Point((Xl+X) - ScreenPos2X,(Yo+Y) - ScreenPos2Y,Image2) and &hFFFFFF
Else
Col2 = TransparentColor and &hFFFFFF
End If
If (Col1<>(TransparentColor and &hFFFFFF)) and (Col2<>(TransparentColor and &hFFFFFF)) Then Return (Not 0)'PixelCollision...
End If
Next X
Next Y
End If
Return 0 'No Collision
End Function
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|
|