Collision detection in Visual Basic-VBForums
Results 1 to 2 of 2

Thread: Collision detection in Visual Basic

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    17

    Collision detection in Visual Basic

    My game looks like this:



    The plane moves up and down, and the missiles go from right to left at a random height and velocity.

    I'm having trouble detecting if the missile has hit the plane, though. I have tried comparing the .Top values of the plane and the missile, and that works (although it doesn't detect the x axis) but it has to be pixel perfect, and I need it to work for the whole picture.

    Idk, i've completely run out of ideas. The ones I had failed when I tried to put it into code lol.

    Thanks for any reponses

  2. #2
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: Collision detection in Visual Basic

    The following example is intended for vb6:

    The code below shows a simple bounding box collision. Your planes and your missles make up an invisible boxes on the screen defined by:
    left=left
    right = left+width
    top=top
    bottom=bottom+height.
    These bounding boxes can be coded as RECT data types and Windows can check if they collide by creating an intersection of rectangles then checking if the new rectangle has any area.

    Put this code in a form and run and in the caption it will tell you if the rectangle controlled by the mouse has collided with the red rectangle. No math no border checks. Press any key to exit demo.


    Code:
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Declare Function IsRectEmpty Lib "user32" (lpRect As RECT) As Long
    Private Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
    Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
    
    Dim s1 As RECT
    
    Private Sub Form_KeyPress(KeyAscii As Integer)
    showmouse
    Unload Me
    End Sub
    
    Private Sub Form_Load()
    Me.hidemouse
    With Me
        .MousePointer = 15
        .BackColor = 0
        .WindowState = 2
        .AutoRedraw = True
        .ScaleMode = vbPixels
        .Show
    
        s1.Left = .ScaleWidth / 2
        s1.Right = .ScaleWidth / 2 + .ScaleWidth / 15
        s1.Top = .ScaleHeight / 2
        s1.Bottom = .ScaleHeight / 2 + .ScaleHeight / 15
        Line (s1.Left, s1.Top)-(s1.Right, s1.Bottom), RGB(255, 0, 0), B
        Me.AutoRedraw = False
    End With
    
    End Sub
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
    Dim m1 As RECT, x1 As RECT
    m1.Top = Y: m1.Bottom = Y + 15
    m1.Left = x: m1.Right = x + 15
    Me.Refresh
    Line (m1.Left, m1.Top)-(m1.Right, m1.Bottom), RGB(0, 0, 255), B
    IntersectRect x1, m1, s1
    If IsRectEmpty(x1) Then
        Me.Caption = "Out of the box"
    Else
        Me.Caption = "In the box"
    
    End If
    End Sub
    Sub showmouse()
    Dim RET As Long
        Do Until RET > 0
            RET = ShowCursor(1&)
        Loop
    End Sub
    Sub hidemouse()
    Dim RET As Long
        Do Until RET < 0
            RET = ShowCursor(0&)
        Loop
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    showmouse
    End Sub
    And if I were you, I'd get rid of the sun it's gonna melt that snowman!
    Last edited by technorobbo; Jul 14th, 2009 at 06:05 AM.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.