Getting absolute Tile Position?

BlitzMax Forums/BlitzMax Beginners Area/Getting absolute Tile Position?

Amon(Posted 2007) [#1]
I have a TileMap drawn to screen. If the tiledata = 2 it draws my player at that tile position.

eg.

if Map[xy] = 2
Drawimage Player, x*32 , y*32, 0

Now, How would I get it's actual location across the screen (ie it's at coords 400,300 px)?

Another example below.

defdata 0,0,0,0,0
defdata 0,0,0,0,0
defdata 0,0,0,0,0
defdata 0,0,x,0,0

Where x is. I need to know it's screen location.

Am I making sense?


Amon(Posted 2007) [#2]
By the way. I'm trying some Platform Code I learned a long time ago from a guy called Alan Mcdonald. This is going back 5 years. Don't know if he is still around.

I can't find the thread where he helped me and am trying from memory to recode it for max.

Heres how far I've got so far.

SuperStrict


Graphics 800 , 600

Global Tiles:TImage = LoadAnimImage("ice_floortiles.png",32,32,0,8)
Global Player:TImage = LoadImage("player.png")


Const MAPWIDTH:Int = 800/32
Const MAPHEIGHT:Int = 600/32

Global Map:Int[MAPWIDTH , MAPHEIGHT]

Global PlayerX:Int
Global PlayerY:Int

Global Direction:String = "Standing"

ReadLevelData()

While Not KeyHit(KEY_ESCAPE)
	
	Cls
	
	DrawMap()
	DrawPlayer()
	MovePLayer()
	CheckIfPlayerCollideWithTile()
	Flip
	
	
Wend

Function DrawPlayer()
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 2
					'DrawImage Player , PlayerX+x*32 ,PlayerY + y*32 , 0
					DrawImage Player ,x*32 ,y*32 , 0
					Local TempPosX:Int = x/32
					
					DrawText "tempPosX = " + TempPosX , 0 , 20
			End Select
		Next
	Next	
End Function

Function MovePLayer()
	
	If KeyDown(KEY_LEFT)
		If Not KeyDown(KEY_RIGHT)
			Direction = "Moving_Left"
			PlayerX:-2
		EndIf
	ElseIf KeyDown(KEY_RIGHT)
		If Not KeyDown(KEY_LEFT)
			Direction = "Moving_Right"
			PlayerX:+2
		EndIf
	End If
End Function

Function CheckIfPlayerCollideWithTile()
	Local TempX:Int = PlayerX/32
	Local TempY:Int = PlayerY/32
	
	If Direction = "Moving_Right"
'	If TempX>0
		If Map[TempX, TempY] = 1
			PlayerX:-2
		End If
	EndIf
'	EndIf
	DrawText "TempX = "+TempX,0,0
End Function


Function DrawMap()
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 1
					DrawImage Tiles , x*32 , y*32 , 0
			End Select
		Next
	Next
End Function


Function ReadLevelData()
	For Local y:Int = 0 Until MAPHEIGHT
		For Local x:Int = 0 Until MAPWIDTH
			Local data:Int 
			ReadData data
			Map[x,y] = data
		Next
	Next
End Function



DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,2,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


As you can see I'm trying to get platform collision working.


Czar Flavius(Posted 2007) [#3]
I'm not quite sure what you're asking, but hopefully this function-tastic code will be helpful in some way? ;)

Assuming your tiles will have a size sizeX, and perhaps offset from the side of the screen offsetX.

Function getX:Int(x:Int)
   Return sizeX * x + offsetX
End Function


And a similar function for Y. Store the values at least as constants, rather than "magic numbers"

So to draw your tiles,

For Local x:Int = 0 Until MAPWIDTH
   For Local y:Int = 0 Until MAPHEIGHT
      Drawimage Blah, getX(x), getY(y), blahh
   Next
Next


I made a simple top-down tile game stored using an array. You can look at it if you want. It was written in the old Blitz Basic though.


Amon(Posted 2007) [#4]
Thanks for the code. I'll try it.

Can you send me your old BB code. My email is in my profile. :)


AltanilConard(Posted 2007) [#5]
The code you posted seems kind of weard in my eyes and I don't really understand how you were planning to keep track of the players location. I allways handle tile maps by keeping track of the player his ScreenX and Y location and his MapX and Y location (MapX = ScreenX/TILE_WIDTH). Here I edited your code to show you what I mean:
SuperStrict


Graphics 800 , 600

Global Tiles:TImage = LoadAnimImage("ice_floortiles.png",32,32,0,8)
Global Player:TImage = LoadImage("player.png")


Const MAPWIDTH:Int = 800/32
Const MAPHEIGHT:Int = 600/32

Global Map:Int[MAPWIDTH , MAPHEIGHT]

Global PlayerScreenX:Int
Global PlayerScreenY:Int
Global PlayerMapX:Int
Global PlayerMapY:Int

Global Direction:String = "Standing"

ReadLevelData()

While Not KeyHit(KEY_ESCAPE)
	
	Cls
	
	DrawMap()
	DrawPlayer()
	MovePLayer()
	CheckIfPlayerCollideWithTile()
	Flip
	
	
Wend

Function DrawPlayer()
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 2
					'DrawImage Player , PlayerX+x*32 ,PlayerY + y*32 , 0
					DrawImage Player, PlayerScreenX, PlayerScreenY, 0
			End Select
		Next
	Next	
End Function

Function MovePLayer()
	
	If KeyDown(KEY_LEFT)
		If Not KeyDown(KEY_RIGHT)
			Direction = "Moving_Left"
			PlayerScreenX:-2
		EndIf
	ElseIf KeyDown(KEY_RIGHT)
		If Not KeyDown(KEY_LEFT)
			Direction = "Moving_Right"
			PlayerScreenX:+2
		EndIf
	End If
End Function

Function CheckIfPlayerCollideWithTile()
	PlayerMapX = PlayerScreenX/32
	PlayerMapY = PlayerScreenY/32
	
	If Direction = "Moving_Right"
		If Map[PlayerMapX+1,PlayerMapY] = 1
			PlayerScreenX:-2
		End If
	ElseIf Direction = "Moving_Left"
		If Map[PlayerMapX,PlayerMapY] = 1
			PlayerScreenX:+2
		EndIf
	EndIf

	DrawText "PlayerMapX = "+PlayerMapX,0,0
End Function


Function DrawMap()
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 1
					DrawImage Tiles , x*32 , y*32 , 0
			End Select
		Next
	Next
End Function


Function ReadLevelData()
	For Local y:Int = 0 Until MAPHEIGHT
		For Local x:Int = 0 Until MAPWIDTH
			Local data:Int 
			ReadData data
			Map[x,y] = data
			If Map[x,y] = 2 Then
				PlayerMapX = x
				PlayerMapY = y
				PlayerScreenX = x*32
				PlayerScreenY = y*32
			EndIf
		Next
	Next
End Function



DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,2,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


(It only uses the 2 value of the map to determine the players starting location and then updates it to PlayerScreenX/Y and PlayerMapX/Y)


Amon(Posted 2007) [#6]
Hi. Thanks for the replies. I've made some progress.

Here it is. It Kind of works but not to the extent of a working platformer example.

Any help would be appreciated.

SuperStrict


Graphics 800 , 600

Global Tiles:TImage = LoadAnimImage("ice_floortiles.png",32,32,0,8)
Global Player:TImage = LoadImage("player.png")


Const MAPWIDTH:Int = 800/32
Const MAPHEIGHT:Int = 600/32

Global Map:Int[MAPWIDTH , MAPHEIGHT]

Global PlayerX:Float
Global PlayerY:Float

Global CheckPlayerPosition:Int = 0

Global Direction:String = "Standing"

Global Jump:Int = 0
Const Gravity:Float = 0.1
Global JumpHeight:Float = 3
Global CanJump:Int = 0
Global Falling:Int = 0

ReadLevelData()

While Not KeyHit(KEY_ESCAPE)
	
	Cls
	
	DrawMap()
	DrawPlayer()
	MovePLayer()
	DoJump()
	CheckIfPlayerCollideWithTile()
	
	Flip
	
	
Wend

Function DrawPlayer()
	If CheckPlayerPosition = 0
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 2
					'DrawImage Player ,x*32 ,y*32 , 0
					PlayerX = getX(x)
					PlayerY = getY(y)
					CheckPlayerPosition = 1
			End Select
		Next
	Next
	EndIf
	DrawText "PlayerX = " + PlayerX , 0 , 20
	DrawImage Player, PlayerX , PlayerY , 0	
End Function

Function MovePLayer()
	
	If KeyDown(KEY_LEFT)
		If Not KeyDown(KEY_RIGHT)
			Direction = "Moving_Left"
			PlayerX:-2
		EndIf
	ElseIf KeyDown(KEY_RIGHT)
		If Not KeyDown(KEY_LEFT)
			Direction = "Moving_Right"
			PlayerX:+2
		EndIf
	End If
End Function

Function CheckIfPlayerCollideWithTile()
	Select Direction
		Case "Moving_Right"
			If Jump = 0
			If Map[PlayerX/32 + 1, PlayerY/32] = 1
				PlayerX:-2
			EndIf
			EndIf
		Case "Moving_Left"
			If Jump = 0
			If Map[PlayerX/32, PlayerY/32] = 1
				PlayerX:+2
			End If
			endif
		Case "Standing"
			If Map[PlayerX/32 , PlayerY/32+1] = 1
				Jump = 0
				CanJump = 1
				JumpHeight = 3
				Falling = 0
			End If
	End Select
	
	If Falling = 1
		If Map[PlayerX/32 , PlayerY/32+1] = 1
			Jump = 0
			CanJump = 1
			JumpHeight = 3
			Falling = 0
			PlayerY:-PlayerY
		End If
	End If
	
	
End Function

Function DoJump()
	If KeyHit(KEY_SPACE) And CanJump = 1
		Jump = 1
		CanJump = 0
	End If
	
	If Jump = 1
		PlayerY:-JumpHeight
		JumpHeight:-Gravity
		If JumpHeight <=-5
			JumpHeight = -5
			Falling = 1
		EndIf
	End If
	
	
End Function

Function DrawMap()
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 1
					DrawImage Tiles , x*32 , y*32 , 0
			End Select
		Next
	Next
End Function


Function ReadLevelData()
	For Local y:Int = 0 Until MAPHEIGHT
		For Local x:Int = 0 Until MAPWIDTH
			Local data:Int 
			ReadData data
			Map[x,y] = data
		Next
	Next
End Function

'Function getX:Int(x:Int)
'   Return sizeX * x '+ offsetX
'End Function
'
'Function getY:Int(y:Int)
'   Return sizeX * y '+ offsetX
'End Function

Function getX:Int(x:Int)
   Return 32 * x '+ offsetX
End Function

Function getY:Int(y:Int)
   Return 32 * y '+ offsetX
End Function

DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,2,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1



Amon(Posted 2007) [#7]
Version 3. Again this Kind of Works. :/

Please someone help me figure out how to make platform games collision. :)

SuperStrict


Graphics 800 , 600

Global Tiles:TImage = LoadAnimImage("ice_floortiles.png",32,32,0,8)
Global Player:TImage = LoadImage("player.png")


Const MAPWIDTH:Int = 800/32
Const MAPHEIGHT:Int = 600/32

Global Map:Int[MAPWIDTH , MAPHEIGHT]

Global PlayerX:Float
Global PlayerY:Float

Global CheckPlayerPosition:Int = 0

Global Direction:Int = -1

Global Jump:Int = 0
Const Gravity:Float = 0.2
Global JumpHeight:Float = 5.5
Global CanJump:Int = 1
Global Falling:Int = 0
Global Jumping:Int = 0

ReadLevelData()

While Not KeyHit(KEY_ESCAPE)
	
	Cls
	
	DrawMap()
	DrawPlayer()
	MovePLayer()
	DoJump()
	CheckIfPlayerCollideWithTile()
	
	Flip
	
	
Wend

Function DrawPlayer()
	If CheckPlayerPosition = 0
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 2
					'DrawImage Player ,x*32 ,y*32 , 0
					PlayerX = getX(x)
					PlayerY = getY(y)
					CheckPlayerPosition = 1
			End Select
		Next
	Next
	EndIf
	DrawText "PlayerX = " + PlayerX , 0 , 20
	DrawImage Player, PlayerX , PlayerY - 3 , 0	
End Function

Function MovePLayer()
	
	If KeyDown(KEY_LEFT)
		If Not KeyDown(KEY_RIGHT)
			Direction = 0
			PlayerX:-2
		EndIf
	ElseIf KeyDown(KEY_RIGHT)
		If Not KeyDown(KEY_LEFT)
			Direction = 1
			PlayerX:+2
		EndIf
	End If
End Function

Function CheckIfPlayerCollideWithTile()
'#Region 
	Select Direction
		Case 1
			If Map[PlayerX/32+1, PlayerY/32] = 1
				PlayerX:-2
			EndIf
		Case 0
			If Map[PlayerX/32, PlayerY/32] = 1
				PlayerX:+2
			End If
	End Select
		
'#End Region 

	If Falling = 1
		PlayerY:+5
		If Map[PlayerX/32,PlayerY/32+1] = 1
			Jump = 0
			Falling = 0
			CanJump = 1
			JumpHeight = 5.5
		Else
			Falling = 0
		End If
	End If

End Function

Function DoJump()
	If KeyHit(KEY_SPACE) And CanJump = 1
		Jump = 1
		CanJump = 0
		Jumping = 1
	End If
	
	If Jump = 1
		PlayerY:-JumpHeight
		JumpHeight:-Gravity
		If JumpHeight <=-3
			Falling = 1
		EndIf
	End If
	
	
End Function

Function DrawMap()
	For Local x:Int = 0 Until MAPWIDTH
		For Local y:Int = 0 Until MAPHEIGHT
			Select Map[x,y]
				Case 1
					DrawImage Tiles , x*32 , y*32 , 0
			End Select
		Next
	Next
End Function


Function ReadLevelData()
	For Local y:Int = 0 Until MAPHEIGHT
		For Local x:Int = 0 Until MAPWIDTH
			Local data:Int 
			ReadData data
			Map[x,y] = data
		Next
	Next
End Function

'Function getX:Int(x:Int)
'   Return sizeX * x '+ offsetX
'End Function
'
'Function getY:Int(y:Int)
'   Return sizeX * y '+ offsetX
'End Function

Function getX:Int(x:Int)
   Return 32 * x '+ offsetX
End Function

Function getY:Int(y:Int)
   Return 32 * y '+ offsetX
End Function

DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,2,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1



Jesse(Posted 2007) [#8]
here I tried to fix your code but is taking me too long to figure out what you are doing I managed to fix the movement and added falling code sorry for removing the jump function but I thought it was better for all user input to be in the same function. It is just my opinion. anyway I was trying to figure out why you land in the middle of tiles but it was taking me too long and I don't have the time. maybe if no one solves it for you I might have time later on to do it. also I noted that you add the movement before you check for collition. I suggest you check for collition before you move the character and add after. I think it makes the code more readable. again just an opinion.



Dabz(Posted 2007) [#9]
'Draw tiles 32*32 on a 640*480 screen
For loopy = 0 to MAP_HEIGHT-1 'We're MAP_HEIGHT = 15
   For loopx = 0 to MAP_WIDTH-1 'We're MAP_WIDTH = 20
      DrawImage Tiles, loopx Shl 5, loopy shl 5, map[loopx,loopy]
   'Get screen coordinate of Tile marked 2
   if map[loopx,loopy] = 2
      TileX = loopx shl 5
      TileY = loopy shl 5
   end if
   Next
Next


And thats it! Bewarned, People dont like Bitshifting for some strange reason, I do though!

Dabz


Czar Flavius(Posted 2007) [#10]
Give this a spin.




Amon(Posted 2007) [#11]
Hi! Thanks for all the replies. I've made some modification from ideas I got from this thread and I have made some progress.

The code works but not flawlessly. I get stuck in between tiles when jumpin ie. i land in the middle of them.

I'm so close to getting this sorted but need a bit more help from you guys.

I've gone through the code and commented it as much as I could so that people can understand what I'm trying to do. Hopefully this will end up as a little template for people to figure out how to make platformers from.

More help is ofcourse appreciated. :)




[edit 1] The art needed to get this example working can be download here. http://www.kamikazekrow.com/storage/PlatArt.zip

[edit 2] to put code in a scrollable box use the codebox tag. it's the same as [ code ] but [ codebox ].


Czar Flavius(Posted 2007) [#12]
Could you put up the image files, or make it so the program is not dependant upon external files? Thanks.


Amon(Posted 2007) [#13]
The files can be downloaded here.

http://www.kamikazekrow.com/storage/PlatArt.zip

:)


Amon(Posted 2007) [#14]
bump! :)

If I'm going about it wrong then please let me know. Currently it works ok but you can get stuck in the middle of a tile on it's y axis when jumping.

Are there better methods to platform collsision?


tonyg(Posted 2007) [#15]
I'll have a go at the code later.
Couple of things.
1) You're checking for collision with a tile regardless of the height of the player's jump.
e.g. If the player end his jump and begin to fall at Y=48 it will be half way intoa tile and get stuck.
You should only be checking for the collision when Y is 'tile' boundary.
2) You're fall speed means you sometimes fall through a tile. If you collide you need to check if your previous position would also have collided
e.g. Y=Y+3. If Y+3 is 2 pixels into a tile you need to check Y+2 and Y+1.
.
As for other types of collision you might want to add all the tiles to a collision layer and test the players feet against that collision layer.
If it is a single screen platformer you could consider a single screensized image which you don't have to display but contains the upper most part of any platform (e.g. a 32*1 rect). This would simplify the collision testing although it means you have to check each position.
Hope it helps.
P.S. There must be example paltform code somewhere. Didn't Scott Shaver do some? If not, B2D/B3D code will be similar and the JumpAround source is out there somewhere.


Amon(Posted 2007) [#16]
Have you had a look at the code yet, tonyg?

I tried to implement what you said but kept getting index out of bounds errors.


tonyg(Posted 2007) [#17]
Sorry Amon, other things have taken over. I will try again later but can't promise anything.


Dabz(Posted 2007) [#18]
I love making platformers... I use bit-shifting (Non of that 2*32 pap) or pixel checking for collisions, as it works for me! :)

I think we went over this at BlitzCoder Amon didnt we? Well, in BlitzBasic anyway! :)

I havent made a full platformer in Max as of yet, but here is a conversion of a Blitz+ one I did yonks ago:-

http://www.syntaxbomb.com/ftp/dabz/Dizzy.zip

*Note: Not the tidiest of Max code in there mind, and the media is a little disorganised, but its easy to see whats going on with collisions.

It shows pixel checking collisions, up/down hills, jumping up and banging your head... No real left/right checking as I do that with if map[(x Shr 5)-1,y shr 5) = wall

I like pixel checking up/down as it frees you from blocky graphics, as well as you can disguise normal objects as bonus ones... etc etc

Anyway, thats my 10p, have fun! :)

Dabz


Jesse(Posted 2007) [#19]
Here Amon, see if this helps you. This is your code only the changes added:




Amon(Posted 2007) [#20]
Jesse, DUDE :) You just fixed my platform code worries. :)

I don't fully understand why it works and if you could explain the workings of it I would appreciate it. :)

What i gather from it is that PlayerY mod 32 returns the remainder of PlayerY being divided by 32. And if it is <= 6.4 which is the top of the tile it detects a collision.

If I'm wrong could you explain it please.

Thanks again dude. :)

@dabz

Hey, that bitshifting looks cool. I don't understand it but it looks cool. :)


Jesse(Posted 2007) [#21]
You are right.
and for the rest(its true when you are less than 6.4 into the tile):
lets assume you are falling to a tile, the tile is at y = 128. you are at position 127, you have to add 3.2 to go to the next position. if you add 3.2 you get y = 130.2 that is below the top of the block. to find out how much you are of the top of the block you get the remainder of division by 32(size of tile) wich in this case will give you 2.2. you subtract 2.2 from where you are and will give you the top of the tile wich is 128. ;)

hope you understand this. I don't thik I am the best at explaining it. maybe somebody can explain it to you better.

I have never made a game like this but I have little experience manipulating tiles. maybe one of this days I will have time to start one(work, you know).


MGE(Posted 2007) [#22]
@Dabz - Your getpixel routine is very interesting! I don't know if you could use it for large scrolling worlds, where sprites are moving around on the virtual world outside the area that is rendered, but I like your concept, I may do something with that at some point. Very cool! Thanks for sharing the concept! :)