【问题标题】:Difficulties with wall collisions in a 3rd person game VB6第三人称游戏VB6中的墙壁碰撞困难
【发布时间】:2011-10-11 10:27:24
【问题描述】:

好的,所以我正在尝试在 VB6 中为一个班级项目制作第三人称游戏,当这个人与墙壁(形状)碰撞时,他们不应该移动。但问题是,当人撞到墙上时,它会停下来,但是现在墙现在被卡住了,不会与所有其他墙一起滚动。这是我的代码:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyLeft Or vbKeyRight Or vbKeyUp Or vbKeyDown Then
        tmrMove.Enabled = True
    End If

    Select Case KeyCode
        Case vbKeyLeft
            XVel = 0 - Speed
            YVel = 0
        Case vbKeyRight
            XVel = Speed
            YVel = 0
        Case vbKeyUp
            YVel = 0 - Speed
            XVel = 0
        Case vbKeyDown
            YVel = Speed
            XVel = 0
    End Select

    Keys(KeyCode) = True
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Keys(KeyCode) = False
    If Keys(vbKeyLeft) = False And Keys(vbKeyRight) = False And Keys(vbKeyUp) = False And Keys(vbKeyDown) = False Then
        XVel = 0
        YVel = 0
    End If
End Sub


Private Sub tmrMove_Timer()
    For i = 0 To (Wall.Count - 1)
        If Collision(Character, Wall(i)) = False Then
            Wall(i).Left = Wall(i).Left - XVel
            Wall(i).Top = Wall(i).Top - YVel
        End If
    Next i
End Sub


Public Function Collision(Shape1 As ShockwaveFlash, Shape2 As Shape) As Boolean
    If (Shape1.Left + Shape1.Width) > Shape2.Left And _
    Shape1.Left < (Shape2.Left + Shape2.Width) And _
    (Shape1.Top + Shape1.Height) > Shape2.Top And _
    Shape1.Top < (Shape2.Top + Shape2.Height) Then
        Collision = True
    Else
        Collision = False
    End If
End Function

现在你可以看到,问题是当它碰撞时,我不知道如何“解除碰撞”,所以我们碰撞的墙卡住了,不会随着其他东西滚动。解释起来很混乱,希望你能理解。谢谢

如你所见,

【问题讨论】:

    标签: vb6 collision-detection


    【解决方案1】:

    解决碰撞逻辑最直接的方法是考虑以下问题:

    • 我可以向上移动吗?
    • 我可以下楼吗?
    • 我可以向左移动吗?
    • 我可以向右移动吗?

    而不是“我在与墙壁相撞吗?”的问题

    您可以通过将移动后的位置与墙壁施加的限制进行比较来回答这些问题。

    代码示例(请客气……我在过去 10 年没有编写 VB6 ;-)

    Public Function CanMoveLeft(Shape1 As ShockwaveFlash, Shape2 As Shape) As Boolean
        If (Shape1.Left + Shape1.Width) > Shape2.Right) 
        Then
            CanMoveLeft = True
        Else
            CanMoveLeft = False
        End If
    End Function
    

    此示例假定您已将提议的新职位应用到Shape1。如果您愿意,可以传入未移动的Shape1 以及向左的速度并相应地修改计算。我认为您可能希望在代码示例中将形状的左边缘与墙的 边缘而不是墙的左边缘进行比较。

    请注意,如果您的移动后位置会将您置于墙内,您可能需要将实际位置调整为刚好在房间内(如果您每帧移动超过一个像素,您的速度可能会将您的墙内或墙外的当前位置)。

    【讨论】:

      猜你喜欢
      • 2018-05-06
      • 2022-01-11
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2018-01-21
      相关资源
      最近更新 更多