Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working
Galaga Hit Detection not working Galaga Hit Detection not working
Galaga Hit Detection not working
Go Back  Xtreme Visual Basic Talk > > > Galaga Hit Detection not working


Reply
 
Thread Tools Display Modes
  #1  
Old 05-14-2011, 10:38 AM
Mr Espresso Mr Espresso is offline
Newcomer
 
Join Date: May 2011
Posts: 2
Default Galaga Hit Detection not working


Hi guys, I'm having trouble with hit detection in a game i'm working on and can't seem to figure it out. I made another game before that also has hit detection and that works fine but when i tried to translate the code from that one to the Galaga game its doesn't seem to work the same.

Heres my code, its all commented so you can see what you need.
Code:
Public Class GameForm
    'Player Variables
    Dim Direction As String
    Dim Shoot As Boolean = False
    Dim NumOfBullets As Integer
    Dim GameOver As Boolean

    'Enemy Variables
    Dim Aliens(32) As Aliens
    Dim NoOfAliens As Integer
    Dim AlienMove As Integer
    Dim AlienCounter As Integer
    Dim Alien1 As PictureBox
    Dim Alien2 As PictureBox
    Dim Alien3 As PictureBox
    Dim rnd As New Random


    Private Sub GameForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Generates the Enemies on Form
        GenEnemies()

        'Activates Timers
        PlayerTimer.Enabled = True
        EnemyTimer.Enabled = True

        'Controls Enemy Speed
        AlienMove = 5

        AlienCounter = 100
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Sets the Players movement to Keys
        If (e.KeyData = Keys.Left) Then
            Direction = "l"
        ElseIf (e.KeyData = Keys.Right) Then
            Direction = "r"
        End If
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        'Stops Player when key is released
        If (e.KeyData = Keys.Left) Or (e.KeyData = Keys.Right) Then
            Direction = ""
        End If
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        'Assigns Space to shooting the bullet
        Select Case e.KeyChar = " "
            Case Keys.Down
                PlayerBullet.Visible = True
                PlayerBullet.Location = New Point(PlayerShip.Left + 15, PlayerShip.Top - 20)
        End Select

        e.Handled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayerTimer.Tick
        'Controls Score
        ScoreLbl.Text = 0

        'Controls Lives
        LiveLbl.Text = 3

        'Ship movement
        If (PlayerShip.Left >= 0 And PlayerShip.Right <= 1000) Then
            If (Direction = "r") Then
                PlayerShip.Left += 8
            ElseIf (Direction = "l") Then
                PlayerShip.Left -= 8
            End If
        Else
            Direction = ""
            If (PlayerShip.Left < 0) Then
                PlayerShip.Left = 0
            ElseIf (PlayerShip.Right > 595) Then
                PlayerShip.Left = 595 - PlayerShip.Width
            End If
        End If

        'Firing Bullets
        If PlayerBullet.Visible Then
            PlayerBullet.Top -= 20
        Else
            PlayerBullet.Top = 664
            PlayerBullet.Left = PlayerShip.Left + 18
        End If

        If (PlayerBullet.Bottom < 0) Then
            Shoot = False
        End If

        'Hit Detection for Bullet
        For i As Integer = 0 To NoOfAliens
            If Aliens(i).Status = AlienState.active Then
                Aliens(i).Left = Aliens(i).Left + Aliens(i).speed
                If PlayerBullet.Visible = True Then
                    If PlayerBullet.Top + PlayerBullet.Height > Aliens(i).Top And PlayerBullet.Top < Aliens(i).Top + Aliens(i).Height And PlayerBullet.Left + PlayerBullet.Width > Aliens(i).Left And PlayerBullet.Left < Aliens(i).Left + Aliens(i).Width Then
                        Aliens(i).Status = AlienState.dead
                        PlayerBullet.Visible = False
                        ScoreLbl.Text = ScoreLbl.Text + Aliens(i).GetScore
                    End If
                End If
            ElseIf Aliens(i).Status = AlienState.dead Then
                Aliens(i).Visible = False
            End If

            If Aliens(i).Direction = ScreenDirection.left Then
                If Aliens(i).Left + Aliens(i).Width <= 0 Then
                    Aliens(i).Left = Aliens(i).Width + Me.Width
                End If
            Else
                If Aliens(i).Left > (Aliens(i).Width + Me.Width) Then
                    Aliens(i).Left = -Aliens(i).Width * 2
                End If
            End If

        Next

        GameOver = True

        For i As Integer = 0 To NoOfAliens
            If Aliens(i).Status <> AlienState.dead Then
                GameOver = False
            End If
        Next

        If GameOver = True Then
            EnemyTimer.Stop()
            PlayerTimer.Stop()
            MessageBox.Show("Objective Complete")
        End If
    End Sub


    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnemyTimer.Tick

        'Controls Enemy Movement
        For i = 0 To 31
            Aliens(i).Left -= AlienMove
        Next
        AlienCounter -= AlienMove
        If AlienCounter < 0 Or AlienCounter > 200 Then
            AlienMove *= -1
        End If
        'Controls Enemy Missles


        'Hit Detection for Enemy Missles

        'Enemy Dive
    End Sub


    Sub GenEnemies()
        'Generates Enemies on Form
        For x = 1 To 8
            For y = 1 To 2
                Aliens(((x - 1) * 2 + y) - 1) = New FirstAlien(x, y)
                Me.Controls.Add(Aliens(((x - 1) * 2 + y) - 1))
            Next
        Next


        For x = 1 To 6
            For y = 1 To 2
                Aliens(((x - 1) * 2 + y) + 15) = New SecondAlien(x, y)
                Me.Controls.Add(Aliens(((x - 1) * 2 + y) + 15))
            Next
        Next


        For x = 1 To 4
            For y = 1 To 1
                Aliens(((x - 1) + y) + 27) = New StrongAlien(x, y)
                Me.Controls.Add(Aliens(((x - 1) + y) + 27))
            Next
        Next
    End Sub

    Private Sub RestartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestartToolStripMenuItem.Click
        'Resets the game
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'Exits the game back to the Main Menu
        Me.Hide()
        StartForm.Show()
    End Sub

    Private Sub ScoreLbl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScoreLbl.Click

    End Sub
