Posted: 14th Feb 2003 1:10
Been playing with matrices and come up with following to demonstrate texturing, slope physics, e.t.c.

By playing with the variables at top of code, you can tailor program for different types of games.

I think it copes with nearly everything.

During programming though I found some combinations of matrix size produced weird ground height effects but haven't tracked problem down yet. I'll repost any mods.



+ Code Snippet
sync on : randomize timer() : autocam off : hide mouse

rem *** bitmap variables ***

rem below will create 4*4=16 random tile textures at 64*64 pixels
rem this is same as loading an image of 256*256 pixels cut up into 16 tiles

r=64     : rem texture resolution (32,64,128,256)
w=4      : rem tile width
h=4      : rem tile height

rem *** matrix variables ***

mw=2000  : rem units wide
mh=2000  : rem units high
mx=64    : rem number of tiles wide
mz=64    : rem number of tiles high

rem *** player variables ***

cx#=100  : rem start x position
cz#=100  : rem start z position
cya#=0   : rem start y angle
cxa#=0   : rem start x angle
ch=20    : rem height above ground of player's eyes

rem *** collision variables ***

s#=0.5   : rem step size
r#=10    : rem player radius
frc#=75  : rem force (bigger number means bigger slopes can be climbed)

rem *** player camera variables ***

mang#=80 : rem maximum angle you can look up or down
midx#=3  : rem smoothness of mouse camera (higher=smoother)

gosub setupbitmap
gosub setupmatrix
gosub setupwater

