Jump to content
  • Advertisement
Sign in to follow this  
cambalinho

VB2010: how can i make a good wall collision?

This topic is 585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm trying do a good wall collision:

Private Sub sprPlayer_Collision(ByVal ControlName As System.Windows.Forms.Control) Handles sprPlayer.Collision

        'getting points:
        If ControlName.Name Like "Diamante#" Then
            ControlName.Dispose()
            intScore += 10
            Me.Text = "Score: " + intScore.ToString + " Animation: " + intFramesPerSecond.ToString + "fps"
            Static i As Integer = 0
            i += 1
            If i = NumberOfDiamonds Then
                sprPlayer.StopAnimation = True
                MsgBox("end of game")
                End
            End If

            'wall collision:
        ElseIf ControlName.Name Like "sprWall#" Then
            If strFileName = "Esquerda" Then
                dcDirectionCollision.Left = True
                sprPlayer.Left += intPlayerSpeed
            ElseIf strFileName = "Direita" Then
                dcDirectionCollision.Right = True
                sprPlayer.Left -= intPlayerSpeed
            ElseIf strFileName = "Cima" Then
                dcDirectionCollision.Up = True
                sprPlayer.Top += intPlayerSpeed
            ElseIf strFileName = "Baixo" Then
                dcDirectionCollision.Down = True
                sprPlayer.Top -= intPlayerSpeed
            End If
        End If
    End Sub

    Private Sub sprPlayer_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles sprPlayer.KeyDown
        'If sprPlayer.Opacy = 0 Then sprPlayer.Opacy = 0.25
        sprPlayer.StopAnimation = False
        If e.KeyCode = Keys.Up Then
            If (dcDirectionCollision.Up = False) Then
                sprPlayer.Top -= intPlayerSpeed
                If strFileName <> "Cima" Then
                    sprPlayer.BackgroundImage = My.Resources.Resource1.Cima
                    strFileName = "Cima"
                End If
                dcDirectionCollision.Down = False
                dcDirectionCollision.Left = False
                dcDirectionCollision.Right = False
            End If
        ElseIf e.KeyCode = Keys.Down Then
            If (dcDirectionCollision.Down = False) Then
                sprPlayer.Top += intPlayerSpeed
                If strFileName <> "Baixo" Then
                    sprPlayer.BackgroundImage = My.Resources.Resource1.Baixo
                    strFileName = "Baixo"
                End If
                dcDirectionCollision.Up = False
                dcDirectionCollision.Left = False
                dcDirectionCollision.Right = False
            End If
        ElseIf e.KeyCode = Keys.Left Then
            If dcDirectionCollision.Left = False Then
                sprPlayer.Left -= intPlayerSpeed
                If strFileName <> "Esquerda" Then
                    'sprPlayer.BackgroundImage = Image.FromFile("Jogador\Esquerda.gif")
                    sprPlayer.BackgroundImage = My.Resources.Resource1.Esquerda
                    strFileName = "Esquerda"
                End If
                dcDirectionCollision.Down = False
                dcDirectionCollision.Up = False
                dcDirectionCollision.Right = False
            End If
        ElseIf e.KeyCode = Keys.Right Then
            If dcDirectionCollision.Right = False Then
                sprPlayer.Left += intPlayerSpeed
                If strFileName <> "Direita" Then
                    sprPlayer.BackgroundImage = My.Resources.Resource1.Direita
                    strFileName = "Direita"
                End If
                dcDirectionCollision.Down = False
                dcDirectionCollision.Left = False
                dcDirectionCollision.Up = False
            End If
        ElseIf e.KeyCode = Keys.Escape Then
            End
        End If
    End Sub

