Results 1 to 3 of 3

Thread: [RESOLVED] [VB6] - wall collision precision

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Resolved [RESOLVED] [VB6] - wall collision precision

    is these function correct for test the collision?
    Code:
    Public Function TestCollision(X1 As Long, Y1 As Long, Width1 As Long, Height1 As Long, X2 As Long, Y2 As Long, Width2 As Long, Height2 As Long) As Boolean
        If (X1 + Width1 >= X2 And X1 <= X2 + Width2) And (Y1 + Height1 >= Y2 And Y1 <= Y2 + Height2) Then
            TestCollision = True
        Else
            TestCollision = False
        End If
    End Function
    i'm trying use it, but isn't working
    Code:
    Private Type CollisionDirections
        Left As Boolean
        Right As Boolean
        Up As Boolean
        Down As Boolean
    End Type
    
    Const PlayerSpeed As Integer = 5
    Dim dirCollision As CollisionDirections
    
    Private Sub sprPlayer_KeyDown(KeyCode As Integer, Shift As Integer, KeyDownTime As Long)
        Dim i As Integer
        
        If (KeyCode = vbKeyLeft) Then
            For i = 0 To sprWall.Count - 1
                If (sprPlayer.TestCollision(sprPlayer.Left - PlayerSpeed, sprPlayer.Top, sprPlayer.Width, sprPlayer.Height, sprWall(i).Left, sprWall(i).Top, sprWall(i).Width, sprWall(i).Height) = True) Then
                    dirCollision.Left = True
                Else
                    dirCollision.Left = False
                End If
                
                
            Next i
            
            If dirCollision.Left = False Then sprPlayer.Left = sprPlayer.Left - PlayerSpeed
    .......
    anyone can tell me what i'm doing wrong?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [VB6] - wall collision precision

    You've got the loop wrong... as you will keep setting dirCollision.Left for each wall, so it will only store the last value.

    Here is one way you could fix it:
    Code:
        If (KeyCode = vbKeyLeft) Then
            dirCollision.Left = False
            For i = 0 To sprWall.Count - 1
                If sprPlayer.TestCollision(sprPlayer.Left - PlayerSpeed, sprPlayer.Top, sprPlayer.Width, sprPlayer.Height, sprWall(i).Left, sprWall(i).Top, sprWall(i).Width, sprWall(i).Height) Then
                    dirCollision.Left = True
                    Exit For
                End If
            Next i

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - wall collision precision

    Quote Originally Posted by si_the_geek View Post
    You've got the loop wrong... as you will keep setting dirCollision.Left for each wall, so it will only store the last value.

    Here is one way you could fix it:
    Code:
        If (KeyCode = vbKeyLeft) Then
            dirCollision.Left = False
            For i = 0 To sprWall.Count - 1
                If sprPlayer.TestCollision(sprPlayer.Left - PlayerSpeed, sprPlayer.Top, sprPlayer.Width, sprPlayer.Height, sprWall(i).Left, sprWall(i).Top, sprWall(i).Width, sprWall(i).Height) Then
                    dirCollision.Left = True
                    Exit For
                End If
            Next i
    thanks.. it's working
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width