本文介绍了检测目标的命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目标(图形)移动通过,我正在尝试

检测到目标的命中和文章(复制如下)

我的教授给了我们,但是我不知道该怎么做,如果你这个b $ b可以让我朝着正确的方向前进,我会

appreaciate it:


Private Sub tmrTarget_Tick(ByVal sender As Object,ByVal
$ b $ as As System.EventArgs)处理tmrTarget.Tick

''在图表中移动图形(目标)

静态intX为Integer = picTarget.Left

静态intY为Integer = picTarget.Top

静态intWidth为Integer = picTarget.Width

静态intHeight As Integer = picTarget.Height


''设置新的x坐标

intX - = 10

如果intX< = -picTarget.Width然后''图形关闭

表格边缘

intX = Me.Width

结束如果

''移动图片

picTarget.SetBounds(intX,intY,intWidth,

intHeight)

结束子

___________________________________________-

确定碰撞

在Proj2中,确定

炮弹之间的碰撞和目标是使用

关闭两点的概念。如果你已经定义了

a函数距离来返回两点之间的欧几里德距离

那么它很诱人但错误的是

碰撞检测使用以下内容。

私函数closeTo(ByVal x1为单,ByVal y1为

单,_

ByVal x2为单,ByVal y2 As

Single,_

ByVal criterionDistance As

Single)As Boolean

''如果且仅为真如果点数(X1,Y1)和(X2,Y2)

都在

'之内,那么彼此的标准距离。

返回距离(x1,y1,x2,y2)< = criterionDistance

结束功能

注意此代码中的注释 - 它测量的是什么

两个点的意思是关闭,意思是关闭。因此可以合理地使用
来确定是否两个小物件

(点对象)接近,但可能不合适

作为较大物体接近程度的度量。对于

的例子,在图中,标有a和b

的点不是非常接近,但我们应该说
$ b $的矩形b它们分别是左上顶点是关闭

或碰撞。


图1


请记住,即使是一个看似圆形的对象,也可以将
视为一个矩形,具有

属性.Top,.Left, .Width和.Height。如果您尝试

类似

collision = closeTo(imgTargetTop,imgTarget.Left,

_

shpCBall。顶部,

shpCBall.Left,50)

你的程序会处理如

图所示的情况图1作为炮弹错过的情况目标。

更好的方法:

1.矩形物体的碰撞发生在矩形相交(重叠)的时候。



2.当水平

区间由它们的左右边缘确定时,矩形重叠

重叠,并且垂直间隔由它们确定

顶部和底部边缘重叠。


3.当其他间隔中包含

间隔之一的端点时,间隔重叠(请参阅图

2,其中,例如,[a,b]和[c,d]重叠,

对应于c在[a,b]中的事实];和[a,d]

和[c,b]重叠,对应于ci的事实s in

[a,d])。

图2


这些观察建议使用以下代码:

函数intervalMember(byVal x As Single,byVal a As

Single,byVal b As Single)_

As Boolean

''当且仅当x是区间的成员[a,b]

返回(a< = x)和(x $时为真b $ b结束函数


函数intervalOverlap(byVal a As Single,byVal b As

Single,_

byVal x as

Single,byVal y As Single)As Boolean

''当且仅当区间[a,b]和[x,y]
$ b时为真$ b相交

返回intervalMember(a,x,y)或intervalMember(b,x,

y)或_

intervalMember(x, a,b)或

intervalMember(y,a,b)

结束函数


函数controlOverlap(byVal c1 As Control ,byVal c2 As

Control)As Boolean

''参数被认为是occ的控件upy

矩形,由

''。Top,.Left,.Height和.Width属性决定。函数

是真的,当且仅当参数占用的矩形

''重叠时。

返回intervalOverlap(c1.Top,c1。顶部+ c1.Height,_


c2.Top,c2.Top + c2.Height)和_

intervalOverlap(c1.Left,

c1.Left + c1.Width,_


c2.Left,c2.Left + c2.Width)

结束函数

可能的修改:如果你想要一个接近未命中数来计算

作为命中,只需扩展你测试的间隔。对于

示例,您可能已经定义了一个小的正数

MARGIN,并将

替换为上面的最后一个函数。

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.

推荐答案





没有人这里将做你的作业。


-

Herfried K. Wagner

MVP·VB Classic,VB.NET

< http://www.mvps.org/dotnet>



Nobody here will do your homework.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>



这篇关于检测目标的命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:11