Detecting a hit of a target

  • Thread starter Thread starter Deek
  • Start date Start date
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.
 
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
 
* "Deek said:
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.
 
Hi Herfried,

I like coding for some people.

If the problem is interesting...

Others I like to teach.

;-)

Regards,
Fergus
 
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
:( .
 
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
 
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
 
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 <is> to send the project to me. Make sure that it contains
all the source, etc, and the prof's stuff - but <nothing> 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 <don't understand>.

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 <do> understand.

So, stuck for a few days because...?? ;-)

Regards,
Fergus
 
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.
 
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 .
 
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
 
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
 
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents radLevel1 As System.Windows.Forms.RadioButton
Friend WithEvents radLevel2 As System.Windows.Forms.RadioButton
Friend WithEvents radLevel3 As System.Windows.Forms.RadioButton
Friend WithEvents lblTitle As System.Windows.Forms.Label
Friend WithEvents btnSelect As System.Windows.Forms.Button
Friend WithEvents lblNames As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents btnContinue As System.Windows.Forms.Button
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents tmrTarget As System.Windows.Forms.Timer
Friend WithEvents txtpl1 As System.Windows.Forms.TextBox
Friend WithEvents txtpl2 As System.Windows.Forms.TextBox
Friend WithEvents picIntro As System.Windows.Forms.PictureBox
Friend WithEvents picTarget As System.Windows.Forms.PictureBox
Friend WithEvents picCannon As System.Windows.Forms.PictureBox
Friend WithEvents picCannonBall As System.Windows.Forms.PictureBox
Friend WithEvents picGun As System.Windows.Forms.PictureBox
Friend WithEvents picBall As System.Windows.Forms.PictureBox
Friend WithEvents btnShoot As System.Windows.Forms.Button
Friend WithEvents tmrShoot As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.radLevel3 = New System.Windows.Forms.RadioButton
Me.radLevel2 = New System.Windows.Forms.RadioButton
Me.radLevel1 = New System.Windows.Forms.RadioButton
Me.lblTitle = New System.Windows.Forms.Label
Me.btnSelect = New System.Windows.Forms.Button
Me.picIntro = New System.Windows.Forms.PictureBox
Me.lblNames = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.btnContinue = New System.Windows.Forms.Button
Me.btnClear = New System.Windows.Forms.Button
Me.tmrTarget = New System.Windows.Forms.Timer(Me.components)
Me.picTarget = New System.Windows.Forms.PictureBox
Me.txtpl1 = New System.Windows.Forms.TextBox
Me.txtpl2 = New System.Windows.Forms.TextBox
Me.picCannon = New System.Windows.Forms.PictureBox
Me.picGun = New System.Windows.Forms.PictureBox
Me.picBall = New System.Windows.Forms.PictureBox
Me.btnShoot = New System.Windows.Forms.Button
Me.tmrShoot = New System.Windows.Forms.Timer(Me.components)
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.radLevel3)
Me.GroupBox1.Controls.Add(Me.radLevel2)
Me.GroupBox1.Controls.Add(Me.radLevel1)
Me.GroupBox1.Location = New System.Drawing.Point(320, 264)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(264, 184)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Select a skill level:"
Me.GroupBox1.Visible = False
'
'radLevel3
'
Me.radLevel3.Location = New System.Drawing.Point(56, 104)
Me.radLevel3.Name = "radLevel3"
Me.radLevel3.TabIndex = 2
Me.radLevel3.Text = "Level # 3"
'
'radLevel2
'
Me.radLevel2.Location = New System.Drawing.Point(56, 72)
Me.radLevel2.Name = "radLevel2"
Me.radLevel2.TabIndex = 1
Me.radLevel2.Text = "Level # 2"
'
'radLevel1
'
Me.radLevel1.Location = New System.Drawing.Point(56, 40)
Me.radLevel1.Name = "radLevel1"
Me.radLevel1.TabIndex = 0
Me.radLevel1.Text = "Level # 1"
'
'lblTitle
'
Me.lblTitle.Font = New System.Drawing.Font("Arial Black", 22.2!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
Me.lblTitle.Location = New System.Drawing.Point(264, 104)
Me.lblTitle.Name = "lblTitle"
Me.lblTitle.Size = New System.Drawing.Size(392, 64)
Me.lblTitle.TabIndex = 1
Me.lblTitle.Text = "TARGET HUNTER"
'
'btnSelect
'
Me.btnSelect.Location = New System.Drawing.Point(496, 456)
Me.btnSelect.Name = "btnSelect"
Me.btnSelect.Size = New System.Drawing.Size(88, 23)
Me.btnSelect.TabIndex = 2
Me.btnSelect.Text = "Select Level"
Me.btnSelect.Visible = False
'
'picIntro
'
Me.picIntro.Image = CType(resources.GetObject("picIntro.Image"),
System.Drawing.Image)
Me.picIntro.Location = New System.Drawing.Point(400, 160)
Me.picIntro.Name = "picIntro"
Me.picIntro.Size = New System.Drawing.Size(112, 80)
Me.picIntro.TabIndex = 3
Me.picIntro.TabStop = False
Me.picIntro.Visible = False
'
'lblNames
'
Me.lblNames.Font = New System.Drawing.Font("Microsoft Sans Serif",
12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.lblNames.Location = New System.Drawing.Point(328, 168)
Me.lblNames.Name = "lblNames"
Me.lblNames.Size = New System.Drawing.Size(288, 40)
Me.lblNames.TabIndex = 4
Me.lblNames.Text = "Please enter player's names:"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(344, 208)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(64, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Player 1:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(344, 240)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(64, 16)
Me.Label2.TabIndex = 7
Me.Label2.Text = "Player 2:"
'
'btnContinue
'
Me.btnContinue.Location = New System.Drawing.Point(424, 272)
Me.btnContinue.Name = "btnContinue"
Me.btnContinue.Size = New System.Drawing.Size(104, 24)
Me.btnContinue.TabIndex = 8
Me.btnContinue.Text = "Continue"
'
'btnClear
'
Me.btnClear.Location = New System.Drawing.Point(544, 272)
Me.btnClear.Name = "btnClear"
Me.btnClear.TabIndex = 9
Me.btnClear.Text = "Clear"
'
'tmrTarget
'
'
'picTarget
'
Me.picTarget.Image = CType(resources.GetObject("picTarget.Image"),
System.Drawing.Image)
Me.picTarget.Location = New System.Drawing.Point(768, 168)
Me.picTarget.Name = "picTarget"
Me.picTarget.Size = New System.Drawing.Size(104, 96)
Me.picTarget.TabIndex = 10
Me.picTarget.TabStop = False
Me.picTarget.Visible = False
'
'txtpl1
'
Me.txtpl1.Location = New System.Drawing.Point(416, 208)
Me.txtpl1.Name = "txtpl1"
Me.txtpl1.Size = New System.Drawing.Size(128, 22)
Me.txtpl1.TabIndex = 11
Me.txtpl1.Text = ""
'
'txtpl2
'
Me.txtpl2.Location = New System.Drawing.Point(416, 240)
Me.txtpl2.Name = "txtpl2"
Me.txtpl2.Size = New System.Drawing.Size(128, 22)
Me.txtpl2.TabIndex = 12
Me.txtpl2.Text = ""
'
'picCannon
'
Me.picCannon.Location = New System.Drawing.Point(0, 0)
Me.picCannon.Name = "picCannon"
Me.picCannon.TabIndex = 0
Me.picCannon.TabStop = False
'
'picGun
'
Me.picGun.Image = CType(resources.GetObject("picGun.Image"),
System.Drawing.Image)
Me.picGun.Location = New System.Drawing.Point(424, 472)
Me.picGun.Name = "picGun"
Me.picGun.Size = New System.Drawing.Size(56, 72)
Me.picGun.TabIndex = 13
Me.picGun.TabStop = False
Me.picGun.Visible = False
'
'picBall
'
Me.picBall.Image = CType(resources.GetObject("picBall.Image"),
System.Drawing.Image)
Me.picBall.Location = New System.Drawing.Point(464, 464)
Me.picBall.Name = "picBall"
Me.picBall.Size = New System.Drawing.Size(16, 8)
Me.picBall.TabIndex = 14
Me.picBall.TabStop = False
Me.picBall.Visible = False
'
'btnShoot
'
Me.btnShoot.Location = New System.Drawing.Point(520, 512)
Me.btnShoot.Name = "btnShoot"
Me.btnShoot.TabIndex = 15
Me.btnShoot.Text = "Shoot"
Me.btnShoot.Visible = False
'
'tmrShoot
'
Me.tmrShoot.Interval = 1500
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(920, 560)
Me.Controls.Add(Me.btnShoot)
Me.Controls.Add(Me.picGun)
Me.Controls.Add(Me.picCannon)
Me.Controls.Add(Me.txtpl2)
Me.Controls.Add(Me.txtpl1)
Me.Controls.Add(Me.picTarget)
Me.Controls.Add(Me.btnClear)
Me.Controls.Add(Me.btnContinue)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.lblNames)
Me.Controls.Add(Me.picIntro)
Me.Controls.Add(Me.btnSelect)
Me.Controls.Add(Me.lblTitle)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.picBall)
Me.Name = "Form1"
Me.Text = "TARGET HUNTER"
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region
Private timeStep As Integer 'global var
Private player1 As String, player2 As String
Private Sub boolReverse2(ByRef a As Boolean, ByRef b As Boolean)
'Reverse the values of the parameters
a = Not a
b = Not b
End Sub
Private Sub visReverse2(ByRef c1 As Control, ByRef c2 As Control)
'Reverse the visibility of the parameters
boolReverse2(c1.Visible, c2.Visible)
End Sub

Private Sub visReverse4(ByRef c1 As Control, ByRef c2 As Control, _
ByRef c3 As Control, ByRef c4 As Control)
visReverse2(c1, c2)
visReverse2(c3, c4)
End Sub
Private Sub visReverse8(ByRef c1 As Control, ByRef c2 As Control, _
ByRef c3 As Control, ByRef c4 As Control, _
ByRef c5 As Control, ByRef c6 As Control, _
ByRef c7 As Control, ByRef c8 As Control)
visReverse4(c1, c2, c3, c4)
visReverse4(c5, c6, c7, c8)
End Sub
Private Sub btnSelect_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSelect.Click
If radLevel1.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 800 ' slow speed for target
visReverse2(picTarget, btnShoot)
picGun.Visible = True
Else
If radLevel2.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 400 ' Medium speed for target
visReverse2(picTarget, btnShoot)
picGun.Visible = True
Else
If radLevel3.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 200 ' Fast speed for target
visReverse2(picTarget, btnShoot)
picGun.Visible = True
Else ' Then none of the radio buttons are checked
MsgBox("Please select a difficulty level!")
visReverse4(GroupBox1, btnSelect, lblTitle, picIntro)

End If
End If
End If
visReverse4(lblTitle, picIntro, GroupBox1, btnSelect)
'picTarget.Visible = True
'tmrTarget.Enabled = True
End Sub

Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnContinue.Click
'Dim player1 As String, player2 As String
player1 = txtpl1.Text
player2 = txtpl2.Text
'lblans.Text = player1
visReverse8(lblNames, txtpl1, txtpl2, btnContinue, btnClear, GroupBox1,
_
btnSelect, picIntro)
visReverse2(Label1, Label2)
End Sub

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



Private Sub tmrShoot_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrShoot.Tick
Dim jump As Integer = 10
With picBall
.Top = .Top - jump
If .Top < jump Then
.Visible = False
tmrShoot.Enabled = False

End If

End With
End Sub
Private Sub btnShoot_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShoot.Click
With picBall
.Left = 454
.Top = 440
.Visible = True
End With
tmrShoot.Enabled = True
End Sub

End Class
 
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
 
(e-mail address removed) (Deek164) wrote in
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
 
Hi Derrick,

Here are a few hints and tips to start off with.

When you have a set of options (such a difficulty level), rather than
leave it blank and tell the user off when they don't select a value, set a
default and leave it up to them to change it if they want. So in my version I
set difficulty level 1 as the default.

When testing it's a pain in the bum having to go through introductory
screen before you get to where the action is.
Having done the above, double-click on the Form in design mode so that it
creates Form_Load for you and put this inside.
btnContinue.PerformClick
btnSelect.PerformClick
This will press these buttons for you and get you immediately into the
game. ;-)

