Wyo VB Lecture Notes

Objective #1: Use collision detection when to detect whether an object hits a line.

Objective #2: Use collision detection to detect when two objects collide with each other

Objective #3: Use collision detection to make a PictureBox object "bounce back" when it hits a line

        Dim horizontalMovement As Integer = 0    ' amount player moves horizontally
        Dim verticalMovement As Integer = 0      ' amount player moves vertically

        If (e.KeyCode = Keys.Right Or e.KeyCode = Keys.D Or e.KeyCode = Keys.NumPad6) Then
            horizontalMovement = 5
        ElseIf (e.KeyCode = Keys.Left Or e.KeyCode = Keys.A Or e.KeyCode = Keys.NumPad4) Then
            horizontalMovement = -5
        ElseIf (e.KeyCode = Keys.Up Or e.KeyCode = Keys.W Or e.KeyCode = Keys.NumPad8) Then
            verticalMovement = -5
        ElseIf (e.KeyCode = Keys.Down Or e.KeyCode = Keys.S Or e.KeyCode = Keys.NumPad2) Then
            verticalMovement = 5
        End If

        ' move the player in the specified direction
        picPlayer.Left += horizontalMovement
        picPlayer.Top += verticalMovement

        ' horizontal line "bounce back" collision detection
        If (picPlayer.Top < 50 And picPlayer.Bottom > 50 And picPlayer.Right > 50 And picPlayer.Left < 200) Then        ' line 1
            picPlayer.Top -= verticalMovement
        ElseIf (picPlayer.Top < 80 And picPlayer.Bottom > 80 And picPlayer.Right > 80 And picPlayer.Left < 170) Then    ' line 2
            picPlayer.Top -= verticalMovement
        ElseIf (picPlayer.Top < 170 And picPlayer.Bottom > 170 And picPlayer.Right > 80 And picPlayer.Left < 170) Then  ' line 3
            picPlayer.Top -= verticalMovement
        ElseIf (picPlayer.Top < 200 And picPlayer.Bottom > 200 And picPlayer.Right > 50 And picPlayer.Left < 200) Then  ' line 4
            picPlayer.Top -= verticalMovement
        End If

        ' vertical line "bounce back" collision detection
        If (picPlayer.Right > 80 And picPlayer.Left < 80 And picPlayer.Bottom > 80 And picPlayer.Top < 170) Then        ' line 5
            picPlayer.Left -= horizontalMovement
        ElseIf (picPlayer.Right > 50 And picPlayer.Left < 50 And picPlayer.Bottom > 50 And picPlayer.Top < 200) Then    ' line 6
            picPlayer.Left -= horizontalMovement
        ElseIf (picPlayer.Right > 170 And picPlayer.Left < 170 And picPlayer.Bottom > 80 And picPlayer.Top < 170) Then  ' line 7
            picPlayer.Left -= horizontalMovement
        ElseIf (picPlayer.Right > 200 And picPlayer.Left < 200 And picPlayer.Bottom > 50 And picPlayer.Top < 200) Then  ' line 8
            picPlayer.Left -= horizontalMovement
        End If
  
Note that the lines being used in the example above are

e.Graphics.DrawLine(Pens.Black, 50, 50, 200, 50) ' line 1 - horizontal
e.Graphics.DrawLine(Pens.Black, 80, 80, 170, 80) ' line 2 - horizontal
e.Graphics.DrawLine(Pens.Black, 80, 170, 170, 170) ' line 3 - horizontal
e.Graphics.DrawLine(Pens.Black, 50, 200, 200, 200) ' line 4 - horizontal
e.Graphics.DrawLine(Pens.Black, 80, 80, 80, 170) ' line 5 - vertical
e.Graphics.DrawLine(Pens.Black, 50, 50, 50, 200) ' line 6 - vertical
e.Graphics.DrawLine(Pens.Black, 170, 80, 170, 170) ' line 7 - vertical
e.Graphics.DrawLine(Pens.Black, 200, 50, 200, 200) ' line 8 - vertical