我正在制作一个月球着陆器游戏,为每个新游戏随机化地形,因此每个游戏都是不同的。对于我的游戏,我正在创建一个图形对象,为游戏的地形绘制一条闭合曲线。我试图检测图片对象是否与图形对象发生碰撞。我像这样创建图形对象:
Private Sub generateRandomTerrain()
Dim terrainBrush As New SolidBrush(Color.Gray)
Dim terrainDraw As Graphics = Me.CreateGraphics
'Generate Different Points for Terrain
Me.xpoint1 = (Rnd() * 10) + (Rnd() * 20) + (Rnd() * 10) - (Rnd() * 20)
Me.ypoint1 = (Rnd() * 30) + (Rnd() * 10) - (Rnd() * 20) + (Rnd() + 5)
Me.xpoint2 = (Rnd() * 10) + (Rnd() * 20) + (Rnd() * 10) - (Rnd() * 20)
Me.ypoint2 = (Rnd() * 30) + (Rnd() * 10) + (Rnd() * 20) + (Rnd() + 5) + (Rnd() * 30) - (Rnd() * 30)
Me.xpoint3 = (Rnd() * 10) + (Rnd() * 20) + (Rnd() * 10) - (Rnd() * 20)
Me.ypoint3 = (Rnd() * 30) + (Rnd() * 10) - (Rnd() * 20) + (Rnd() + 5) - (Rnd() * 30) + (Rnd() * 30)
Me.xpoint4 = (Rnd() * 10) + (Rnd() * 20) + (Rnd() * 10) - (Rnd() * 20)
Me.ypoint4 = (Rnd() * 30) + (Rnd() * 10) + (Rnd() * 20) + (Rnd() + 5) - (Rnd() * 30) + (Rnd() * 30)
'Add System to make sure that hills are not too sharp???
'Generate Points to Draw
Dim terrain() As Point = {New Point(0, Me.Size.Height), New Point(0, Me.Size.Height - 100), New Point((Me.Size.Width * 0.2) + Me.xpoint1, Me.Size.Height - Me.ypoint1 - 100), New Point((Me.Size.Width * 0.45) + Me.xpoint2, Me.Size.Height - Me.ypoint2 - 100), New Point((Me.Size.Width * 0.75) - Me.xpoint3, Me.Size.Height - 100 - Me.ypoint3), New Point((Me.Size.Width * 0.8) + (Me.Size.Width * 0.05) - Me.xpoint4, Me.Size.Height - Me.ypoint4 - 100), New Point(Me.Size.Width, Me.Size.Height - 100), New Point(Me.Size.Width, Me.Size.Height)}
'Is Terrain Drawn-Clear
If Me.isTerrainDrawn = 1 Then
terrainDraw.Clear(Color.Transparent)
Me.isTerrainDrawn = 0
End If
'Draw Terrain Aspects
terrainDraw.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
terrainDraw.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
terrainDraw.CompositingMode = Drawing2D.CompositingMode.SourceOver
'Draw Terrain
terrainDraw.FillClosedCurve(terrainBrush, terrain)
'Set Terrain to Drawn
Me.isTerrainDrawn = 1
End Sub
我尝试了很多方法,但似乎都没有。
谢谢。
答案 0 :(得分:1)
使用System.Drawing.Drawing2D.GraphicsPath
类将您的路径创建为样条曲线。您可以使用AddClosedCurve
方法将点添加到路径中。然后,您可以使用path.IsVisible(point)
或path.IsVisible(x, y)
来测试点是否在闭合曲线内。
最后,您可以使用terrainDraw.FillPath(brush, path)
您的代码
Me.xpoint1 = (Rnd() * 10) + (Rnd() * 20) + (Rnd() * 10) - (Rnd() * 20)
可以简化为
Me.xpoint1 = 20 * Rnd()
作为10 Rnd + 20 Rnd + 10 Rnd - 20 Rnd == 20 Rnd
。通过添加四个随机值,您将无法获得更好的随机值。
Region
课程也很有趣。它可以包含矩形和图形路径,并且具有Intersect
方法,该方法返回自身与另一个Region
,GraphicsPath
或Rectangle
的交集。它还有一个Complement
方法,可用于获取地形外部。如果您将地形的外部与某个图形对象相交并且结果为空(您可以使用IsEmpty
方法测试),则表示没有碰撞。