Tiles and collision

Blitz3D Forums/Blitz3D Beginners Area/Tiles and collision

Hi
I found another great example of tile collision/slideing
on my harddisk i think you can use.
It is made by "Norc" (i think he use another nickname now)
Bye
Henrik
;Demonstration of very simple Non-Image-Collision-based "Wall-Detection"

Graphics 640,480,16;,2
nux#=0
oldx#=0
oldy#=0
x#=100
y#=100
a#=0

Dim tiles(15,15) ; Read the Wall information
For j=0 To 15
For i=0 To 15
Read tiles(i,j)
Next
Next

; MAINLOOP
While KeyHit(1)=0
Cls

For j=0 To 15 ; draw all walls for test purposes
For i=0 To 15 ; use your favorite Picture instead
If tiles(i,j)=1 Then
Color 120,120,120
Rect i*40,j*30,40,30,0
EndIf
Next
Next

oldx=x ; remember old player pos
oldy=y

; Keys
If KeyDown(200)=1 ; fwd
x=x+Sin(a)
y=y+Cos(a)
EndIf
If KeyDown(208)=1 ; back
x=x-Sin(a)
y=y-Cos(a)
EndIf
If KeyDown(203)=1 ; Player angle <
a=a+2
If a> 360 Then a=a-360
EndIf
If KeyDown(205)=1 ; Player angle >
a=a-2
If a< 0 Then a=a+360
EndIf

; dont allow walking tru walls, but allow sliding along
If tiles(((x-20)/40),((y-15)/30))=1 Then ; try new x, new y
nux#=x ; remeber nu x
x=oldx ; reset x to old x
EndIf
If tiles(((x-20)/40),((y-15)/30))=1 Then ; try old x, new y
x=nux#
y=oldy
EndIf
If tiles(((x-20)/40),((y-15)/30))=1 Then ; try new x, old y
x=oldx ; no, no chance, reset x and y both to old pos
y=oldy
EndIf

Color 255,0,0 ; Show a graphical Player
Oval x-5,y-5,10,10,1
Color 255,255,255
; show also wich direction Player is facing
Line x+Sin(a)*10,y+Cos(a)*10,x-Sin(a)*10,y-Cos(a)*10

Flip
Wend
End

; Walls = "1", Air = "0" 
; you can also use bigger levels - just keep 
; the relation (ie. 640/16=40) to check the Collision
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,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