第三人称游戏 VB6 中墙壁碰撞的困难

发布于 2024-12-09 10:03:30 字数 1652 浏览 0 评论 0原文

好的,我正在尝试用 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

现在如您所见,问题是当它碰撞时,我不知道如何“取消碰撞”,因此我们碰撞的墙壁会被卡住,并且不会与其余的东西一起滚动。解释起来很混乱希望你能理解。谢谢

如你所见,

Ok so I am trying to make a third person game in VB6 for a class project, and when the person collides with a Wall (shape) then they shouldnt move. But the problem is, when the person collides into the wall, it stops, however now the wall is now stuck and wont scroll along with all the other walls. Here is my code:

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

Now as you can see, the problem is that when it collides, I dont know howto "uncollide" so the wall that we collided with becomes stuck and will not scroll with the rest of the things. It is confusing to explain hopefully you understand. Thanks

As you can see,

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

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

发布评论

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

评论(1

白日梦 2024-12-16 10:03:30

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

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

而不是问题“我是否与墙壁相撞?”

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

代码示例(说实话...过去 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 以及向左速度,并相应地修改计算。我认为您可能想要将形状的左边缘与墙的右边缘进行比较,而不是代码示例中墙的左边缘。

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

The most straightforward way to fix your collision logic is to consider the questions:

  • Can I move up?
  • Can I move down?
  • Can I move left?
  • Can I move right?

rather than the question "am I in a collision with the wall?"

You answer those questions by comparing your post-move position to the limit imposed by the wall.

Code example (be kind... I didn't write VB6 for the last 10 years ;-)

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

This example presumes you have already applied a proposed new position to Shape1. If you prefer, you can pass in the unmoved Shape1 along with the leftward velocity and modify the calculation accordingly. I think you probably want to compare the left edge of the shape to the right edge of the wall rather than the left edge of the wall in your code sample.

Note that if your post-move position would place you inside the wall, you would want to adjust the actual position to be just inside the room (if you're moving more than one pixel per frame, your velocity can put your current position inside or beyond the wall).

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