
02-25-2012, 06:00 AM
|
|
Newcomer
|
|
Join Date: Feb 2012
Posts: 1
|
|
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
|
|