1 year ago

#364604

test-img

Average Capitalist

VB6 pixel based object control Collision Detection

Currently, I'm trying to make a simple platformer in VB6 and I'm drawing objects onto the WinForm using BitBlt. I can program rectangular, circular, and line collisions but I want to have pixel-based collision detection.

I assume this can be done with Windows GDI APIs, like MaskBlt, StretchBlt, BitBlt, PltBlt... etc, but I can't get my head around how this would work out.

This is my current VB code for the form:

I currently have 2 PictureBox controls (one for player sprite, one for mask) and one timer control, and another PictureBox which is an obstacle.

Option Explicit

Dim Keys(255) As Boolean

Dim Player As PlayerPos

Private Type PlayerPos
    x As Integer
    y As Integer
End Type

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Keys(KeyCode) = True
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Keys(KeyCode) = False
End Sub

Private Sub timTick_Timer()
    Cls
    ' Draw collision obj
    BitBlt Me.hDC, 100, 100, pic3.Width \ 15, pic3.Height \ 15, pic3.hDC, 0, 0, vbSrcAnd
    BitBlt Me.hDC, 100, 100, pic3.Width \ 15, pic3.Height \ 15, pic3.hDC, 0, 0, vbSrcPaint
    ' End Draw
    With Player
        Dim Result(0 To 2) As Integer
        If Keys(vbKeyUp) Then .y = .y - 5
        If Keys(vbKeyDown) Then .y = .y + 5
        If Keys(vbKeyRight) Then .x = .x + 5
        If Keys(vbKeyLeft) Then .x = .x - 5
        ' Draw player
        Result(0) = BitBlt(Me.hDC, .x, .y, pic1.Width \ 15, pic1.Height \ 15, pic1.hDC, 0, 0, vbSrcAnd)
        Result(1) = BitBlt(Me.hDC, .x, .y, pic1.Width \ 15, pic1.Height \ 15, pic1Mask.hDC, 0, 0, vbSrcPaint)
        Result(2) = BitBlt(Me.hDC, .x, .y, pic1.Width \ 15, pic1.Height \ 15, pic1.hDC, 0, 0, vbSrcAnd)
        ' End Draw
        lblTest.Caption = Result(0) & Result(1) & Result(2)
    End With
    Me.Refresh
End Sub

Thanks in advance!

vb6

collision-detection

collision

0 Answers

Your Answer

Accepted video resources