Tile Collision help

BlitzMax Forums/BlitzMax Programming/Tile Collision help

I now understand the above code, it works well, but how would one handle collision on slopes ?

Also another silly question, I am using a linear array, and pushing each element tile into a list, is there any advantage in rather using a multi-dimensional array instead of a linear array ? ala map[x][y] and looping through it ? Would it make life easier ?

I am just trying to gauge how this tiling thing is done.

'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

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()



Type TPlayer
	Global px:Float
	Global py:Float
	
	
	Function Update()
		Local index:Int=0
		Local y_comp:Int=0
		
		If KeyDown(KEY_LEFT) Then
				If px > 0 Then px:-3
				If px < 0 Then 
				px=0 
		Else
			' upper left
			index = px/32
			y_comp = py/32
			y_comp:*MAP_WIDTH
			index:+y_comp
		
			If map[index] <> 0 Then
				px:+ 3
			Else
				' lower left
				index=px/32
				y_comp = (py+32)/32
				y_comp:*MAP_WIDTH
				index :+ y_comp%
				If map[index] <> 0 Then px :+3
			End If
		
		
		End If
		End If

	   If KeyDown(KEY_RIGHT) Then 
				If px < (MAP_WIDTH*32)-32 Then px:+3
				If px+32 > (MAP_WIDTH*32)-32 Then 
				px=(MAP_WIDTH*32)-32
	   Else
			' upper right
			index=((px+32)/32)
			y_comp=(py/32)
			y_comp:*MAP_WIDTH
			index:+y_comp

			If map[index]<>0 Then 
				px:-3
			Else ' lower right corner
				index=((px#+32)/32)
				y_comp=((py#+32)/32)
				y_comp:*MAP_WIDTH
				index:+y_comp
				If map[index]<>0 Then px:-3
			EndIf
       EndIf
	   End If	

		If KeyDown(KEY_UP) Then 
			If py > 0 Then py:-3
			If py < 0 Then 
			py=0
		Else
			' check the characters upper left corner
				index=(px/32)
				y_comp=(py/32)
				y_comp:*MAP_HEIGHT
				index:+y_comp
				If map[index]<>0 Then 
					py:+3
 			    Else
		' check the characters upper right corner
					index=((px+32)/32)
					y_comp=(py/32)
					y_comp:*MAP_HEIGHT
					index:+y_comp
					If map[index]<>0 Then py:+3
				EndIf
		EndIf
	   End If

	   If KeyDown(KEY_DOWN) Then 
			If py < (MAP_HEIGHT*32)-32 Then py:+3
			If py > (MAP_HEIGHT*32)-32 Then 
			py=(MAP_HEIGHT*32)-32
	   Else
		' check the characters lower left corner
			index=(px/32)
			y_comp=((py+32)/32)
			y_comp:*MAP_HEIGHT
			index:+y_comp

			If map[index]<>0 Then 
				py#:-3
	   		Else
		' check the characters lower right corner
				index=((px+32)/32)
				y_comp:Int=((py+32)/32)
				y_comp:*MAP_HEIGHT
				index:+y_comp
				If map[index]<>0 Then py:-3
			End If	
	    EndIf
	End If	

	DrawRect  px,py,32,32

		
	End Function
	
	Function Draw()
		DrawRect  self.px#,self.py#,32,32
	End Function
	
End Type




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# = 255
				t.green# = 0
				t.blue# = 0
				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# = 0
				t.green# = 255
				t.blue# = 0
				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# = 0
				t.green# = 0
				t.blue# = 255
				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

TPlayer.px=64
TPlayer.py=64


While Not KeyDown(KEY_ESCAPE)

	RenderMap()
	
	TPlayer.Update()
	
	Flip
	FlushMem
	Cls
	
	
Wend

 




ok, was playing around a bit with this, i like muti-dim arrays better :)

Strict
Graphics 640,480,0


Global map%[5,5]
Global px#=128
Global py#=128

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

Function rendermap()

	For Local x:Int = 0 To 4
		For Local y:Int=0 To 4
			Local tile = map[x,y]
			If(tile=1) Then
				SetColor 255,0,0
			Else
				SetColor 0,255,0			
			End If
  		    DrawRect x*32,y*32,32,32

		Next
	Next 
End Function

While Not KeyDown(key_escape)

	If KeyHit(KEY_LEFT) px:-32
	If KeyHit(KEY_RIGHT) px:+32
	If KeyHit(KEY_UP) py:-32
	If KeyHit(KEY_DOWN) py:+32
	
	SetColor 255,255,255
	
	
	RenderMap

	SetColor 0,0,255
	DrawRect px,py,32,32
	If map[px/32,py/32]=1 DrawText "collision",10,10
	Flip
	Cls
Wend