but i have 1 bad effect when i do these steps(it's 1 way, theres for others directions too):

1 - move right to the wall;

2 - the collision is detected and then don't moves;

3 - i move up(just 3 or 4 pixels);

4 - i move right to the wall;

5 - i don't understand why, but the player, automatic, moves down :(

i don't understand what is wrong with my code... can anyone advice me more?

Share this post


Link to post
Share on other sites
Advertisement

Don't eject from walls by just inverting the movement vector, there are several better approaches. Also labelling your walls and giving them predefined behaviour based on their names will cause a lot of problems in the long run. Treat all walls as the same thing and use the player's position/previous frame's position/movement direction for wall collision/ejection.

 

A few potential solutions:

 

-Apply movement one pixel at a time in a for loop and break upon collision. If you move many pixels/frame this can result in a lot of collision math calls in a single frame, so be wary.

-Keep track of the player's position on the previous frame, use that to determine which side of the wall they hit, then snap them to that edge's coordinate

-Eject to nearest edge of the wall. If your walls are small and your movement speed is high this can result in tunnelling, though.

Share this post


Link to post
Share on other sites

i found another nice way: i test the colision on next movement before do it. if theres a colision the movement isn't done ;)

Private Sub sprPlayer_KeyBoardDown(key As System.Windows.Forms.Keys) Handles sprPlayer.KeyBoardDown
        Dim rectPlayer As Rectangle
        If key = Keys.Up Then 'UP
            sprPlayer.StopAnimation = False
            If strFileName <> "Cima" Then
                sprPlayer.BackgroundImage = My.Resources.Resource1.Cima
                strFileName = "Cima"
            End If
            rectPlayer = New Rectangle(New Point(sprPlayer.Left, sprPlayer.Top - intPlayerSpeed), New Size(sprPlayer.Width, sprPlayer.Height))
            If rectPlayer.IntersectsWith(sprWall1.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall2.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall3.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall4.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall5.Bounds) = False Then
                sprPlayer.Top -= intPlayerSpeed
            End If
        ElseIf key = Keys.Down Then 'Down
            sprPlayer.StopAnimation = False
            If strFileName <> "Baixo" Then
                sprPlayer.BackgroundImage = My.Resources.Resource1.Baixo
                strFileName = "Baixo"
            End If
            rectPlayer = New Rectangle(New Point(sprPlayer.Left, sprPlayer.Top + intPlayerSpeed), New Size(sprPlayer.Width, sprPlayer.Height))
            If rectPlayer.IntersectsWith(sprWall1.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall2.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall3.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall4.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall5.Bounds) = False Then
                sprPlayer.Top += intPlayerSpeed
            End If
        ElseIf key = Keys.Left Then 'Left
            sprPlayer.StopAnimation = False
            If strFileName <> "Esquerda" Then
                sprPlayer.BackgroundImage = My.Resources.Resource1.Esquerda
                strFileName = "Esquerda"
            End If
            rectPlayer = New Rectangle(New Point(sprPlayer.Left - intPlayerSpeed, sprPlayer.Top), New Size(sprPlayer.Width, sprPlayer.Height))
            If rectPlayer.IntersectsWith(sprWall1.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall2.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall3.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall4.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall5.Bounds) = False Then
                sprPlayer.Left -= intPlayerSpeed
            End If
        ElseIf key = Keys.Right Then 'Right
            sprPlayer.StopAnimation = False
            If strFileName <> "Direita" Then
                sprPlayer.BackgroundImage = My.Resources.Resource1.Direita
                strFileName = "Direita"
            End If
            rectPlayer = New Rectangle(New Point(sprPlayer.Left + intPlayerSpeed, sprPlayer.Top), New Size(sprPlayer.Width, sprPlayer.Height))
            If rectPlayer.IntersectsWith(sprWall1.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall2.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall3.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall4.Bounds) = False AndAlso rectPlayer.IntersectsWith(sprWall5.Bounds) = False Then
                sprPlayer.Left += intPlayerSpeed
            End If
        ElseIf key = Keys.Escape Then
            End
        End If
    End Sub

thanks for all

Edited by cambalinho

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!