End Class
My Aliens are in a outside Class so here they are if you need to see them
Code:
Public Class Aliens
    Inherits System.Windows.Forms.PictureBox

    Protected Score As Integer = 0
    Protected Type As AlienType
    Private mStatus As AlienState
    Private mSpeed As Integer
    Private mDirection As ScreenDirection

    ReadOnly Property GetScore()
        Get
            Return Score
        End Get
    End Property

    ReadOnly Property GetAlienType()
        Get
            Return Type
        End Get
    End Property

    Property Status() As AlienState
        Get
            Return mStatus
        End Get
        Set(ByVal value As AlienState)
            mStatus = value
        End Set
    End Property

    Property Direction() As ScreenDirection
        Get
            Return mDirection
        End Get
        Set(ByVal value As ScreenDirection)
            mDirection = value
            If value = ScreenDirection.left Then
                mSpeed = -System.Math.Abs(mSpeed)
            Else
                mSpeed = System.Math.Abs(mSpeed)
            End If
        End Set
    End Property

    Property speed() As Integer
        Get
            Return mSpeed
        End Get
        Set(ByVal value As Integer)
            mSpeed = value
            If mDirection = ScreenDirection.left Then
                mSpeed = -System.Math.Abs(mSpeed)
            Else
                mSpeed = System.Math.Abs(mSpeed)
            End If
        End Set
    End Property
End Class

Public Enum AlienType
    FirstAlien = 0
    SecondAlien = 1
    StrongAlien = 2
End Enum

Public Enum AlienState
    hiding = 0
    active = 1
    dead = 2
End Enum

Public Enum ScreenDirection
    left = 0
    right = 1
End Enum

Public Class FirstAlien
    Inherits Aliens
    Public Sub New(ByVal x, ByVal y)
        Size = New System.Drawing.Size(44, 38)
        Image = GameForm.pbAlien1.Image
        Location = New System.Drawing.Point((x * 50) + 60, (y * 40) + 250)
        Score = 10
    End Sub
End Class

Public Class SecondAlien
    Inherits Aliens
    Public Sub New(ByVal x, ByVal y)
        Size = New System.Drawing.Size(33, 39)
        Image = GameForm.pbAlien2.Image
        Location = New System.Drawing.Point((x * 50) + 115, (y * 40) + 160)
        Score = 20
    End Sub
End Class

Public Class StrongAlien
    Inherits Aliens
    Public Sub New(ByVal x, ByVal y)
        Size = New System.Drawing.Size(47, 52)
        Image = GameForm.pbAlien3.Image
        Location = New System.Drawing.Point((x * 50) + 155, (y * 40) + 100)
        Score = 50
    End Sub
End Class
Reply With Quote
  #2  
Old 05-14-2011, 03:03 PM
AtmaWeapon's Avatar
AtmaWeaponGalaga Hit Detection not working AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

If you're not careful, you can have a situation where this is never true. If the bullet is smaller than the alien and it's moving fast enough, it can move beyond its hit window before you check if it hits. For example, here's two frames that cause it:
Code:
Frame 1
_________
|       |
| alien |
|_______|
   * <- bullet 
   
------------------------

Frame 2

   *
_________
|       |
| alien |
|_______|
There's a few things to consider.

First, have a look at my collision detection article. It explains the math behind figuring out the collisions by hand and points out a nice one-line shortcut. Figuring out if two rectangles collide is simple, and once you find a working solution it's best not to re-derive it.

However, when elements move quickly, you can end up skipping a collision by a frame like above. For example, say your bullets move 20 pixels/frame. That's a lot of movement. You might sit down and realize that if they move more than 5 pixels in a frame you might miss a collision. You can handle this a few ways.

You might introduce "virtual" frames. Instead of moving the bullet 20 pixels, you might move it 5 at a time and check for collisions:
Code:
For quarterFrameIndex As Integer = 1 To 4
    bullet.Y += bullet.VelocityY \ 4
    If CheckCollision(bullet) Then
        ' The bullet hit!
    End If
Next
You also might decide to rewrite your collision code to include ranges. For example, if the bullet was at Y=30, you know it will be at Y=50. If the alien is from Y=32 to Y=48, you can tell that the bullet must pass through the alien in the vertical direction:
Code:
Dim isVerticalCollision As Boolean = False
Dim bulletNow As Integer = bullet.Y
Dim bulletFuture As Integer = bullet.Y + bullet.VelocityY
If bulletNow > alien.Bottom AndAlso bulletFuture < alien.Bottom Then
    ' The bullet had to have passed through or stopped inside the alien
    isVerticalCollision = True
End If
bullet.Y = bulletFuture
...
Either way, you have to account for the fact that in your game your bullet is teleporting from place to place rather than moving in a continuous line.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 05-15-2011, 03:09 AM
Mr Espresso Mr Espresso is offline
Newcomer
 
Join Date: May 2011
Posts: 2
Default

Thanks for the help, I will try and implement what you said and see what i get from it.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working Galaga Hit Detection not working
Galaga Hit Detection not working
Galaga Hit Detection not working
 
Galaga Hit Detection not working
Galaga Hit Detection not working
 
-->