经过一些研究,我决定使用 Liang-Barsky 在我的2D游戏中,线剪切算法。 Google并未传递此算法的任何VB.NET实现,而是提供了大量C/++。因此,正如我在C ++方面的知识,决定 在Skytopia上发现的港口 到vb.net。不幸的是,它不适合:

Public Class PhysicsObject
    Public Function CollideRay(ByVal p0 As Point, ByVal p1 As Point, ByRef clip0 As Point, ByRef clip1 As Point) As Boolean
        Dim t0 As Double = 0.0
        Dim t1 As Double = 1.0
        Dim xdelta As Double = p1.X - p0.X
        Dim ydelta As Double = p1.Y - p0.Y
        Dim p, q, r As Double

        For edge = 0 To 3
            ' Traverse through left, right, bottom, top edges
            If (edge = 0) Then
                p = -xdelta
                q = -(AABB.Left - p0.X)
            ElseIf (edge = 1) Then
                p = xdelta
                q = (AABB.Right - p0.X)
            ElseIf (edge = 2) Then
                p = -ydelta
                q = -(AABB.Bottom - p0.Y)
            ElseIf (edge = 3) Then
                p = ydelta
                q = (AABB.Top - p0.Y)
            End If

            r = q / p

            If p = 0 And q < 0 Then Return False ' Don't draw line at all. (parallel line outside)

            If p < 0 Then
                If r > t1 Then
                    Return False ' Don't draw line at all.
                ElseIf r > t0 Then
                    t0 = r ' Line is clipped!
                End If
            ElseIf p > 0 Then
                If r < t0 Then
                    Return False ' Don't draw line at all.
                ElseIf r < t1 Then
                    t1 = r ' Line is clipped!
                End If
            End If
        Next

        clip0.X = p0.X + t0 * xdelta
        clip0.Y = p0.Y + t0 * ydelta
        clip1.X = p0.X + t1 * xdelta
        clip1.Y = p0.Y + t1 * ydelta

        Return True        ' (clipped) line is drawn
    End Function

    Public AABB As Rectangle
End Class

我正在使用类似的类/方法:

    Dim testPhysics As PhysicsObject = New PhysicsObject
    testPhysics.AABB = New Rectangle(30, 30, 20, 20)

    Dim p0, p1 As Point
    p0 = New Point(0, 0)
    p1 = New Point(120, 120)

    Dim clip0, clip1 As Point
    clip0 = New Point(-1, -1)
    clip1 = New Point(-1, -1)

    GlobalRenderer.Graphics.DrawLine(Pens.LimeGreen, p0, p1)

    If testPhysics.CollideRay(p0, p1, clip0, clip1) Then
        GlobalRenderer.Graphics.DrawLine(Pens.Magenta, clip0, clip1)
    End If

然而 对撞机 方法在其第三端迭代(Edge = 3),r <t0上失败,因此该函数返回false。

我想知道是否有人可以发现我的问题 对撞机 功能会导致这种行为,因为我确实很困难。

提前致谢。

有帮助吗?

解决方案

该代码假设一个不同的坐标系,请注意,Topedge是 更大 比链接网页中的底部格。您的测试与普通图形坐标合作,其中底部大于顶部。您必须交换底部和最高参数。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top