Rather than have a [Shoot] button, take the text out of it and set the
Button's Image to your gun picture. That way the user can click the gun to
shoot!

Rather than have a fixed jump for the movement and a different timer
intervals, make the timer interval constant and small (something between 10 to
20 times per second) and adjust the distance. This makes the movement much
smoother.

Now, to answer your questions:

!! I mapped out you 6 diagrams and if i am correct 3 would count as
!! a hit, your #4,#5,#6.

Yes, 4, 5 and 6 are the collisions.

!! 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?

It is the tick event and that's just the place to call controlOverlap.

!! 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?

Yes. You can do diagrams again for this too.
1 No overlap
A----------B
X----------Y

2 No overlap
A----------B
X----------Y

3 X within AB and B within XY
A-----------------B
X--------------------Y

4 Y within AB and A within XY
A-----------------B
X-----------------Y

5 X within AB and Y winthin AB
A-----------------B
X------Y

6 A within XY and B within XY
A-------B
X--------------------Y

Because there are four endpoints, there are four tests made.


!! I am not sure how to put the a,b, x, y into my program to get it to
!! work.

But you made a good guess because

|| in control overlap i tried replacing c1, and c2 with picTarget
!! and picBall,

That's what I did too.

!! and figured that it would then detect it with the .tops and
!! .lefts but i missed something

