Wyo VB Lecture Notes
Objective #1: Use collision detection to control animation.
' 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