Difficulties with wall collisions in a 3rd person game VB6
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 b开发者_运维问答ecomes stuck and will not scroll with the rest of the things. It is confusing to explain hopefully you understand. Thanks
As you can see,
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).
精彩评论