What did you do once you had called controlOverlap? I had it in a If
statement and had a MsgBox pop up it returned True

You're very close. ;-))

A final challenge:
If you have a fast timer interval (I set it to 50ms) then your
collision detector will go off several times in a row for a single hit (unless
you switch the Timer off). What you can do is count up how many times it goes
off, then when the run is over, (the first time that there's no collision) you
can use the number of hits as a score - eg. 1 or 2 means you only winged the
target, > 5 means a direct hit, target destroyed.

Regards,
Fergus
 
Ok i think i made the changes you suggeseted. I am unsure where to put the
call on controlOverlap, would that be in my tick event for the target?

my new code:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents radLevel1 As System.Windows.Forms.RadioButton
Friend WithEvents radLevel2 As System.Windows.Forms.RadioButton
Friend WithEvents radLevel3 As System.Windows.Forms.RadioButton
Friend WithEvents lblTitle As System.Windows.Forms.Label
Friend WithEvents btnSelect As System.Windows.Forms.Button
Friend WithEvents lblNames As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents btnContinue As System.Windows.Forms.Button
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents tmrTarget As System.Windows.Forms.Timer
Friend WithEvents txtpl1 As System.Windows.Forms.TextBox
Friend WithEvents txtpl2 As System.Windows.Forms.TextBox
Friend WithEvents picIntro As System.Windows.Forms.PictureBox
Friend WithEvents picTarget As System.Windows.Forms.PictureBox
Friend WithEvents picCannonBall As System.Windows.Forms.PictureBox
Friend WithEvents picBall As System.Windows.Forms.PictureBox
Friend WithEvents tmrShoot As System.Windows.Forms.Timer
Friend WithEvents btnShoot As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.radLevel3 = New System.Windows.Forms.RadioButton
Me.radLevel2 = New System.Windows.Forms.RadioButton
Me.radLevel1 = New System.Windows.Forms.RadioButton
Me.lblTitle = New System.Windows.Forms.Label
Me.btnSelect = New System.Windows.Forms.Button
Me.picIntro = New System.Windows.Forms.PictureBox
Me.lblNames = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.btnContinue = New System.Windows.Forms.Button
Me.btnClear = New System.Windows.Forms.Button
Me.tmrTarget = New System.Windows.Forms.Timer(Me.components)
Me.picTarget = New System.Windows.Forms.PictureBox
Me.txtpl1 = New System.Windows.Forms.TextBox
Me.txtpl2 = New System.Windows.Forms.TextBox
Me.picBall = New System.Windows.Forms.PictureBox
Me.tmrShoot = New System.Windows.Forms.Timer(Me.components)
Me.btnShoot = New System.Windows.Forms.Button
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.radLevel3)
Me.GroupBox1.Controls.Add(Me.radLevel2)
Me.GroupBox1.Controls.Add(Me.radLevel1)
Me.GroupBox1.Location = New System.Drawing.Point(320, 264)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(264, 184)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Select a skill level:"
Me.GroupBox1.Visible = False
'
'radLevel3
'
Me.radLevel3.Location = New System.Drawing.Point(56, 104)
Me.radLevel3.Name = "radLevel3"
Me.radLevel3.TabIndex = 2
Me.radLevel3.Text = "Level # 3"
'
'radLevel2
'
Me.radLevel2.Location = New System.Drawing.Point(56, 72)
Me.radLevel2.Name = "radLevel2"
Me.radLevel2.TabIndex = 1
Me.radLevel2.Text = "Level # 2"
'
'radLevel1
'
Me.radLevel1.Location = New System.Drawing.Point(56, 40)
Me.radLevel1.Name = "radLevel1"
Me.radLevel1.TabIndex = 0
Me.radLevel1.Text = "Level # 1"
'
'lblTitle
'
Me.lblTitle.Font = New System.Drawing.Font("Arial Black", 22.2!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
Me.lblTitle.Location = New System.Drawing.Point(264, 104)
Me.lblTitle.Name = "lblTitle"
Me.lblTitle.Size = New System.Drawing.Size(392, 64)
Me.lblTitle.TabIndex = 1
Me.lblTitle.Text = "TARGET HUNTER"
'
'btnSelect
'
Me.btnSelect.Location = New System.Drawing.Point(496, 456)
Me.btnSelect.Name = "btnSelect"
Me.btnSelect.Size = New System.Drawing.Size(88, 23)
Me.btnSelect.TabIndex = 2
Me.btnSelect.Text = "Select Level"
Me.btnSelect.Visible = False
'
'picIntro
'
Me.picIntro.Image = CType(resources.GetObject("picIntro.Image"),
System.Drawing.Image)
Me.picIntro.Location = New System.Drawing.Point(400, 160)
Me.picIntro.Name = "picIntro"
Me.picIntro.Size = New System.Drawing.Size(112, 80)
Me.picIntro.TabIndex = 3
Me.picIntro.TabStop = False
Me.picIntro.Visible = False
'
'lblNames
'
Me.lblNames.Font = New System.Drawing.Font("Microsoft Sans Serif",
12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.lblNames.Location = New System.Drawing.Point(328, 168)
Me.lblNames.Name = "lblNames"
Me.lblNames.Size = New System.Drawing.Size(288, 40)
Me.lblNames.TabIndex = 4
Me.lblNames.Text = "Please enter player's names:"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(344, 208)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(64, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Player 1:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(344, 240)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(64, 16)
Me.Label2.TabIndex = 7
Me.Label2.Text = "Player 2:"
'
'btnContinue
'
Me.btnContinue.Location = New System.Drawing.Point(424, 272)
Me.btnContinue.Name = "btnContinue"
Me.btnContinue.Size = New System.Drawing.Size(104, 24)
Me.btnContinue.TabIndex = 8
Me.btnContinue.Text = "Continue"
'
'btnClear
'
Me.btnClear.Location = New System.Drawing.Point(544, 272)
Me.btnClear.Name = "btnClear"
Me.btnClear.TabIndex = 9
Me.btnClear.Text = "Clear"
'
'tmrTarget
'
'
'picTarget
'
Me.picTarget.Image = CType(resources.GetObject("picTarget.Image"),
System.Drawing.Image)
Me.picTarget.Location = New System.Drawing.Point(768, 168)
Me.picTarget.Name = "picTarget"
Me.picTarget.Size = New System.Drawing.Size(104, 96)
Me.picTarget.TabIndex = 10
Me.picTarget.TabStop = False
Me.picTarget.Visible = False
'
'txtpl1
'
Me.txtpl1.Location = New System.Drawing.Point(416, 208)
Me.txtpl1.Name = "txtpl1"
Me.txtpl1.Size = New System.Drawing.Size(128, 22)
Me.txtpl1.TabIndex = 11
Me.txtpl1.Text = ""
'
'txtpl2
'
Me.txtpl2.Location = New System.Drawing.Point(416, 240)
Me.txtpl2.Name = "txtpl2"
Me.txtpl2.Size = New System.Drawing.Size(128, 22)
Me.txtpl2.TabIndex = 12
Me.txtpl2.Text = ""
'
'picBall
'
Me.picBall.Image = CType(resources.GetObject("picBall.Image"),
System.Drawing.Image)
Me.picBall.Location = New System.Drawing.Point(464, 464)
Me.picBall.Name = "picBall"
Me.picBall.Size = New System.Drawing.Size(16, 8)
Me.picBall.TabIndex = 14
Me.picBall.TabStop = False
Me.picBall.Visible = False
'
'tmrShoot
'
Me.tmrShoot.Interval = 25
'
'btnShoot
'
Me.btnShoot.Image = CType(resources.GetObject("btnShoot.Image"),
System.Drawing.Image)
Me.btnShoot.Location = New System.Drawing.Point(440, 480)
Me.btnShoot.Name = "btnShoot"
Me.btnShoot.Size = New System.Drawing.Size(56, 64)
Me.btnShoot.TabIndex = 15
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(920, 560)
Me.Controls.Add(Me.btnShoot)
Me.Controls.Add(Me.txtpl2)
Me.Controls.Add(Me.txtpl1)
Me.Controls.Add(Me.picTarget)
Me.Controls.Add(Me.btnClear)
Me.Controls.Add(Me.btnContinue)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.lblNames)
Me.Controls.Add(Me.picIntro)
Me.Controls.Add(Me.btnSelect)
Me.Controls.Add(Me.lblTitle)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.picBall)
Me.Name = "Form1"
Me.Text = "TARGET HUNTER"
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region
Private timeStep As Integer 'global var
Private player1 As String, player2 As String
Private Sub boolReverse2(ByRef a As Boolean, ByRef b As Boolean)
'Reverse the values of the parameters
a = Not a
b = Not b
End Sub
Private Sub visReverse2(ByRef c1 As Control, ByRef c2 As Control)
'Reverse the visibility of the parameters
boolReverse2(c1.Visible, c2.Visible)
End Sub

Private Sub visReverse4(ByRef c1 As Control, ByRef c2 As Control, _
ByRef c3 As Control, ByRef c4 As Control)
visReverse2(c1, c2)
visReverse2(c3, c4)
End Sub
Private Sub visReverse8(ByRef c1 As Control, ByRef c2 As Control, _
ByRef c3 As Control, ByRef c4 As Control, _
ByRef c5 As Control, ByRef c6 As Control, _
ByRef c7 As Control, ByRef c8 As Control)
visReverse4(c1, c2, c3, c4)
visReverse4(c5, c6, c7, c8)
End Sub
Private Sub btnSelect_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSelect.Click
If radLevel1.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 800 ' slow speed for target
visReverse2(picTarget, btnShoot)
btnShoot.Visible = True
Else
If radLevel2.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 400 ' Medium speed for target
visReverse2(picTarget, btnShoot)
btnShoot.Visible = True
Else
If radLevel3.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 200 ' Fast speed for target
visReverse2(picTarget, btnShoot)
btnShoot.Visible = True
Else ' Then none of the radio buttons are checked
MsgBox("Please select a difficulty level!")
visReverse4(GroupBox1, btnSelect, lblTitle, picIntro)

End If
End If
End If
visReverse4(lblTitle, picIntro, GroupBox1, btnSelect)
'picTarget.Visible = True
'tmrTarget.Enabled = True
End Sub

Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnContinue.Click
'Dim player1 As String, player2 As String
player1 = txtpl1.Text
player2 = txtpl2.Text
'lblans.Text = player1
visReverse8(lblNames, txtpl1, txtpl2, btnContinue, btnClear, GroupBox1,
_
btnSelect, picIntro)
visReverse2(Label1, Label2)
End Sub

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



Private Sub tmrShoot_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrShoot.Tick
Dim jump As Integer = 10
With picBall
.Top = .Top - jump
If .Top < jump Then
.Visible = False
tmrShoot.Enabled = False

End If

End With
End Sub
Private Sub btnShoot_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
With picBall
.Left = 454
.Top = 440
.Visible = True
End With
tmrShoot.Enabled = True
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
btnContinue.PerformClick()
btnSelect.PerformClick()

End Sub

Private Sub btnShoot_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShoot.Click
With picBall
.Left = 454
.Top = 440
.Visible = True
End With
tmrShoot.Enabled = True
End Sub
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 interval[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 picBall As Control, ByVal picTarget 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(picBall.Top, picBall.Top + picBall.Height, _
picTarget.Top, picTarget.Top + picTarget.Height)
And _
intervalOverlap(picBall.Left, picBall.Left +
picBall.Width, _
picTarget.Left, picTarget.Left +
picTarget.Width)
End Function
End Class
 
Back
Top