在 Visual Basic 中检测图形对象和图片对象之间的碰撞

发布于 2024-12-11 01:01:09 字数 2238 浏览 0 评论 0原文

我正在制作一款月球着陆器游戏,它会随机化每个新游戏的地形,因此每个游戏都是不同的。对于我的游戏,我正在创建一个图形对象,为游戏地形绘制一条闭合曲线。然后我试图检测图片对象是否与图形对象发生碰撞。我像这样创建图形对象:

    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

我尝试了很多方法来执行此操作,但似乎都不起作用。

谢谢。

I am making a lunar lander game that randomizes the terrain for each new game, so each game is different. For my game, I am creating a graphics object that draws a closed curve for the terrain of the game. I am trying to then detect if a picture object collides with the graphics object. I create the graphics object like so:

    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

I have tried many ways of doing this, but none of them seem to work.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2024-12-18 01:01:09

使用 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 方法,该方法返回其自身与另一个 RegionGraphicsPathRectangle 的交集。它还有一个 Complement 方法,您可以使用它来获取地形的外部。如果将地形外部与某个图形对象相交并且结果为空(可以使用 IsEmpty 方法进行测试),则意味着没有碰撞。

Create your on path as a spline curve by using the System.Drawing.Drawing2D.GraphicsPath class. You can add the points to the path with the AddClosedCurve method. Then you can test if a point lies within the closed curve with path.IsVisible(point) or path.IsVisible(x, y).

Finally you can draw the path with terrainDraw.FillPath(brush, path)


Your code

Me.xpoint1 = (Rnd() * 10) + (Rnd() * 20) + (Rnd() * 10) - (Rnd() * 20)

could be simplified as

Me.xpoint1 = 20 * Rnd()

As 10 Rnd + 20 Rnd + 10 Rnd - 20 Rnd == 20 Rnd. You will not get a better random value by adding four random values.


The Region class is also interesting. It can contain rectangles and graphic paths and has an Intersect method, which returns the intersection of itself and another Region, GraphicsPath or Rectangle. It also has a Complement method that you could use to get the outside of the terrain. If you intersect the outside of the terrain with some graphic object and the result is empty (which you can test with the IsEmpty method), it means that there is no collision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文