I posted this in a thread over
here . I figure it'll be easier to find here, and maybe some of you other guru's can expand on it.
Press space to drop the bombs and use the arrowkeys to move around. The size of the craters is currently dependent on the sphere size which is randomized.
You can also click with your mouse to get information and manually lower the tiles. This can demonstrate how to make a mini matrix editor for those interested.
`Destructable matrix example - Bombs away!
`Written by MikeS 2/18/07
`
`Use arrowkeys to move around, and spacekey to drop bombs
`Click on a cube to move the terrain by yourself
`This sample can also be used to demonstrate how to make a basic matrix editor.
Sync on : Sync rate 60
Set display mode 1024,768,32
autocam off
`generate a random image for the matrix
lock pixels
for x = 1 to 32
for y = 1 to 32
dot x,y,rgb(rnd(128),rnd(128),0)
next x
next y
unlock pixels
get image 1,1,1,32,32
`Are Variables for the matrix
Size=200
Segments=20
Unit#=Size/Segments
`make a matrix, smoooth it, and calculates the normals
make matrix 1,Size,Size,Segments,Segments
prepare matrix texture 1,1,1,1
CalculateNormals(1,Size,Segments)
randomize matrix 1,40
smooth_matrix(1,Segments,Segments,2)
update matrix 1
`Make a grid of objects
Objects=1
modifier=0
for x = 1 to Segments
for z = 1 to Segments+2
make object cube Objects,1
position object Objects,Unit#*modify,0,Unit#*modifier
modify=modify+1
if modify>Segments then modifier=modifier+1:modify=0
inc Objects,1
next x
next z
Bombs=0
Do
`positions the camera 20 units above the terrain
position camera camera position x(),get ground height(1,camera position x(),camera position z())+20,camera position z()
control camera using arrowkeys 0,1,1
`picks an object.
p=pick object(mousex(),mousey(),1,Objects)
`little bit of info to display the tile number you've last selected, and its height.
text 0,0,str$(TileX)+","+str$(TileZ)+"="+str$(height#)
`show fps and polygons being displayed
text 0,20,str$(statistic(1))+" "+str$(screen fps())
`Pick a matrix tile and it will change. Normals are also recalculated.
`Lock object is used to ensure that an object that exists will shift.
if p>0 and mouseclick()=1
lockobject=p
if object exist(lockobject)
x1#=object position x(lockobject)
z1#=object position z(lockobject)
TileX=x1#/Unit#
TileZ=z1#/Unit#
height#=get ground height(1,x1#,z1#)
set matrix height 1,TileX,TileZ,height#-1
CalculateNormals(1,Size,Segments)
update matrix 1
endif
endif
`Drops the bombs in a random spot ont he matrix, from a random height
if spacekey()=1
Bombs=Bombs+1
make object sphere 5000+bombs,1
position object 5000+bombs,rnd(Size),50+rnd(25),rnd(Size)
endif
`Here is where all the bombs are dropped. Whichever tile they are dropped closest too, is where
`the tile is dropped 5 units. This is done by dividing the unit number(size of each tile) from the position of the object.
`This will then leave in integer which will correspond to the matrix tile that will change.
`The normals are also recalculated each time the matrix changes.
for x = 5000 to 5000+bombs
if object exist(x)
if object position y(x)>get ground height(1,object position x(x),object position z(x))
move object down x,1
endif
if object position y(x)<=get ground height(1,object position x(x),object position z(x))
ObjectTileX=object position x(x)/Unit#
ObjectTileZ=object position z(x)/Unit#
set matrix height 1,ObjectTileX,ObjectTileZ,get ground height(1,ObjectTileX,ObjectTileZ)-5
update matrix 1
scale object x,500,500,500
delete object x
CalculateNormals(1,Size,Segments)
endif
endif
next x
`Reposition objects(the cubes) based on height. The cubes are purely for the users viewing to help determine height.
for x = 1 to Objects-1
height#=get ground height(1,object position x(x),object position z(x))
position object x,object position x(x),height#,object position z(x)
next x
Sync
Loop
`Matrix normals calculations courtesy of Lee, modified by Yarbles and MikeS
Function CalculateNormals(MatNum,Size,Segments)
Dim MatHeight#(Segments,Segments)
For X = 1 To segments - 1
For Z = 1 To segments -1
MatHeight#(X,Z) = Get Matrix Height(MatNum,X,Z)
Next Z
Next X
for x=1 to Segments-1
for z=1 to Segments-1
h8#=MatHeight#(x,z-1)
h4#=MatHeight#(x-1,z)
h1#=MatHeight#(x,z)
y1#=h1#
y2#=h4#
dx#=stepX#/10
dy#=y2#-y1#
ax#=atanfull(dx#,dy#)
ax#=wrapvalue(90-ax#)
y1#=h1#
y2#=h8#
dz#=stepZ#/10
dy#=y2#-y1#
az#=atanfull(dz#,dy#)
az#=wrapvalue(90-az#)
nx# = Sin(ax#)
ny# = Cos(ax#)
nz# = Sin(az#)
Set Matrix Normal MatNum,x,z,nx#,ny#,nz#
Next X
Next Z
update matrix MatNum
undim MatHeight#(0)
Endfunction
`Smooth matrix function courtesy of Red_EyE
function smooth_matrix(matnum,tilex,tilez,times)
for multiple=1 to times
for x=1 to tilex-1
for z=1 to tilez-1
a=get matrix height(matnum,x-1,z+1)
b=get matrix height(matnum,x,z+1)
c=get matrix height(matnum,x+1,z+1)
d=get matrix height(matnum,x+1,z)
e=get matrix height(matnum,x+1,z-1)
f=get matrix height(matnum,x,z-1)
g=get matrix height(matnum,x-1,z-1)
h=get matrix height(matnum,x-1,z)
total=a+b+c+d+e+f+g+h
av#=total/8
set matrix height matnum,x,z,av#
next z
next x
next multiple
for x=0 to tilex
set matrix height matnum,x,0,get matrix height(matnum,x,1)
next x
for x=0 to tilex
set matrix height matnum,x,tilez,get matrix height(matnum,x,tilez-1)
next x
for z=0 to tilez
set matrix height matnum,0,z,get matrix height(matnum,1,z)
next z
for z=0 to tilez
set matrix height matnum,tilex,z,get matrix height(matnum,tilex-1,z)
next z
update matrix matnum
endfunction
[Screen shot]
A book? I hate book. Book is stupid.
(Formerly Yellow)