PC Review


Reply
Thread Tools Rate Thread

Detecting a hit of a target

 
 
Deek
Guest
Posts: n/a
 
      13th Oct 2003
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.

 
Reply With Quote
 
 
 
 
Fergus Cooney
Guest
Posts: n/a
 
      13th Oct 2003
Hi Deek,

I understand the essay, I understand the issues. I don't, unfortunately,
understand what your problem is.

By that I mean that I don't know <exactly> what you are trying to achieve
and in <what terms>. And I don't yet know what it is that you don't understand
about the problem and your professor's article.

However, when you can put your problem to me so that I <do> understand, I
will do my best to put forward something that makes sense to <you>.

Tell me more about targets, hitting, what objects you have, and anything
that might fill in the gaps...

;-)

Regards,
Fergus


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th Oct 2003
* "Deek" <(E-Mail Removed)> scripsit:
> 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:


Nobody here will do your homework.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      13th Oct 2003
Hi Herfried,

I like coding for some people.

If the problem is interesting...

Others I like to teach.

;-)

Regards,
Fergus


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th Oct 2003
* "Fergus Cooney" <filter-(E-Mail Removed)> scripsit:
> I like coding for some people.


I like it too, but I do not make other people's homework.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      13th Oct 2003
Hi Herfried,

'Nobody here' - speaking for the group?

Regards,
Fergus


 
Reply With Quote
 
Derrick Pizur
Guest
Posts: n/a
 
      13th Oct 2003


I will do my best, this is my first attempt at a programming course.

I have a target moving with this code:

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


_______________________________________________


The target goes from right to left then re appears on the right. What i
want to happen is have when then controls overlap it detect a hit, right
now when the controls overlap both target and projectile keep moving. I
am not really sure what else to provide, i have never has to ask for
help like this. If someone needs to see the code i could email it to
someone if that would help. I have a zip file with my current code, the
project specs , the prof essay. I have been stuck at this point for a
few days now
.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Derrick Pizur
Guest
Posts: n/a
 
      13th Oct 2003


hmm my last post didnt show up is it time delayed? or am i doing
something wrong?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      13th Oct 2003
Hi Derrick,

Both are present for me, but earlier someone else's message header arrived
yet the message itself was deleted. Server hiccup.

However, the delay from other sources than MS itself may take some time. I
know Google is several hours behind. I don't know about DEX.

I'm just pondering your main message...

Later, Dude.
Fergus


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      13th Oct 2003
Hi Fergus,

I think Herfried is right we have to watch that, I love it too sometimes
those nice compact problems.

(That would not say I am not answering them)

:-)

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in setting Target Node and Target Tree Property mohit Microsoft ASP .NET 0 23rd Jan 2008 05:41 AM
Target cell reference moves when target is cut and pasted =?Utf-8?B?SWxseWEgVGVpZGVtYW4=?= Microsoft Excel Misc 5 31st May 2007 11:34 AM
unable to select Save Target As... and Print Target in IE6 Vedant Lath Windows XP Internet Explorer 1 6th Jun 2004 03:25 PM
Detecting focus target Atlas Microsoft Access Form Coding 3 28th May 2004 10:09 AM
Detecting DragDrop target when dropped into Explorer Simon Bond Microsoft Dot NET Framework Forms 0 21st Aug 2003 02:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:44 AM.