455,572 Members | 1,711 Online
Need help? Post your question and get tips & solutions from a community of 455,572 IT Pros & Developers. It's quick & easy.

# Detecting a hit of a target

 P: n/a 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. Nov 20 '05 #1
27 Replies

 P: n/a 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 what you are trying to achieve and in . 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 understand, I will do my best to put forward something that makes sense to . Tell me more about targets, hitting, what objects you have, and anything that might fill in the gaps... ;-) Regards, Fergus Nov 20 '05 #2

 P: n/a * "Deek" 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 Nov 20 '05 #3

 P: n/a Hi Herfried, I like coding for some people. If the problem is interesting... Others I like to teach. ;-) Regards, Fergus Nov 20 '05 #4

 P: n/a * "Fergus Cooney" 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 Nov 20 '05 #5

 P: n/a Hi Herfried, 'Nobody here' - speaking for the group? Regards, Fergus Nov 20 '05 #6

 P: n/a 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! Nov 20 '05 #7

 P: n/a 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! Nov 20 '05 #8

 P: n/a 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 Nov 20 '05 #9

 P: n/a 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 Nov 20 '05 #10

 P: n/a Hi Derrick, Ok, you've shown me the code to move the target across the form but there's still nothing about the other objects that are supposed to be colliding, etc. The best bet to send the project to me. Make sure that it contains all the source, etc, and the prof's stuff - but from the \bin or \debug directories (as that will be recreated). Make sure that it's a working project and I can give it a go. But I still need to know more about what you want. Is it that you've read the prof's article and it makes no sense, or does it make sense but you are not sure how to do it in VB? I know, lol, - it's both - but I'm after details, if you can! - tell me what you . The thing is I'll need to explain some stuff to you but I don't know what needs to be explained. Only you know where the gaps are in your understanding. I could write reams about collision detection to find that you understand that but need to now about how it's done in VB. Or I could describe how to do it in code but find that it makes no sense to you because collision detection is a mystery!! Tell me what you understand. So, stuck for a few days because...?? ;-) Regards, Fergus Nov 20 '05 #11

 P: n/a First off I do not want anyone to do my homework for me I just want some help so I understand this and learn from my mistakes. I believe I understand collision detection in general. The projectile has to hit between the coordinates of the target, in sense they overlap. I am unsure how to convert that to code within VB.NET. Hopefully this is what you were getting at. Thank you for helping me out. Nov 20 '05 #12

 P: n/a Hi Derrick, || First off I do not want anyone to do my homework for me Hmm, I wonder where you got that idea, lol. Sometimes I do code for people - especially when I think it's a work situation with time constraints. With students I prefer to explain. || I just want some help so I understand this and learn from my mistakes. The best way ;-) || I believe I understand collision detection in general. Good. I didn't really want to have to explain that from scratch! || I am unsure how to convert that to code within VB.NET. || Hopefully this is what you were getting at. I was. Having a copy of your project was not so that I could do it for you but so that I'd have everything that you have and we could talk on a common foundation. My plan is/was to get it working for myself and then guide you to the same conclusion. || The projectile has to hit between the coordinates of the target, || in sense they overlap. Prof's controlOverlap() looks like the one to use. If you want to get a complete feel for it, draw some boxes that are 1) away from each other diagonally, 2) side by side, 3) one above the other, 4) overlapping on a corner, 5) overlapping on an edge and 6) one within the other. That covers every possibility. Then work out how it does its pairs of intervalOverlaps to determine whether an overlap has occurred. The projectile is the bit that's been missing from your information so far. Assuming that it has its own Control like the Target, and perhaps has its own Timer, then you'll need to check for the pair being overlapped every time that one of them moves. Once you've positioned picTarget, or picProjectile, controlOverlap (picTarget, picProjectile) will detect the collision of the objects (or at least, of the bounding rectangles). That's all I've got for you tonight as I'm off to bed. Tell me how you get on. :-) Others may pick this thread up with you if you are up over the next few hours. If you want to post the project (or send it directly to me - my email address is valid), please do so. I'll be back again tomorrow. Regards, Fergus ps. 'Static' isn't required on variables that are initialised every time the routine is called . Nov 20 '05 #13

 P: n/a I tried emailing you my code but it says ur mailbox is over its quota. I am re-reading your last post and will try to follow what you mean. is filter-1@tesco your real email? Thanks, Derrick Nov 20 '05 #14

 P: n/a Ok here is my code so far minus any of the hit detection stuff. So far my program starts, asks for input of 2 players names, uses the visreverse code to move controls visible property. Then the target comes up moving and it has a cannon , click btnShoot to fire. Fergus, I mapped out you 6 diagrams and if i am correct 3 would count as a hit, your #4,#5,#6. My cannonball has a tmr so in its "tick" event i believe it is called. i could have in an if statement that would call on control Overlap? Just to see if i understand interval overlap right. starting at the return statement. it detects whether a is contained in x,y, then b in x,y and so on? I am not sure how to put the a,b, x, y into my program to get it to work. in control overlap i tried replacing c1, and c2 with picTarget and picBall, and figured that it would then detect it with the .tops and .lefts but i missed something. Thanks, Derrick Nov 20 '05 #15

 P: n/a Hi Derrick, It's a real email address alright. Thanks for letting me know that it's not working. I've tried sending myself mail to that address and a couple of others with the same ISP - and none of them are working, dammit. The ISP has been informed. In the meantime, I've got the code that you posted here. Regards, Fergus Nov 20 '05 #17

 P: n/a de*****@aol.com (Deek164) wrote in news:20***************************@mb-m18.aol.com: Public Class Form1 .... End Class Nowhere in the code do I see you checking for the overlaps. Each tick event seems to just move the objects. Somewhere in that code you need to check each obect to see if it is overlapping the other. Chris Nov 20 '05 #18

 P: n/a Now I got my target to detect a hit, but it does multiple times in one shot, I am working on that part now :) Nov 20 '05 #21

 P: n/a Hi Derrick, If you take my suggestion of keeping the timer intervals the same and short (eg, 40ms), then you can put the collision test in either tick event handler. If you have the target with a long timer interval (which makes the action jerky) then you need to put the collision test in with the cannonball as this is moving more frequently. If controlOverlap (picC, picT) Then Stop the action. Let off the fireworks!!! End If Regards, Fergus Nov 20 '05 #22

 P: n/a Hi Derrick, Well done!! :-) Now you can either switch the Timers off. This is the simple solution. More of a challenge is to start counting how many times you get a hit detected. It will happen consecutively so when a run of detections stops, you stop the Timers and let off the fireworks. And the count of multiple detections that you've just made can be used as a score, or to have different messages. (Have a read of the other post again and see if it makes more sense now) Regards, Fergus Nov 20 '05 #23

 P: n/a Thanks for the help Fergus, I am going to try and tackle the rest of the assigned project then I will do your suggestion. My next goal is: 1)give each player 3 shots in a row 2) declare the winner 3) record to a txt file for a high score table 4) setup my error checks Nov 20 '05 #24

 P: n/a Hi Derrick, Ah yes, you'll come across this a lot in your future - the todo list versus the wishlist!! You're on your way at last, after being blocked. Well done. :-)) Come back if you need anything more - just to sound off ideas if necessary. You can start a new thread or continue in this thread, whichever you like, I have this one monitored. All the best, Fergus Nov 20 '05 #25