第三人称游戏 VB6 中墙壁碰撞的困难
好的,我正在尝试用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修复碰撞逻辑的最直接方法是考虑以下问题:
而不是问题“我是否与墙壁相撞?”
您可以通过将移动后的位置与隔离墙施加的限制进行比较来回答这些问题。
代码示例(说实话...过去 10 年我没有编写 VB6 ;-)
此示例假定您已经将建议的新位置应用于
Shape1
。如果您愿意,可以传入未移动的Shape1
以及向左速度,并相应地修改计算。我认为您可能想要将形状的左边缘与墙的右边缘进行比较,而不是代码示例中墙的左边缘。请注意,如果您的移动后位置会将您置于墙内,则您需要将实际位置调整为正好在房间内(如果您每帧移动超过一个像素,则您的速度可以将您当前的位置置于房间内)或越过墙)。
The most straightforward way to fix your collision logic is to consider the questions:
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 ;-)
This example presumes you have already applied a proposed new position to
Shape1
. If you prefer, you can pass in the unmovedShape1
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).