D
Deek
I have a target(graphic) that moves via, i am trying to
detect a hit of the target with and essay(copied below)
my prof gave us, but I am not sure what to do, if you
could get me going in the right direction i would
appreaciate it:
Private Sub tmrTarget_Tick(ByVal sender As Object, ByVal
e As System.EventArgs) Handles tmrTarget.Tick
'Move the graphic(target) across the form
Static intX As Integer = picTarget.Left
Static intY As Integer = picTarget.Top
Static intWidth As Integer = picTarget.Width
Static intHeight As Integer = picTarget.Height
'Set new x coordinate
intX -= 10
If intX <= -picTarget.Width Then 'Graphic is off
edge of form
intX = Me.Width
End If
'Move image
picTarget.SetBounds(intX, intY, intWidth,
intHeight)
End Sub
___________________________________________-
Determining a Collision
In Proj2, it's tempting, but incorrect, to determine a
collision between a "cannonball" and its target by using
a notion of two points being close. If you have defined
a function distance to return the Euclidean distance
between two points, then it's tempting but wrong for
collision detection to use the following.
Private Function closeTo(ByVal x1 As Single, ByVal y1 As
Single, _
ByVal x2 As Single, ByVal y2 As
Single, _
ByVal criterionDistance As
Single) As Boolean
' true if and only if the points (X1, Y1) and (X2, Y2)
are within
' the criterionDistance of each other.
Return distance(x1, y1, x2, y2) <= criterionDistance
End Function
Notice the comments in this code - it measures what it
means for two points to be "close," and therefore could
also reasonably be used to determine if two small objects
("point objects") are close, but it may be inappropriate
as a measure of closeness for larger objects. For
example, in the diagram, the points labeled a and b
aren't very close, yet we should say the rectangles for
which they are respectively top-left vertices are "close"
or in collision.
Figure 1
Remember that even an object that appears to be circular
is treated as a rectangle, in the sense of having
properties .Top, .Left, .Width, and .Height. If you try
something like
collision = closeTo(imgTargetTop, imgTarget.Left,
_
shpCBall.Top,
shpCBall.Left, 50)
your program would treat a situation as illustrated in
Figure 1 as one in which the cannonball missed the target.
A better approach:
1. A collision of rectangular objects happens when
the rectangles intersect (overlap).
2. Rectangles overlap when both the horizontal
intervals determined by their left and right edges
overlap, and the vertical intervals determined by their
top and bottom edges overlap.
3. Intervals overlap when an endpoint of one of the
intervals is contained in the other interval (see Figure
2, in which, for example, [a,b] and [c,d] overlap,
corresponding to the fact that c is in [a,b]; and [a,d]
and [c,b] overlap, corresponding to the fact that c is in
[a,d]).
Figure 2
These observations suggest the use of the following code:
Function intervalMember(byVal x As Single, byVal a As
Single, byVal b As Single) _
As Boolean
' true if and only if x is a member of the interval [a, b]
Return (a <= x) And (x <= b)
End Function
Function intervalOverlap (byVal a As Single, byVal b As
Single, _
byVal x as
Single, byVal y As Single) As Boolean
' true if and only if intervals [a, b] and [x, y]
intersect
Return intervalMember(a, x, y) Or intervalMember(b, x,
y) Or _
intervalMember(x, a, b) Or
intervalMember(y, a, b)
End Function
Function controlOverlap (byVal c1 As Control, byVal c2 As
Control) As Boolean
' Parameters are assumed to be controls that occupy
rectangles, as determined by
' .Top, .Left, .Height, and .Width properties. Function
is true if and only if the rectangles
' occupied by the parameters overlap.
Return intervalOverlap(c1.Top, c1.Top + c1.Height, _
c2.Top, c2.Top + c2.Height) And _
intervalOverlap(c1.Left,
c1.Left + c1.Width, _
c2.Left, c2.Left + c2.Width)
End Function
Possible modifications: If you want a near-miss to count
as a hit, simply expand the intervals you test. For
example, you might have defined a small positive quantity
MARGIN, and replace the last function above by the
following.
detect a hit of the target with and essay(copied below)
my prof gave us, but I am not sure what to do, if you
could get me going in the right direction i would
appreaciate it:
Private Sub tmrTarget_Tick(ByVal sender As Object, ByVal
e As System.EventArgs) Handles tmrTarget.Tick
'Move the graphic(target) across the form
Static intX As Integer = picTarget.Left
Static intY As Integer = picTarget.Top
Static intWidth As Integer = picTarget.Width
Static intHeight As Integer = picTarget.Height
'Set new x coordinate
intX -= 10
If intX <= -picTarget.Width Then 'Graphic is off
edge of form
intX = Me.Width
End If
'Move image
picTarget.SetBounds(intX, intY, intWidth,
intHeight)
End Sub
___________________________________________-
Determining a Collision
In Proj2, it's tempting, but incorrect, to determine a
collision between a "cannonball" and its target by using
a notion of two points being close. If you have defined
a function distance to return the Euclidean distance
between two points, then it's tempting but wrong for
collision detection to use the following.
Private Function closeTo(ByVal x1 As Single, ByVal y1 As
Single, _
ByVal x2 As Single, ByVal y2 As
Single, _
ByVal criterionDistance As
Single) As Boolean
' true if and only if the points (X1, Y1) and (X2, Y2)
are within
' the criterionDistance of each other.
Return distance(x1, y1, x2, y2) <= criterionDistance
End Function
Notice the comments in this code - it measures what it
means for two points to be "close," and therefore could
also reasonably be used to determine if two small objects
("point objects") are close, but it may be inappropriate
as a measure of closeness for larger objects. For
example, in the diagram, the points labeled a and b
aren't very close, yet we should say the rectangles for
which they are respectively top-left vertices are "close"
or in collision.
Figure 1
Remember that even an object that appears to be circular
is treated as a rectangle, in the sense of having
properties .Top, .Left, .Width, and .Height. If you try
something like
collision = closeTo(imgTargetTop, imgTarget.Left,
_
shpCBall.Top,
shpCBall.Left, 50)
your program would treat a situation as illustrated in
Figure 1 as one in which the cannonball missed the target.
A better approach:
1. A collision of rectangular objects happens when
the rectangles intersect (overlap).
2. Rectangles overlap when both the horizontal
intervals determined by their left and right edges
overlap, and the vertical intervals determined by their
top and bottom edges overlap.
3. Intervals overlap when an endpoint of one of the
intervals is contained in the other interval (see Figure
2, in which, for example, [a,b] and [c,d] overlap,
corresponding to the fact that c is in [a,b]; and [a,d]
and [c,b] overlap, corresponding to the fact that c is in
[a,d]).
Figure 2
These observations suggest the use of the following code:
Function intervalMember(byVal x As Single, byVal a As
Single, byVal b As Single) _
As Boolean
' true if and only if x is a member of the interval [a, b]
Return (a <= x) And (x <= b)
End Function
Function intervalOverlap (byVal a As Single, byVal b As
Single, _
byVal x as
Single, byVal y As Single) As Boolean
' true if and only if intervals [a, b] and [x, y]
intersect
Return intervalMember(a, x, y) Or intervalMember(b, x,
y) Or _
intervalMember(x, a, b) Or
intervalMember(y, a, b)
End Function
Function controlOverlap (byVal c1 As Control, byVal c2 As
Control) As Boolean
' Parameters are assumed to be controls that occupy
rectangles, as determined by
' .Top, .Left, .Height, and .Width properties. Function
is true if and only if the rectangles
' occupied by the parameters overlap.
Return intervalOverlap(c1.Top, c1.Top + c1.Height, _
c2.Top, c2.Top + c2.Height) And _
intervalOverlap(c1.Left,
c1.Left + c1.Width, _
c2.Left, c2.Left + c2.Width)
End Function
Possible modifications: If you want a near-miss to count
as a hit, simply expand the intervals you test. For
example, you might have defined a small positive quantity
MARGIN, and replace the last function above by the
following.