Basic Tile Collision

Blitz3D Forums/Blitz3D Programming/Basic Tile Collision

LamptonWorm(Posted 2008) [#1]
Hi all,

I've been looking through the code archives but haven't found a working or simple example (or one without broken/old links) of tile collision.

I'm after something like this... http://jnrdev.72dpiarmy.com/en/jnrdev1/ except in Blitz using 2D to help me understand/build my own routine, any ideas?

Cheers,
LW.


Tobo(Posted 2008) [#2]
I have one that shows debugging etc.

I'm at work at the mo, but I'll dig it out when I get the chance.

T


Kryzon(Posted 2008) [#3]
Dunno why this crappy forum code for links don't work. Well anyways, copy and paste this on your adress bar:

http://web.archive.org/web/20050306221202/http://www.blitzcoder.com/articles.shtml

Then search on the "Blitz Basic Newbies" section, there's lots of tile\maps\collision\platform related stuff there. It's a great learning place (too bad it's dead and we gotta use Web.Archives to see it =/ )


Tobo(Posted 2008) [#4]
Rafael's link is better documented than this.

In fact this following code was adapted from some guy's on Blitzcoder - I couldn't get my head round his, so I kind of taught myself through his - if that makes sense.

If you want me to comment it, I might.

Graphics 640,480,32,2
SetBuffer BackBuffer()

Dim map(19,19)

Global px=20
Global py=20
Global gpx,gpy

readmap()

While Not KeyHit(1)
    Cls

    drawmap()
    checkmove()
    drawplayer()
    showstats()

    Flip
Wend
End


; FUNCTIONS


; Player Map Collision
Function playermapcollision()
    For x=-1 To 1
        For y=-1 To 1
            If map(gpx+x,gpy+y)=1
                If RectsOverlap (px,py,20,20,((px/20)*20)+x*20,((py/20)*20)+y*20,20,20) Then Return True
            End If
        Next
    Next
End Function

; Show Stats
Function showstats()
    Text 430,10,"Grid Ref: "+gpx+","+gpy
   
    Text 430,30,map(gpx-1,gpy-1)+" "+map(gpx,gpy-1)+" "+map(gpx+1,gpy-1)
    Text 430,40,map(gpx-1,gpy)+" "+map(gpx,gpy)+" "+map(gpx+1,gpy)
    Text 430,50,map(gpx-1,gpy+1)+" "+map(gpx,gpy+1)+" "+map(gpx+1,gpy+1)

End Function

; Check Move
Function checkmove()
   
    ox=px
    oy=py
   
    If KeyDown(205)
        px=px+2
    End If
   
    If KeyDown(203)
        px=px-2
    End If
   
    If KeyDown(200)
        py=py-2
    End If
   
    If KeyDown(208)
        py=py+2
    End If
   
    If px>360 Or px<20 Then px=ox
    If py>360 Or py<20 Then py=oy
   
    gpx=px/20
    gpy=py/20
   
    If playermapcollision() = True Then px=ox:py=oy
   
End Function

; Draw Player
Function drawplayer()
    Rect px,py,20,20
End Function

; Draw Map
Function drawmap()
    For ac=0 To 19
        For dn=0 To 19
            Color 0,0,255
            If map(ac,dn)=1 Then Rect ac*20,dn*20,20,20
            Color 0,255,0
            Line ac*20,0,ac*20,400
            Line 0,dn*20,400,dn*20
            Color 255,255,255
        Next
    Next
End Function

; Read Map
Function readmap()
    Restore level1
    For dn=0 To 19
        For ac=0 To 19
            Read a
            map(ac,dn)=a
        Next
    Next
End Function

.level1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1



Tobo(Posted 2008) [#5]
Actually, this one is better for debugging. I posted the wrong one.

Graphics 640,480,32,2
SetBuffer BackBuffer()

Dim map(19,19)

Global px=20
Global py=20
Global gpx,gpy
Global touch=False


readmap()

While Not KeyHit(1)
	Cls

	drawmap()
	checkmove()
	drawsurround()
	drawplayer()
	showstats()

	Flip
Wend
End


; FUNCTIONS

; Draw Surround
Function drawsurround()
	For x=-1 To 1
		For y=-1 To 1
			If map(gpx+x,gpy+y)=1
				Color 255,255,0
				If RectsOverlap (px,py,20,20,((px/20)*20)+x*20,((py/20)*20)+y*20,20,20)
					touch =True
				Else
					touch =False
				EndIf
				Rect ((px/20)*20)+x*20,((py/20)*20)+y*20,20,20,touch
				Color 255,255,255
			End If
		Next
	Next
End Function

; Show Stats
Function showstats()
	Text 430,10,"Grid Ref: "+gpx+","+gpy
	
	Text 430,30,map(gpx-1,gpy-1)+" "+map(gpx,gpy-1)+" "+map(gpx+1,gpy-1)
	Text 430,40,map(gpx-1,gpy)+" "+map(gpx,gpy)+" "+map(gpx+1,gpy)
	Text 430,50,map(gpx-1,gpy+1)+" "+map(gpx,gpy+1)+" "+map(gpx+1,gpy+1)

End Function

; Check Move
Function checkmove()
	
	ox=px
	oy=py
	
	If KeyDown(205)
		px=px+2
	End If
	
	If KeyDown(203)
		px=px-2
	End If
	
	If KeyDown(200)
		py=py-2
	End If
	
	If KeyDown(208)
		py=py+2
	End If
	
	If px>360 Or px<20 Then px=ox
	If py>360 Or py<20 Then py=oy
	
	gpx=px/20
	gpy=py/20
	
End Function

; Draw Player
Function drawplayer()
	Rect px,py,20,20
End Function

; Draw Map
Function drawmap()
	For ac=0 To 19
		For dn=0 To 19
			Color 0,0,255
			If map(ac,dn)=1 Then Rect ac*20,dn*20,20,20
			Color 0,255,0
			Line ac*20,0,ac*20,400
			Line 0,dn*20,400,dn*20
			Color 255,255,255
		Next
	Next
End Function

; Read Map
Function readmap()
	Restore level1
	For dn=0 To 19
		For ac=0 To 19
			Read a
			map(ac,dn)=a
		Next
	Next
End Function

.level1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1



Kryzon(Posted 2008) [#6]
Yeah I never quite got all that familiar with 2D collision. 3D is just too damn easy (just set up the commands and radii, if necessary, and voilą).

A method I thought of was the same that Metal Slug uses. You'd have a huge level image (I mean, really really big width-wise), separated into smaller pieces so that you don't draw everthing always (kinda like how "polygon clipping works", you don't show what you couldn't see if it was rendered). Then you define in a level editor of your making the Guidelines for that level. They are nothing but lines that define the "ground" of that level. Meaning you wouldn't have to use tiles to make your level - you could draw it however you wanted it to be, with ground distortion and shit. Then use a mathematical function to define the players position at that spot in the Guideline(perhaps something using Pithagoras, I can't recall right now).


LamptonWorm(Posted 2009) [#7]
Hi Tobo,

Sorry for the late reply, but still after this :)

I tried the snippets, the first 2nd one only slides on the outside border and passes through the other blocks, and the 1st snippet slides on the outside border but sticks to the blocks on the inside.

Cheers,
LW.


Warner(Posted 2009) [#8]
Maybe you can look at my code archive entry here?
http://www.blitzmax.com/codearcs/codearcs.php?code=2374


LamptonWorm(Posted 2009) [#9]
Hi, thanks, that is pretty cool, but the ball sticks to the edges of the cubes where I need it all slidey - I'm basically writing a simple FPS where the world is created from blocks, created from a text file / array. Using blitz col boxes works for now, until I use a bigger map then it slows down, so if I can nail basic math sliding box/tile collision I'll be sorted :) if we can tweak Tobo's code so it has sliding response that'll do the trick nicely!

Cheers,
LW.