Wyo VB Lecture Notes
Objective #1: Use collision detection when to detect whether an object hits a line.
' lines must be defined with the x1, y1 point being the leftmost endpoint
' and the x2, y2 point being the rightmost endpoint
' line 1 slanted down from left to right
Dim l1x1 As Integer = 100
Dim l1y1 As Integer = 20
Dim l1x2 As Integer = 200
Dim l1y2 As Integer = 200
Dim m1 As Double = (l1y2 - l1y1) / (l1x2 - l1x1)
' line 2 slanted up from left to right
Dim l2x1 As Integer = 10
Dim l2y1 As Integer = 300
Dim l2x2 As Integer = 200
Dim l2y2 As Integer = 200
Dim m2 As Double = (l2y2 - l2y1) / (l2x2 - l2x1)
e.Graphics.DrawLine(New Pen(Color.Black, 1), l1x1, l1y1, l1x2, l1y2)
e.Graphics.DrawLine(New Pen(Color.Black, 1), l2x1, l2y1, l2x2, l2y2)
' line 1
If (picPlayer.Bottom > l1y1 And picPlayer.Top < l1y2 And picPlayer.Right > l1x1 And picPlayer.Left < l1x2 And picPlayer.Top < m1 * (picPlayer.Right - l1x1) + l1y1 And picPlayer.Bottom > m1 * (picPlayer.Left - l1x1) + l1y1) Then
MessageBox.Show("hit line 1")
End If
' line 2
If (picPlayer.Bottom > l2y2 And picPlayer.Top < l2y1 And picPlayer.Right > l2x1 And picPlayer.Left < l2x2 And picPlayer.Top < m2 * (picPlayer.Left - l2x1) + l2y1 And picPlayer.Bottom > m2 * (picPlayer.Right - l2x1) + l2y1) Then
MessageBox.Show("hit line 2")
End If
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