More functions for you to play with. I'll post more info in a second.
[edit]
Ok, here are the functions:
MakeMatrix2D() - Make's a matrix
DrawMatrix2D() - Draws it
RandomizeMatrix2D() - Duh
SmoothMatrix2D() - Smooths it
SetMatrixHeight2D() - Set's the height of a tile
LockTile2D() - Tiles value can't be changed by smoothing etc.
UnlockTile2D() - The opposite of the above
GetGroundHeight2D() - Gets the height of matrix at any x position
GetMatrixHeight2D() - Gets the height of a tile
GetTileGradient2D() - Gets the gradient of a tile
GetTileNumber2D() - Finds what tile you are on at any x position
GetTileAngle2D() - Gets the angle of a selected tile
GetTileHardness2D() - Finds if a tile is locked or not.
No texturing here, not entirely sure how that would be done in 2D heh. Anyway, enjoy.
Here's a simple demo just to show how it works:
`Start
Sync on : sync rate 30
Set Display Mode 1024,768,16 : Hide Mouse
`make matrix
MakeMatrix2D(1,4000,100)
RandomizeMatrix2D(1,300)
SmoothMatrix2D(1,6)
`**Main Loop**
Do
`Draw Matrix
ink rgb(255,255,255),0
DrawMatrix2D(1,ccx)
DrawMatrixNormals2D(1)
if spacekey()=1 then SmoothMatrix2D(1,1)
If Returnkey()=1 then RandomizeMatrix2D(1,300)
`Move Camera
ccx=ccx-(Mousemovex()*2)
cx=screen width()/2
`Change Height
if upkey()=1
SetMatrixHeight2D(1,GetTileNumber2D(1,cx),GetMatrixHeight2D(1,cx)-3)
endif
if downkey()=1
SetMatrixHeight2D(1,GetTileNumber2D(1,cx),GetMatrixHeight2D(1,cx)+3)
endif
`Lock Tiles
If Shiftkey()=1 then LockTile2D(1,GetTileNumber2D(1,cx))
If Controlkey()=1 then UnlockTile2D(1,GetTileNumber2D(1,cx))
`Draw Guy
px#=cx : py#=GetGroundHeight2D(1,cx)
ink rgb(255,255,0),0
circle px#,py#,5
`User Info
ink rgb(255,255,255),0
Text 10,10,"Use Mouse to Move"
Text 10,30,"Use up/Down Arrow Keys to Change Height of Current Tile"
Text 10,50,"Press Space to Smooth Matrix"
Text 10,70,"Press Enter to Randomize Matrix"
Text 10,90,"Press Shift/Control To Lock/Unlock a Tile"
Text 10,200,"Tile Locked: "+str$(GetTileHardness2D(1,GetTileNumber2D(1,cx)))
Text 10,230,"Tile Angle: "+str$(GetTileAngle2D(1,GetTileNumber2D(1,cx)))
`**End Loop**
Sync
Cls
Loop
`**Functions**
Function MakeMatrix2D(mat,width,Tiles)
Dim Mat2DTiles(Mat) : Mat2DTiles(Mat)=Tiles
Dim Mat2DScroll(Mat)
Dim Mat2DW(Mat) : Mat2DW(mat)=width
Dim Mat2DTileW(Mat) as float : Mat2DTileW(Mat)=Width/Tiles
Dim Mat2DX(Mat,Tiles) as float
Dim Mat2DY(Mat,Tiles) as float
Dim Mat2dL(Mat,Tiles) as boolean
For T=0 to Tiles
Mat2DX(Mat,T)=T*Mat2DTileW(Mat)
Mat2DY(Mat,T)=Screen Height()/2
Next T
Endfunction
Function DrawMatrix2D(mat,along)
Mat2DScroll(Mat)=along
For T=1 to Mat2DTiles(mat)
Line Mat2DX(Mat,T-1)+along,Mat2DY(Mat,T-1),Mat2DX(Mat,T)+along,Mat2DY(Mat,T)
Next T
Line Mat2DX(Mat,0)+along,Mat2DY(Mat,0),Mat2DX(Mat,0)+along,Screen Height()
Line Mat2DX(Mat,Mat2DTiles(Mat))+along,Mat2DY(Mat,Mat2DTiles(Mat)),Mat2DX(Mat,Mat2DTiles(Mat))+along,Screen Height()
Endfunction
Function RandomizeMatrix2D(mat,value)
For T=0 to Mat2DTiles(mat)
if Mat2DL(mat,t)=0
Mat2DY(mat,T)=(Screen Height()/2)+Rnd(value)
endif
Next T
Endfunction
Function SmoothMatrix2D(mat,amount)
For A=1 to Amount
Dim TH1(Mat2DTiles(mat)-1) : Dim TH2(Mat2DTiles(mat)-1)
For T=1 to Mat2DTiles(mat)-1
if Mat2DL(mat,t)=0
TH1(T)=Mat2DY(Mat,T-1)
TH2(T)=Mat2DY(Mat,T+1)
Mat2DY(Mat,T)=((TH1(T)+TH2(T))/2)
endif
next t
Undim TH1() : Undim TH2()
next A
Endfunction
Function SetMatrixHeight2D(mat,x,height)
if Mat2DL(mat,x)=0
Mat2DY(mat,x)=height
endif
Endfunction
Function LockTile2D(mat,x)
Mat2DL(mat,x)=1
Endfunction
Function UnlockTile2D(mat,x)
Mat2DL(mat,x)=0
Endfunction
Function GetGroundHeight2D(mat,x)
local MT : local Y as float
local YC : local XD
Local Grad as float
For T=1 to Mat2DTiles(Mat)
If X>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and X<Mat2DX(Mat,T)+Mat2DScroll(Mat)
on=1
else
on=0
endif
if on=1
MT=T : XD=X-(Mat2DX(Mat,MT-1)+Mat2DScroll(Mat))
TH1=Mat2DY(Mat,MT-1) : TH2=Mat2DY(Mat,MT)
YC=TH2-TH1
Grad=YC/Mat2DTileW(Mat)
Y=int((Grad*XD)+Mat2DY(Mat,MT-1))
else
If (X-1)>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and (X-1)<Mat2DX(Mat,T)+Mat2DScroll(Mat)
non=1
else
non=0
endif
Endif
if non=1
MT=T : XD=X-(Mat2DX(Mat,MT-1)+Mat2DScroll(Mat))
TH1=Mat2DY(Mat,MT-1) : TH2=Mat2DY(Mat,MT)
YC=TH2-TH1
Grad=YC/Mat2DTileW(Mat)
Y=(Grad*XD)+Mat2DY(Mat,MT-1)
endif
Next T
Endfunction Y
Function GetMatrixHeight2D(mat,x)
t=GetTileNumber2D(mat,x)
height=Mat2DY(mat,t)
Endfunction height
Function GetTileNumber2D(mat,x)
For T=1 to Mat2DTiles(Mat)
If X>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and X<Mat2DX(Mat,T)+Mat2DScroll(Mat)
on=1
else
on=0
Endif
if on=1
MT=T
else
If (X-1)>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and (X-1)<Mat2DX(Mat,T)+Mat2DScroll(Mat)
MT=T
Endif
Endif
Next T
Endfunction MT
Function GetTileAngle2D(mat,T)
y1=Mat2DY(Mat,T-1)
y2=Mat2DY(Mat,T)
ang#=atanfull(Mat2DTileW(mat),y1-y2)
endfunction ang#
Function GetTileNormalX2D(mat,T)
n#=sin(GetTileAngle2D(mat,T))
endfunction n#
Function GetTileNormalY2D(mat,T)
n#=cos(GetTileAngle2D(mat,T))
endfunction n#
function DrawMatrixNormals2D(mat)
for t=1 to Mat2DTiles(Mat)
x=Mat2DX(Mat,t)-(Mat2DTileW(Mat)/2)+Mat2DScroll(Mat)+(cos(GetTileAngle2D(mat,T))*10)
line x,GetGroundHeight2D(Mat,x),x,GetGroundHeight2D(Mat,x)-(sin(GetTileAngle2D(mat,T))*10)
next t
endfunction
Function GetTileHardness2D(mat,x)
h=Mat2DL(mat,x)
Endfunction h
And here's another demo that uses physics and stuff. The jittery-ness has nothing to do with the functions, just slightly sloppy coding, aha.
`init
sync on : sync rate 30
set display mode 1024,768,16 : hide mouse
`make matrix
MakeMatrix2D(1,4000,100)
RandomizeMatrix2D(1,400)
SmoothMatrix2D(1,2)
`player
x#=500
y#=0
xvel#=0
yvel#=0
g#=0.5
wd#=5.0
wr#=2.0
`**Main Loop**
do
`Controls and Physics
if leftkey() and xvel#>-8.0 then dec xvel#,1.0
if rightkey() and xvel#<8.0 then inc xvel#,1.0
xvel#=xvel#*0.98
ht#=GetGroundHeight2D(1,x#)
if y#>ht#
yvel#=abs((GetTileGradient2D(1,GetTileNumber2D(1,x#))*xvel#)/3)
y#=curvevalue(ht#,y#,1.0)
ong=1
else
yvel#=yvel#-g#
ong=0
endif
y#=y#-yvel#
x#=x#+xvel#
ch1#=GetGroundHeight2D(1,x#-1)
ch2#=GetGroundHeight2D(1,x#+1)
frc#=(ch2#-ch1#)
x#=x#+frc#
if ong
ang#=-GetTileAngle2D(1,GetTileNumber2D(1,x#))
endif
w1x#=(x#+(sin(ang#)*wd#))+(sin(ang#)*wr#) : w1y#=(y#+(cos(ang#)*wd#))+(cos(ang#)*wr#)
w2x#=(x#-(sin(ang#)*wd#))+(sin(ang#)*wr#) : w2y#=(y#-(cos(ang#)*wd#))+(cos(ang#)*wr#)
circle w1x#,w1y#,wr# : circle w2x#,w2y#,wr#
line w1x#,w1y#,w2x#,w2y#
`Draw Matrix
DrawMatrix2D(1,0)
`**End Loop**
sync
cls
loop
`**Functions**
function MakeMatrix2D(mat,width,Tiles)
dim Mat2DTiles(Mat) : Mat2DTiles(Mat)=Tiles
dim Mat2DScroll(Mat)
dim Mat2DW(Mat) : Mat2DW(mat)=width
dim Mat2DTileW(Mat) as float : Mat2DTileW(Mat)=Width/Tiles
dim Mat2DX(Mat,Tiles) as float
dim Mat2DY(Mat,Tiles) as float
dim Mat2dL(Mat,Tiles) as boolean
for T=0 to Tiles
Mat2DX(Mat,T)=T*Mat2DTileW(Mat)
Mat2DY(Mat,T)=screen height()/2
next T
endfunction
function DrawMatrix2D(mat,along)
Mat2DScroll(Mat)=-along
for T=1 to Mat2DTiles(mat)
line Mat2DX(Mat,T-1)+along,Mat2DY(Mat,T-1),Mat2DX(Mat,T)+along,Mat2DY(Mat,T)
next T
line Mat2DX(Mat,0)+along,Mat2DY(Mat,0),Mat2DX(Mat,0)+along,screen height()
line Mat2DX(Mat,Mat2DTiles(Mat))+along,Mat2DY(Mat,Mat2DTiles(Mat)),Mat2DX(Mat,Mat2DTiles(Mat))+along,screen height()
endfunction
function RandomizeMatrix2D(mat,value)
for T=0 to Mat2DTiles(mat)
if Mat2DL(mat,t)=0
Mat2DY(mat,T)=(screen height()/2)+rnd(value)
endif
next T
endfunction
function SmoothMatrix2D(mat,amount)
for A=1 to amount
dim TH1(Mat2DTiles(mat)-1) : dim TH2(Mat2DTiles(mat)-1)
for T=1 to Mat2DTiles(mat)-1
if Mat2DL(mat,t)=0
TH1(T)=Mat2DY(Mat,T-1)
TH2(T)=Mat2DY(Mat,T+1)
Mat2DY(Mat,T)=((TH1(T)+TH2(T))/2)
endif
next t
undim TH1() : undim TH2()
next A
endfunction
function SetMatrixHeight2D(mat,x,height)
if Mat2DL(mat,x)=0
Mat2DY(mat,x)=height
endif
endfunction
function LockTile2D(mat,x)
Mat2DL(mat,x)=1
endfunction
function UnlockTile2D(mat,x)
Mat2DL(mat,x)=0
endfunction
function GetGroundHeight2D(mat,x)
local MT : local Y as float
local YC : local XD
Local Grad as float
for T=1 to Mat2DTiles(Mat)
if X>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and X<Mat2DX(Mat,T)+Mat2DScroll(Mat)
on=1
else
on=0
endif
if on=1
MT=T : XD=X-(Mat2DX(Mat,MT-1)+Mat2DScroll(Mat))
TH1=Mat2DY(Mat,MT-1) : TH2=Mat2DY(Mat,MT)
YC=TH2-TH1
Grad=YC/Mat2DTileW(Mat)
Y=int((Grad*XD)+Mat2DY(Mat,MT-1))
else
If (X-1)>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and (X-1)<Mat2DX(Mat,T)+Mat2DScroll(Mat)
non=1
else
non=0
endif
endif
if non=1
MT=T : XD=X-(Mat2DX(Mat,MT-1)+Mat2DScroll(Mat))
TH1=Mat2DY(Mat,MT-1) : TH2=Mat2DY(Mat,MT)
YC=TH2-TH1
Grad=YC/Mat2DTileW(Mat)
Y=(Grad*XD)+Mat2DY(Mat,MT-1)
endif
next T
endfunction Y
function GetMatrixHeight2D(mat,x)
t=GetTileNumber2D(mat,x)
height=Mat2DY(mat,t)
endfunction height
function GetTileGradient2D(mat,t)
local grad as float
grad=(Mat2DY(mat,t-1)-Mat2DY(mat,t))/Mat2DTileW(mat)
endfunction grad
function GetTileNumber2D(mat,x)
for T=1 to Mat2DTiles(Mat)
if X>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and X<Mat2DX(Mat,T)+Mat2DScroll(Mat)
on=1
else
on=0
endif
if on=1
MT=T
else
if (X-1)>Mat2DX(Mat,T-1)+Mat2DScroll(Mat) and (X-1)<Mat2DX(Mat,T)+Mat2DScroll(Mat)
MT=T
endif
endif
next T
endfunction MT
function GetTileAngle2D(mat,T)
y1=Mat2DY(Mat,T-1)
y2=Mat2DY(Mat,T)
ang#=atanfull(Mat2DTileW(mat),y1-y2)
endfunction ang#
function GetTileHardness2D(mat,x)
h=Mat2DL(mat,x)
endfunction h
No media is needed.