Tile Collision help

BlitzMax Forums/BlitzMax Programming/Tile Collision help

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hi all, I need some help with the code below, specifically detecting collisions correctly

if I move with increments of 32(tile size) collision is perfect, but obviously this is not ideal, so if I move say 3 unit increments, collision gets screwy, as in the example below. Can anyone help me this ?




'tile map example by Deux


Strict
Graphics 640,480,0

Const TILE_TYPE_WALL=1
Const TILE_TYPE_ROOF=2
Const TILE_TYPE_GROUND=3

Const MAP_WIDTH = 10
Const MAP_HEIGHT = 10



'SetClsColor 155,155,155
Type TTile
	Field x#
	Field y#
	Field width#
	Field height#
	Field red#
	Field green#
	Field blue#
	Field TileType%
	Field image:TImage
End Type


Global map[] = [2,2,2,2,2,2,2,2,2,2,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,3,3,3,3,3,3,3,3,3,3]

Global TileList:TList=CreateList()

Global playery#=64
Global playerx#=64
Global oldx#
Global oldy#

Global x#
Global y#



Function LoadMap()

	Local tile_countx%=0
	Local tile_county%=0
	
	For Local x%=EachIn map
		Local t:TTile = New TTile
		If(tile_countx = MAP_WIDTH) Then
			tile_county :+1
			tile_countx = 0
		End If	
		
		
		Select x
		
			Case 1
				t.red# = tile_countx*10
				t.green# = tile_county*10
				t.blue# = 125
				t.width#=32
				t.height#=32
				t.x# = (tile_countx)*t.width#
				t.y#=(tile_county)*t.height#
				t.TileType = TILE_TYPE_WALL
				TileList.AddLast t
			Case 2
				t.red# = tile_countx*10
				t.green# = tile_county*10
				t.blue# = 125
				t.width#=32
				t.height#=32
				t.x# = (tile_countx)*t.width#
				t.y#=(tile_county)*t.height#
				t.TileType = TILE_TYPE_ROOF
				TileList.AddLast t
			Case 3
				t.red# = tile_countx*10
				t.green# = tile_county*10
				t.blue# = 125
				t.width#=32
				t.height#=32
				t.x# = (tile_countx)*t.width#
				t.y#=(tile_county)*t.height#
				t.TileType = TILE_TYPE_GROUND
				TileList.AddLast t
			
		End Select
			
	tile_countx :+ 1


	Next

End Function

Function RenderMap()
	For Local t:TTile=EachIn TileList
		SetColor t.red#,t.green#,t.blue#
		DrawRect t.x#,t.y#,32,32
		DrawText t.tiletype,t.x#,t.y#
	Next
End Function


LoadMap()

HideMouse

While Not KeyDown(KEY_ESCAPE)

	RenderMap()
	oldx# = playerx#
	oldy# = playery#

	If KeyDown(KEY_LEFT) playerx# :-3
	If KeyDown(KEY_RIGHT) playerx# :+3
	If KeyDown(KEY_UP) playery# :-3
	If KeyDown(KEY_DOWN) playery# :+3
	
	Local index:Int = playerx#/32+(playery#/32*MAP_HEIGHT)
	DrawText map[index],10,50
	
	If map[index] = 0 Then
	Else
		playerx# = oldx#
		playery# = oldy#
		
	End If
	
	DrawRect  playerx#,playery#,32,32
	
	Flip
	FlushMem
	Cls
	
	
Wend