h#=get ground height(1,cx#,cz#)
position camera cx#,h#+ch,cz#
ink rgb(255,255,255),rgb(0,0,0)
water#=0
do

   x# = camera position x()
   y# = camera position y()
   z# = camera position z()

   if upkey()
      x#=newxvalue(x#,cya#,s#)
      z#=newzvalue(z#,cya#,s#)
   endif

   if downkey()
      x#=newxvalue(x#,cya#,-s#)
      z#=newzvalue(z#,cya#,-s#)
   endif

   if rightkey()
      x#=newxvalue(x#,wrapvalue(cya#+90.0),s#)
      z#=newzvalue(z#,wrapvalue(cya#+90.0),s#)
   endif

   if leftkey()
      x#=newxvalue(x#,wrapvalue(cya#-90.0),s#)
      z#=newzvalue(z#,wrapvalue(cya#-90.0),s#)
   endif

   rem *** sliding collision (start) ***

   fx#=0 : fz#=0
   for r=0 to 35
      ex#=newxvalue(x#,r*10.0,r#)
      ez#=newzvalue(z#,r*10.0,r#)
      eh#=get ground height(1,ex#,ez#)
      if eh#mang# then cxa#=mang#
   rotate camera wrapvalue(cxa#),cya#,0

   water#=wrapvalue(water#+0.5)
   position object 1,object position x(1),-10+(2*sin(water#)),object position z(1)

   sync
loop

end

rem ********************

setupbitmap:

create bitmap 1,w*r,h*r
b=0
for f=1 to h
   for g=1 to w
      ink rgb(b,b,b),rgb(0,0,0) : box (g-1)*r,(f-1)*r,g*r,f*r : b=rnd(255)
   next g
next f
for f=1 to (w*h*10)
   a1=rnd(w*r) : b1=rnd(h*r) : c1=rnd(20)+2 : d1=rnd(20)+2
   ink rgb(rnd(255),rnd(255),rnd(255)),rgb(0,0,0)
   box a1,b1,a1+c1,b1+d1
next f
get image 1,0,0,w*r,h*r
delete bitmap 1

return

rem ********************

setupmatrix:

make matrix 1,mw,mh,mx,mz
set matrix 1,0,0,1,1,1,1,1
prepare matrix texture 1,1,w,h
fill matrix 1,0,1
for f=1 to (mx-2)
   for g=1 to (mz-2)
      set matrix tile 1,f,g,rnd((w*h)-1)+1
   next g
next f
for f=0 to mx
   set matrix height 1,f,0,150+rnd(20)
   set matrix height 1,f,mz,150+rnd(20)
next f
for f=0 to mz
   set matrix height 1,0,f,150+rnd(20)
   set matrix height 1,mx,f,150+rnd(20)
next f
for f=1 to (mx*mz/5)
   set matrix height 1,(5+rnd(mx-10)),(5+rnd(mz-10)),rnd(100)
next f
for f=0 to mz-10
   set matrix height 1,1,f+5,f*2
   set matrix height 1,2,f+5,f*2
next f
for f=1 to 5
   for g=1 to 5
      set matrix height 1,f+7,g+2,-50
      set matrix height 1,8,g+7,-50
   next g
next f

update matrix 1

return

rem ********************

setupwater:

make object plain 1,mw,mh
rotate object 1,90,0,0
color object 1,rgb(0,0,255)
position object 1,mw/2,0,mh/2
ghost object on 1

return
Posted: 14th Feb 2003 1:14
Some lines did not post correctly!

+ Code Snippet
sync on : randomize timer() : autocam off : hide mouse

rem *** bitmap variables ***

rem below will create 4*4=16 random tile textures at 64*64 pixels
rem this is same as loading an image of 256*256 pixels cut up into 16 tiles

r=64     : rem texture resolution (32,64,128,256)
w=4      : rem tile width
h=4      : rem tile height

rem *** matrix variables ***

mw=2000  : rem units wide
mh=2000  : rem units high
mx=64    : rem number of tiles wide
mz=64    : rem number of tiles high

rem *** player variables ***

cx#=100  : rem start x position
cz#=100  : rem start z position
cya#=0   : rem start y angle
cxa#=0   : rem start x angle
ch=20    : rem height above ground of player's eyes

rem *** collision variables ***

s#=0.5   : rem step size
r#=10    : rem player radius
frc#=75  : rem force (bigger number means bigger slopes can be climbed)

rem *** player camera variables ***

mang#=80 : rem maximum angle you can look up or down
midx#=3  : rem smoothness of mouse camera (higher=smoother)

gosub setupbitmap
gosub setupmatrix
gosub setupwater

h#=get ground height(1,cx#,cz#)
position camera cx#,h#+ch,cz#
ink rgb(255,255,255),rgb(0,0,0)
water#=0
do

   x# = camera position x()
   y# = camera position y()
   z# = camera position z()

   if upkey()
      x#=newxvalue(x#,cya#,s#)
      z#=newzvalue(z#,cya#,s#)
   endif

   if downkey()
      x#=newxvalue(x#,cya#,-s#)
      z#=newzvalue(z#,cya#,-s#)
   endif

   if rightkey()
      x#=newxvalue(x#,wrapvalue(cya#+90.0),s#)
      z#=newzvalue(z#,wrapvalue(cya#+90.0),s#)
   endif

   if leftkey()
      x#=newxvalue(x#,wrapvalue(cya#-90.0),s#)
      z#=newzvalue(z#,wrapvalue(cya#-90.0),s#)
   endif

   rem *** sliding collision (start) ***

   fx#=0 : fz#=0
   for r=0 to 35
      ex#=newxvalue(x#,r*10.0,r#)
      ez#=newzvalue(z#,r*10.0,r#)
      eh#=get ground height(1,ex#,ez#)
      if eh#<0 then eh#=(-eh#*20)
      force#=eh#/frc#
      fx#=fx#+((ex#-x#)*force#)
      fz#=fz#+((ez#-z#)*force#)
   next r
   x#=x#-(fx#/35.0)
   z#=z#-(fz#/35.0)

   rem *** sliding collision (end) ***

   h#=get ground height(1,x#,z#)
   position camera x#,h#+ch,z#
   cya#=wrapvalue(cya#+(mousemovex()/midx#))
   cxa#=cxa#+(mousemovey()/midx#)
   if cxa#<-mang# then cxa#=-mang#
   if cxa#>mang# then cxa#=mang#
   rotate camera wrapvalue(cxa#),cya#,0

   water#=wrapvalue(water#+0.5)
   position object 1,object position x(1),-10+(2*sin(water#)),object position z(1)

   sync
loop

end

rem ********************

setupbitmap:

create bitmap 1,w*r,h*r
b=0
for f=1 to h
   for g=1 to w
      ink rgb(b,b,b),rgb(0,0,0) : box (g-1)*r,(f-1)*r,g*r,f*r : b=rnd(255)
   next g
next f
for f=1 to (w*h*10)
   a1=rnd(w*r) : b1=rnd(h*r) : c1=rnd(20)+2 : d1=rnd(20)+2
   ink rgb(rnd(255),rnd(255),rnd(255)),rgb(0,0,0)
   box a1,b1,a1+c1,b1+d1
next f
get image 1,0,0,w*r,h*r
delete bitmap 1

return

rem ********************

setupmatrix:

make matrix 1,mw,mh,mx,mz
set matrix 1,0,0,1,1,1,1,1
prepare matrix texture 1,1,w,h
fill matrix 1,0,1
for f=1 to (mx-2)
   for g=1 to (mz-2)
      set matrix tile 1,f,g,rnd((w*h)-1)+1
   next g
next f
for f=0 to mx
   set matrix height 1,f,0,150+rnd(20)
   set matrix height 1,f,mz,150+rnd(20)
next f
for f=0 to mz
   set matrix height 1,0,f,150+rnd(20)
   set matrix height 1,mx,f,150+rnd(20)
next f
for f=1 to (mx*mz/5)
   set matrix height 1,(5+rnd(mx-10)),(5+rnd(mz-10)),rnd(100)
next f
for f=0 to mz-10
   set matrix height 1,1,f+5,f*2
   set matrix height 1,2,f+5,f*2
next f
for f=1 to 5
   for g=1 to 5
      set matrix height 1,f+7,g+2,-50
      set matrix height 1,8,g+7,-50
   next g
next f

update matrix 1

return

rem ********************

setupwater:

make object plain 1,mw,mh
rotate object 1,90,0,0
color object 1,rgb(0,0,255)
position object 1,mw/2,0,mh/2
ghost object on 1

return
Posted: 14th Feb 2003 4:19
Thanks m8!! Thats a great code and it really helped me It only have to collide with 3D objects to
Wonderfull code sonic
Posted: 14th Feb 2003 9:23
nice code ...


Make it jump off-hills, now ! ^^
Posted: 14th Feb 2003 13:39
Things I'm going to do next:

1. Have option of not sliding down shallow slopes as this is not natural

2. Stick in some 3d objects, like cubes, or 'x' files and allow collision on those aswell

3. Trace DBPro bug where certain combinations of matrix sizes produce negative ground heights in certain locations.

4. Maybe do a jump routine as Kohai suggests, but this means writing some sort of gravity code. Shouldn't be too had though (fingers crossed)
Posted: 14th Feb 2003 22:05
yeeeeee!!!!! When you do it pleaseee tell us!!
Posted: 15th Feb 2003 0:58
Yeah got to jump off hills, I've got a bit of a project done with a car, I've got it done also with the wheels as separate objects to work on suspension for each wheel, also the wheels turn, I just need some way to make it jumpppp!!
Posted: 16th Feb 2003 0:48
Updated version. You now don't slide down shallow hills.
Sliding collision also on objects. They can even push you around!

Also stuck in a very basic rotating sky sphere.



+ Code Snippet
remstart

mouse to look around
up and down cursors = forward and back
left and right cursors = strafe

remend

sync on : randomize timer() : autocam off : hide mouse : set global collision on

rem *** bitmap variables ***

rem below will create 4*4=16 random tile textures at 64*64 pixels
rem this is same as loading an image of 256*256 pixels cut up into 16 tiles

f$=""     : rem filename of tiled textures (leave blank to create random one)
w=4       : rem number of tiles across
h=4       : rem number of tiles down
r=64      : rem texture resolution (32,64,128,256) (ignored if you specify f$)

rem *** matrix variables ***

mw=1000   : rem units wide
mh=1000   : rem units high
mx=32     : rem number of tiles wide
mz=32     : rem number of tiles high

rem *** player variables ***

cx#=100   : rem start x position
cz#=100   : rem start z position
cya#=0    : rem start y angle
cxa#=0    : rem start x angle
ch=20     : rem height above ground of player's eyes

rem *** collision variables ***

s#=0.5    : rem step size
r#=10     : rem player radius
frc#=100  : rem force (bigger number means bigger slopes can be climbed)
slide#=10 : rem how steep before autoslide
objcol#=30: rem object collision sensitivity

rem *** player camera variables ***

mangu#=50 : rem maximum angle you can look up
mangd#=80 : rem maximum angle you can look down
midx#=3   : rem smoothness of mouse camera (higher=smoother)

rem *** diagnostic variables ***

diag=0    : rem 0=normal, 1=show diag objects

gosub setupbitmap
gosub setupmatrix
gosub setupwater
gosub setupobjects
gosub setupsky

h#=get ground height(1,cx#,cz#) : position camera cx#,h#+ch,cz#

do

   x# = camera position x()
   y# = camera position y()
   z# = camera position z()

   if upkey()
      x#=newxvalue(x#,cya#,s#)
      z#=newzvalue(z#,cya#,s#)
   endif

   if downkey()
      x#=newxvalue(x#,cya#,-s#)
      z#=newzvalue(z#,cya#,-s#)
   endif

   if rightkey()
      x#=newxvalue(x#,wrapvalue(cya#+90.0),s#)
      z#=newzvalue(z#,wrapvalue(cya#+90.0),s#)
   endif

   if leftkey()
      x#=newxvalue(x#,wrapvalue(cya#-90.0),s#)
      z#=newzvalue(z#,wrapvalue(cya#-90.0),s#)
   endif

   fx#=0 : fz#=0
   hlow#=99999 : hhigh#=-99999
   for r=0 to 35
      ex#=newxvalue(x#,r*10.0,r#)
      ez#=newzvalue(z#,r*10.0,r#)
      eh#=get ground height(1,ex#,ez#)
      position object (r+100),ex#,eh#,ez#
      if object collision (r+100,0) > 0 then eh#=objcol#
      if eh# < 0 then eh#=(-eh#*2)
      if eh# > hhigh# then hhigh#=eh#
      if eh# < hlow# then hlow#=eh#
      force#=eh#/frc#
      fx#=fx#+((ex#-x#)*force#) : fz#=fz#+((ez#-z#)*force#)
   next r
   nx#=x#-(fx#/35.0) : nz#=z#-(fz#/35.0)
   if abs(hhigh#-hlow#) >= slide# then x#=nx# : z#=nz#
   h#=get ground height(1,x#,z#)
   position camera x#,h#+ch,z#
   cya#=wrapvalue(cya#+(mousemovex()/midx#))
   cxa#=cxa#+(mousemovey()/midx#)
   if cxa# < -mangu# then cxa#=-mangu#
   if cxa# > mangd# then cxa#=mangd#
   rotate camera wrapvalue(cxa#),cya#,0

   water#=wrapvalue(water#+0.5)
   position object 1,object position x(1),-10+(2*sin(water#)),object position z(1)
   yrotate object 13,wrapvalue(object angle y(13)+0.2)
   yrotate object 10,wrapvalue(object angle y(10)-0.1)
   yrotate object 12,wrapvalue(object angle y(12)-0.1)
   position object 500,camera position x(),camera position y()-50,camera position z()
   yrotate object 500,object angle y(500)+0.01
   position object 14,newxvalue(object position x(10), object angle y(10),50),-10,newzvalue(object position z(10), object angle y(10),50)
   yrotate object 14,object angle y(10)
   position object 15,newxvalue(object position x(10), object angle y(10),80),-10,newzvalue(object position z(10), object angle y(10),80)
   yrotate object 15,object angle y(10)
   sync
loop

end

rem ********************

setupbitmap:

if f$=""
   create bitmap 1,w*r,h*r
   b=0
   for f=1 to h
      for g=1 to w
         ink rgb(b,b,b),rgb(0,0,0) : box (g-1)*r,(f-1)*r,g*r,f*r : b=rnd(255)
      next g
   next f
   for f=1 to (w*h*10)
      a1=rnd(w*r) : b1=rnd(h*r) : c1=rnd(20)+2 : d1=rnd(20)+2
      ink rgb(rnd(255),rnd(255),rnd(255)),rgb(0,0,0)
      box a1,b1,a1+c1,b1+d1
   next f
   get image 1,0,0,w*r,h*r,1 : delete bitmap 1
else
   load image f$,1,1
endif
return

rem ********************

setupmatrix:

make matrix 1,mw,mh,mx,mz : set matrix 1,0,0,1,1,1,1,1
prepare matrix texture 1,1,w,h : fill matrix 1,0,1
for f=1 to (mx-2)
   for g=1 to (mz-2)
      set matrix tile 1,f,g,rnd((w*h)-1)+1
   next g
next f
for f=0 to mx
   set matrix height 1,f,0,150+rnd(20)
   set matrix height 1,f,mz,150+rnd(20)
next f
for f=0 to mz
   set matrix height 1,0,f,150+rnd(20)
   set matrix height 1,mx,f,150+rnd(20)
next f
for f=1 to (mx*mz/5)
   set matrix height 1,(10+rnd(mx-20)),(10+rnd(mz-20)),rnd(40)
next f
for f=0 to mz-10
   set matrix height 1,1,f+5,f*2 : set matrix height 1,2,f+5,f*2
next f
for f=1 to 5
   for g=1 to 5
      set matrix height 1,f+7,g+2,-50
   next g
next f
update matrix 1
return

rem ********************

setupwater:

make object plain 1,mw,mh
rotate object 1,90,0,0
color object 1,rgb(0,0,255)
position object 1,mw/2,0,mh/2
ghost object on 1
water#=0
return

rem ********************

setupobjects:

rem *** required objects (start) ***
for f=0 to 35
   make object sphere (f+100),0.3,5,5
   if diag=0 then hide object (f+100)
next f
rem *** required objects (end) ***

create bitmap 1,128,128
set text size 6
ink rgb(0,0,200),rgb(0,0,0)
box 0,0,128,128
ink rgb(255,255,255),rgb(0,0,0)
text 0,0,"You can walk"
text 0,20,"through this!"
get image 2,0,0,128,128,1 : delete bitmap 1

make object cube 10,50
color object 10,rgb(255,0,0)
position object 10,150,-10,200

make object cube 11,40
color object 11,rgb(0,255,0)
position object 11,80,20,300

make object box 12,100,100,5
position object 12,180,20,300
set object collision off 12
texture object 12,2

make object sphere 13,200
texture object 13,1
position object 13,220,0,500

make object box 14,10,50,50
color object 14,rgb(255,0,0)
make object box 15,40,50,10
color object 15,rgb(255,0,0)

return

rem ********************

setupsky:

make object sphere 500,-3000,20,20 : texture object 500,1
set object light 500,0
color object 500,rgb(0,0,255) : ghost object on 500
set object collision off 500
return
Posted: 16th Feb 2003 17:05
is it just me or is that code really low in the fps
Posted: 16th Feb 2003 17:11
ouch ouch ouch sonic, bad move

for r=0 to 35
(code)
if object collision(r+100,0)>0 then eh#=objcol#
(code)
next r

you do THIRTY FIVE COLLISIONS IN ONE LOOP! HOLY SHAZ! Thats not common sense man, taht eats through the fps like grandma eats through her strait jacket. Must....be...better...
Posted: 16th Feb 2003 17:43
I don't care, that's way better than anything I could ever do. Nice code, mate.
Posted: 16th Feb 2003 19:03
I did not notice any slowdown on my 2 pc's, still get well over 100fps

Admittedly one is an athlon xp2000 with geforce ti4600 and other is a laptop with p4 2.5ghz and radeon 9000 graphics

When I get time I will rewrite whole thing properly anyway

I will also try on some slower pc's as I obviously don't realise just how slow it possibly runs on slower pc's

Or maybe everyone else should buy a faster pc!
Posted: 16th Feb 2003 20:11
Awesome work man
Posted: 17th Feb 2003 7:24
Looks Awesome... but fps could be better,but I've seen worse...
Posted: 20th Feb 2003 5:06
Was this done in pro? it wont work in classic for me

RPGamer
Posted: 20th Feb 2003 12:45
Done in Pro with latest patch.

Didn't bother loading up classic on my new pc as I didn't want to waste time getting programs to work on both.

p.s.

It will speed up a bit if you take off sky sphere and the wavy water effect.

I have also rejigged the collision stuff to be a bit faster but I am saving this code for a game I am writing.

Once game is finished, I may post code.

The idea of the post was to help people having problems with matrix code and for them to improve the code themselves.

You will never learn if all you do is cut and paste!
Posted: 20th Feb 2003 16:00
your code is awesome..it if you could make a update to colllisions with 3d objects (.x and .3ds) it would be outstanding!!!!!
Posted: 20th Feb 2003 17:10
I think if you stick an 'x' file somewhere on matrix, collision should be automatic because of the way I've done things.

When I get home tonight, I'll have a play.

I've never actually done anything with 'proper' 3d objects yet but I suppose now is the time!
Posted: 20th Feb 2003 19:30
only have clasic still its looks Super...Sonic...now all you need to do is add in a round guy with a mustach who collects rings er... $$$
Posted: 21st Feb 2003 13:38
Plonked a few 'x' files on matrix and appears that any loaded objects are autoset to box collision. So long as that box touches the ground, then sliding collision is already working. Alternatively you can set sphere collision on any loaded objects. Polygon collision just crashes on my computer when you approach the object (probably getting fixed in patch 4)

You should also note that sliding collision is only done on native and loaded objects if they touch the ground. If you had a hovering cube for instance, you would walk straight through it!

Another problem is that if objects are not 'thick' enough, you will probably slide right through them.

The tile textures are just random rubbish that program creates so I don't have to provide image files. You can just cut and paste code. Once there are some proper textures in there and I've written a matrix editor, it will probably be quite good.

To allow jumping means doing really complicated collision stuff and allowing you to walk on top of objects. I'm just not interested in doing this at the moment!

I did have an idea of doing multiple matrixes so you could have caves and tunnels but still allowing you to climb over the mountain over the cave (if you see what I mean). I might have a go at this at weekend as it opens up some possibilites of good games.

The other obvious ommision is doing a third person view behind the player and this will entail some clever camera following code.