VB2008 - Space Invader Collision Detection Help!

Need help with my collision detection for my game, please

Page 1 of 1

3 Replies - 3020 Views - Last Post: 28 December 2010 - 05:13 PM Rate Topic: -----

#1 uniquex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-December 10

VB2008 - Space Invader Collision Detection Help!

Posted 28 December 2010 - 04:22 AM

Okay, i've looked up several tutorials on what i should do to create a collision detection, but im not too positive on how to function my ideas and convert them to code.
Basically, i'm creating a space invaders like-game that is 2D Mario-themed (sprites used).
I am using a panel for my enemies and Mario is being controlled by the movement of arrow keys and space button shoots.
The problem is I can not make my bullets collide with the enemies to make them disappear.

This piece of code is what i've got so far with the collision detection :

'*******************************************************************************************************
    '   Collision occurs whenever the pctShoot(podoboo) hits any of the enemy
    '   When blnTrue, both pctShoot and the enemy disappear '*******************************************************************************************************

    Private Sub tmrCollisionDetect_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCollisionDetect.Tick
        '****************************************************************************************
        'Current method using; no collision occurs
        'not positive on what ctlEnemy is suppose to be (a single picturebox or the panel (?))
        'purpose of the tags are like "health" when they reach to zero, enemy disappears
        '   ->purpose of this function is to help the timing issue 
        'time interval set to 1
        '***************************************************************************************

        Dim rect As New Rectangle
        Dim intScore As Integer
        Dim ctlEnemy As Control = pnlEnemies

        For Each c As Control In pnlEnemies.Controls 'all picture boxes in the panel are controlled  

            If TypeOf c Is PictureBox Then  'and "c" is a picture box in the panel

                If pnlEnemies.Visible = True AndAlso pctBullet.Visible = True AndAlso rect.IntersectsWith(pctBullet.Bounds) Then
                    pnlEnemies.Tag = CType(pnlEnemies.Tag, Integer) - 1 'converts the tag to integer to claim an enemies "health"
                    blnCollison = True 'collision happens
                    pctBullet.Visible = False 'bullet disappears

                    If CInt(pnlEnemies.Tag) <= 0 Then 'when enemies health reaches 0 or lower
                        blnCollison = True 'collision activated
                        c.Visible = False 'enemy disappears
                        intScore = intScore + 100 'score accumulated by 100
                    End If
                End If
            End If

        Next
        Me.lblScore.Text = intScore 'score becomes updated


        '*****************************************************************************************
        'Last method used, it works but when playing, the bullet doesn't hit exactly on the enemy
        '   -> bullet hits enemy from one side, the collision occurs on the opposite side
        '   ->said to be due to a timing issue
        '*****************************************************************************************

        'For Each c As Control In pnlEnemies.Controls 'all picture boxes in the panel are controlled

        '    If TypeOf c Is PictureBox Then
        '        If pctBullet.Visible And c.Visible Then
        '            If c.Bounds.IntersectsWith(pctBullet.Bounds) Then
        '                blnCollison = True 'Collsion is true
        '                c.Visible = False
        '            Else
        '                blnCollison = False
        '                c.Visible = True
        '            End If
        '        End If
        '    End If
        'Next
    End Sub


While I'm at it, i have another question in concern; but this is not really related to collision but:
How do you make an object move down automatically using a timer but make it appear randomly??
Like any space invader game, the enemies are shooting back at the hero to make it appear more challenging right?

Any help is appreciative! Please and THANK YOUS IN ADVANCE!
(using Visual Basic 2008)

Is This A Good Question/Topic? 0
  • +

Replies To: VB2008 - Space Invader Collision Detection Help!

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: VB2008 - Space Invader Collision Detection Help!

Posted 28 December 2010 - 05:22 AM

Your rect variable has not been defined. New Rectangle(x, y, width, height)
Was This Post Helpful? 0
  • +
  • -

#3 uniquex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-December 10

Re: VB2008 - Space Invader Collision Detection Help!

Posted 28 December 2010 - 02:50 PM

View Posthawkvalley1, on 28 December 2010 - 04:22 AM, said:

Your rect variable has not been defined. New Rectangle(x, y, width, height)


Oh okay. But i dont understand what x and y defines, even when i read the description. what does it mean by "upper left corner of rectangle"??
is it possible to give the rectangle a background color so i can visually see it?

This post has been edited by uniquex: 28 December 2010 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: VB2008 - Space Invader Collision Detection Help!

Posted 28 December 2010 - 05:13 PM

x, y define where the rectangle is located in pixels. x is left going right and gets larger as it goes right, y is top going down and gets larger as it goes down. The x, y coordinates are relative to the container or another rectangle do you define. You draw/fill the rectangle in the paint event of the container you are drawing it in. Look into GDI+ for drawing.

This post has been edited by hawkvalley1: 28 December 2010 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1