Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg
Collision detection for tile rpg Collision detection for tile rpg
Collision detection for tile rpg
Go Back  Xtreme Visual Basic Talk > > > Collision detection for tile rpg


Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2012, 07:00 AM
Dobbydoo Dobbydoo is offline
Newcomer
 
Join Date: Feb 2012
Posts: 1
Default Collision detection for tile rpg


I'm trying ot make a top down rpg, or pokemon like game, but I can't find any proper tutorials for collision detection. I have found one, but when I did it it doesn't seem to work properly, and I get errors everytime I go to the edge of the form. Can anyone try to explain and show me what I can do to make good collision detection for walls?
Here's my code(much of it is just copy-pasted from a tutorial):
HTML Code:
Public Class Form1

    Public Level1 As New clsLevel()
    Public Dude As New clsSprite(Level1)
    Dim CanWalk As Boolean = True

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Dim x As Integer

        If CanWalk Then
            Application.DoEvents()

            While x < 30
                CanWalk = False
                x = x + 1
                Select Case e.KeyCode

                    Case Keys.Down
                        If Dude.AllowedToWalk(clsSprite.Dir.DOWN) Then
                            Dude.Pos.Y += clsLevel.Tile.Height / 30
                        End If
                    Case Keys.Up
                        If Dude.AllowedToWalk(clsSprite.Dir.UP) Then
                            Dude.Pos.Y -= clsLevel.Tile.Height / 30
                        End If
                    Case Keys.Right
                        If Dude.AllowedToWalk(clsSprite.Dir.RIGHT) Then
                            Dude.Pos.X += clsLevel.Tile.Width / 30
                        End If
                    Case Keys.Left
                        If Dude.AllowedToWalk(clsSprite.Dir.LEFT) Then
                            Dude.Pos.X -= clsLevel.Tile.Height / 30
                        End If

                End Select
                Me.Invalidate()
            End While
            CanWalk = True
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Level1.SetUpTiles()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawRectangle(New Pen(Color.Blue), Dude.Pos.X, Dude.Pos.Y, Level1.Tile.Width, Level1.Tile.Height)
    End Sub
End Class

Public Class clsSprite
    Public Pos As Point
    Public Level As New clsLevel

    Public Enum Dir
        UP
        DOWN
        RIGHT
        LEFT
    End Enum

    Public Function AllowedToWalk(ByVal Direction As Dir) As Boolean
        Select Case Direction
            Case Dir.DOWN


                If Level.Passable(TilePos.X, TilePos.Y + 1) = True Then 'The Tile below him
                    Return True
                Else
                    Return False
                End If

            Case Dir.LEFT

                If Level.Passable(TilePos.X - 1, TilePos.Y) = True Then 'The Tile to his Left
                    Return True
                Else
                    Return False
                End If

            Case Dir.RIGHT

                If Level.Passable(TilePos.X + 1, TilePos.Y) = True Then 'The Tile to his right
                    Return True
                Else
                    Return False
                End If


            Case Dir.UP

                If Level.Passable(TilePos.X, TilePos.Y - 1) = True Then 'The Tile above him
                    Return True
                Else
                    Return False
                End If

        End Select
    End Function

    Public ReadOnly Property Tilepos() As Point
        Get
            Return New Point((Pos.X / Level.Tile.Width), (Pos.Y / Level.Tile.Height))
        End Get
    End Property

    Public Sub New(ByVal TheLevel As clsLevel)
        Level = TheLevel
    End Sub

End Class

Public Class clsLevel
    Public Shared Tile As Size = New Size(30, 30)
    Public Passable(20, 20) As Boolean 'make the map 20x20

    Public Sub SetUpTiles()
        Dim x, y As Integer

        For x = 0 To 20
            For y = 0 To 20
                Passable(x, y) = True 'Set every element in Passable to True
            Next
        Next

        Passable(10, 10) = False 'and then make the middle tile not passable
    End Sub
End Class
Reply With Quote
  #2  
Old 02-25-2012, 04:02 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Picking the right working source code example..

You forgot to mention where (which tutorial) you code came from.
I believe it is from here:
http://www.vbprogramming.8k.com/tuto...dCollision.htm

The issue with that site is that working source code examples used to be available from a download link in the forums, but that link has been broken for quite a while.

I suggest you look at some working examples of doing collisions.
This is a nice thread by IcePlug in the forum's Code Library:
Overlaps & Collision Detection.

I believe post #8 in that thread covers "Inter-tile Collision in a Tile Map".
You might want to start out with the "SimplerVersion.zip" attachment to that post.

Last edited by surfR2911; 02-25-2012 at 04:08 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg Collision detection for tile rpg
Collision detection for tile rpg
Collision detection for tile rpg
 
Collision detection for tile rpg
Collision detection for tile rpg
 
-->