Here's the problem code. I've made to newton physics bits standout so you can see if there's anything wrong with that code. I don't think there's any need to check the rest. Oh, and you need a "grass2.jpg" file (in black and white".
` This code was downloaded from The Game Creators
` It is reproduced here with full permission
` http://www.thegamecreators.com
sync on : sync rate 0
randomize timer()
set display mode 1024,768,32
autocam off
load image "grass2.jpg",1
rem read heightmap
dim height#(64,64)
for x=0 to 64
for z=0 to 64
height#(x,z)=-200+rnd(1000)
next z
next x
smooth_matrix(10)
rem needed vars!
dim tilex(128)
dim tilez(128)
dim tilesizex#(128)
dim tilesizez#(128)
dim texture(128)
dim xpart#(128)
dim ypart#(128)
dim object(128)
rem make sea texture
make memblock 10,(64*64*4)+12
write memblock dword 10,0,64
write memblock dword 10,4,64
write memblock dword 10,8,32
for x=1 to 64
for y=1 to 64
height#=((height#(x-1,(65-y)-1)+height#(x,(65-y)-1)+height#(x-1,(65-y))+height#(x,(65-y)))/4.00)*1.25
if height#>350 then color=RGB(250,250,250)
if height#<150 then color=rgb(0,0,250)
if height#<=350 and height#>=150 then color=rgb((height#-150)*1.25,(height#-150)*1.25,250)
write memblock dword 10,12+((((y-1)*64)+(x-1))*4),color
next y
next x
make image from memblock 2,10
delete memblock 10
rem make the object
makematrix(1,64*60,64*60,64,64) : makematrix(2,64*60,64*60,8,8) : makematrix(3,64*60,64*60,8,8)
texturematrix(1,1,64,64) : texturematrix(2,2,1,1) : texturematrix(3,2,1,1)
color backdrop rgb(150,150,255)
position camera 0,50,0
set camera range 1,20000
rem setup matrix
for x=0 to tilex(1)
for z=0 to tilez(1)
if height#(x,z)>=200 and height#(x,z)<300 then color=rgb(int(200-((height#(x,z)-200)*1.50)),int(150+((height#(x,z)-200)*1.00)),50)
if height#(x,z)>=300 and height#(x,z)<400 then color=rgb(int(50+((height#(x,z)-300)*1.00)),int(250-((height#(x,z)-300)*1.00)),int(50+((height#(x,z)-300)*1.00)))
if height#(x,z)>=400 then color=rgb(150,150,150)
if height#(x,z)<200 then color=rgb(200,150,50)
setmatrixheight(1,x,z,height#(x,z)*1.50)
setmatrixcolor(1,x,z,color)
next z
next x
normalizematrix(1,0,0,tilex(1),tilez(1))
updatematrix(1) : updatematrix(2) : updatematrix(3)
rem position the matrix
positionmatrix(2,0,300,0)
positionmatrix(3,0,300,0)
ghostmatrix(2,0)
ghostmatrix(3,1)
set object cull 50002,0
` NEWTON PHYSICS ********************************************
`Three D Point in space, used for positions, velocities, etc
TYPE ThreeD
x as float
y as float
z as float
ENDTYPe
rem INITIALIZE PHYSICS WORLD!
Floor = 50001
global Ball = 5
rem Initialize the Physics world. this must be called before any other commands.
NDB_NewtonCreate
rem MATERIALS
rem Materials for the physics engine. Changing these variables will change how the balls interact with the terrain
global FloorMat = NDB_NewtonMaterialCreateGroupID()
global BallMat = NDB_NewtonMaterialCreateGroupID()
NDB_NewtonMaterialSetDefaultFriction FloorMat, BallMat, 0.5, 0.2
NDB_NewtonMaterialSetDefaultElasticity FloorMat, BallMat, 0.8
NDB_NewtonMaterialSetDefaultSoftness FloorMat, BallMat, 0.1
rem FLOOR (only 4 commands!!!) - this makes a Newton body out of a previously loaded object.
col = NDB_NewtonCreateTreeCollision( Floor )
NDB_NewtonCreateBody Floor, col
NDB_NewtonBodySetMaterialGroupID Floor, FloorMat
NDB_NewtonBodySetDestructorCallback Floor
rem Repeat until we have a reasonable time value.
repeat
time# = NDB_GetElapsedTimeInSec()
until time# < 0.1
` NEWTON PHYSICS **********************************************
rem vars
time=timer()
fase#=0.00
fogged=0
do
text 0,0,str$(screen fps())
text 0,20,str$(statistic(1))
rem get time
dt#=(timer()-time)/1000.0
time=timer()
yrotate camera wrapvalue(camera angle y()+mousemovex())
xrotate camera wrapvalue(camera angle x()+mousemovey())
if mouseclick()=1 then move camera 200*dt#
if mouseclick()=2 then move camera -200*dt#
height#=getgroundheight(1,camera position x(),camera position z())+5.0
if camera position y()<height# then position camera camera position x(),height#,camera position z()
rem waving effect
positionmatrix(2,0,310+(sin(fase#)*10),0)
positionmatrix(3,0,310+(sin(fase#)*10),0)
fase#=fase#+(45*dt#)
rem fogeffect
if camera position y()<310+(sin(fase#)*10) and fogged=0
fogged=1
fog on
fog color rgb(50,50,250)
fog distance 200
color backdrop rgb(75,75,255)
color ambient light rgb(75,75,250)
endif
if camera position y()>=310+(sin(fase#)*10) and fogged=1
fogged=0
fog off
color backdrop rgb(150,150,255)
color ambient light rgb(255,255,255)
endif
` NEWTON PHYSICS ***************************
if returnkey() and RETURNPRESSED = 0
RETURNPRESSED = 1
x# = camera position x()
y# = camera position y()
z# = camera position z()
vx# = sin(camera angle y()) * 10.0
vy# = cos(camera angle x()) * 10.0
vz# = cos(camera angle y()) * 10.0
MakeBall(x#, y#, z#, vx#, vy#, vz#)
endif
` NEWTON PHYSICS ***************************
sync
loop
function makematrix(mem,x#,z#,tilex,tilez)
tilex(mem)=tilex
tilez(mem)=tilez
tilesizex#(mem)=x#/tilex(mem)
tilesizez#(mem)=z#/tilez(mem)
polys=2*tilex*tilez
size=(polys*36*3)+12
make memblock mem,size
write memblock dword mem,0,338
write memblock dword mem,4,36
write memblock dword mem,8,polys*3
pos=12
for x=1 to tilex(mem)
for z=1 to tilez(mem)
writevectordata(mem,pos,(x-1.0)*tilesizex#(mem),0.000,(z-1.0)*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),0.000,1.000)
writevectordata(mem,pos+36,x*tilesizex#(mem),0.000,z*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),1.000,0.000)
writevectordata(mem,pos+72,x*tilesizex#(mem),0.000,(z-1.0)*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),1.000,1.000)
writevectordata(mem,pos+108,(x-1.0)*tilesizex#(mem),0.000,(z-1.0)*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),0.000,1.000)
writevectordata(mem,pos+144,(x-1.0)*tilesizex#(mem),0.000,z*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),0.000,0.000)
writevectordata(mem,pos+180,x*tilesizex#(mem),0.000,z*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),1.000,0.000)
pos=pos+216
next z
next x
object(mem)=mem+50000
make mesh from memblock object(mem),mem
make object object(mem),object(mem),0
endfunction
function writevectordata(mem,pos,X# as float,Y# as float,Z# as float,NX# as float,NY# as float,NZ# as float,color as dword,TU# as float,TV# as float)
write memblock float mem,pos,X#
write memblock float mem,pos+4,Y#
write memblock float mem,pos+8,Z#
write memblock float mem,pos+12,NX#
write memblock float mem,pos+16,NY#
write memblock float mem,pos+20,NZ#
write memblock dword mem,pos+24,color
write memblock float mem,pos+28,TU#
write memblock float mem,pos+32,TV#
endfunction
function setmatrixheight(mem,x,z,y#)
if x-1>=0 and z-1>=0
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+40,y#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+184,y#
endif
if x-1>=0 and z<tilez(mem)
write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+76,y#
endif
if x<tilex(mem) and z-1>=0
write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+148,y#
endif
if x<tilex(mem) and z<tilez(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+4,y#
write memblock float mem,12+(216*((x*tilez(mem))+z))+112,y#
endif
endfunction
function setmatrixcolor(mem,x,z,color)
if x-1>=0 and z-1>=0
write memblock dword mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+60,color
write memblock dword mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+204,color
endif
if x-1>=0 and z<tilez(mem)
write memblock dword mem,12+(216*(((x-1)*tilez(mem))+z))+96,color
endif
if x<tilex(mem) and z-1>=0
write memblock dword mem,12+(216*((x*tilez(mem))+(z-1)))+168,color
endif
if x<tilex(mem) and z<tilez(mem)
write memblock dword mem,12+(216*((x*tilez(mem))+z))+24,color
write memblock dword mem,12+(216*((x*tilez(mem))+z))+132,color
endif
endfunction
function setmatrixnormal(mem,x,z,nx#,ny#,nz#)
if x-1>=0 and z-1>=0
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+48,nx#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+52,ny#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+56,nz#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+192,nx#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+196,ny#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+200,nz#
endif
if x-1>=0 and z<tilez(mem)
write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+84,nx#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+88,ny#
write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+92,nz#
endif
if x<tilex(mem) and z-1>=0
write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+156,nx#
write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+160,ny#
write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+164,nz#
endif
if x<tilex(mem) and z<tilez(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+12,nx#
write memblock float mem,12+(216*((x*tilez(mem))+z))+16,ny#
write memblock float mem,12+(216*((x*tilez(mem))+z))+20,nz#
write memblock float mem,12+(216*((x*tilez(mem))+z))+120,nx#
write memblock float mem,12+(216*((x*tilez(mem))+z))+124,ny#
write memblock float mem,12+(216*((x*tilez(mem))+z))+128,nz#
endif
endfunction
function getmatrixheight(mem,x,z)
if x-1>=0 and z-1>=0
height#=memblock float(mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+40)
else
if x<tilex(mem) and z<tilez(mem)
height#=memblock float(mem,12+(216*((x*tilez(mem))+z))+4)
else
height#=memblock float(mem,12+(216*(((x-1)*tilez(mem))+z))+76)
endif
endif
endfunction height#
function randomizematrix(mem,bx,bz,ex,ez,value#)
for x=bx to ex
for z=bz to ez
setmatrixheight(mem,x,z,(rnd(int(value#*100))/100.0)-(0.5*value#))
next z
next x
endfunction
function normalizematrix(mem,bx,bz,ex,ez)
if bx<=0 then bx=1
if bz<=0 then bz=1
if ex>=tilex(mem) then ex=tilex(mem)-1
if ez>=tilez(mem) then ez=tilez(mem)-1
rem Use matrix normals to make it smooth
for z=bz to ez
for x=bx to ex
rem Get matrix heights
h8#=getmatrixheight(mem,x,z-1)
h4#=getmatrixheight(mem,x-1,z)
h#=getmatrixheight(mem,x,z)
h2#=getmatrixheight(mem,x,z)
rem Calculate projected angle X using heights
x1#=(x-1)*25.0 : y1#=h#
x2#=(x+0)*25.0 : y2#=h4#
dx#=x2#-x1#
dy#=y2#-y1#
ax#=atanfull(dx#,dy#)
ax#=wrapvalue(90-ax#)
rem Calculate projected angle Z using heights
z1#=(z-1)*25.0 : y1#=h2#
z2#=(z+0)*25.0 : y2#=h8#
dz#=z2#-z1#
dy#=y2#-y1#
az#=atanfull(dz#,dy#)
az#=wrapvalue(90-az#)
rem Make normal from projected angle
nx#=sin(ax#)
ny#=cos(ax#)
nz#=sin(az#)
rem Setting matrix normal for smoothness
setmatrixnormal(1,x,z,nx#,ny#,nz#)
next x
next z
endfunction
function updatematrix(mem)
change mesh from memblock object(mem),mem
change mesh object(mem),0,object(mem)
texture object object(mem),texture(mem)
endfunction
function getgroundheight(mem,x#,z#)
if x#>=0 and z#>=0 and x#<tilex(mem)*tilesizex#(mem) and z#<tilez(mem)*tilesizez#(mem)
xt=int(x#/tilesizex#(mem))
zt=int(z#/tilesizez#(mem))
if (((xt+1)*tilesizex#(mem))-x#)+(z#-(zt*tilesizez#(mem)))<=tilesizex#(mem)
dx#=getmatrixheight(mem,xt+1,zt)-getmatrixheight(mem,xt,zt)
dz#=getmatrixheight(mem,xt+1,zt+1)-getmatrixheight(mem,xt+1,zt)
else
dx#=getmatrixheight(mem,xt+1,zt+1)-getmatrixheight(mem,xt,zt+1)
dz#=getmatrixheight(mem,xt,zt+1)-getmatrixheight(mem,xt,zt)
endif
height#=(((x#-(xt*tilesizex#(mem)))/tilesizex#(mem))*dx#)+(((z#-(zt*tilesizez#(mem)))/tilesizez#(mem))*dz#)+getmatrixheight(mem,xt,zt)
endif
endfunction height#
function preparematrixtexture(mem,image,xsplit,ysplit)
texture(mem)=image
xpart#(mem)=1.000/xsplit
ypart#(mem)=1.000/ysplit
for x=0 to tilex(mem)-1
for z=0 to tilez(mem)-1
setmatrixtile(mem,x,z,1,1)
next z
next x
endfunction
function setmatrixtile(mem,x,z,xtex,ytex)
write memblock float mem,12+(216*((x*tilez(mem))+z))+28,(xtex-1)*xpart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+32,ytex*ypart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+64,xtex*xpart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+68,(ytex-1)*ypart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+100,xtex*xpart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+104,ytex*ypart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+136,(xtex-1)*xpart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+140,ytex*ypart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+172,(xtex-1)*xpart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+176,(ytex-1)*ypart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+208,xtex*xpart#(mem)
write memblock float mem,12+(216*((x*tilez(mem))+z))+212,(ytex-1)*ypart#(mem)
endfunction
function texturematrix(mem,image,xtile#,ztile#)
texture(mem)=image
texx#=xtile#/tilex(mem)
texz#=ztile#/tilez(mem)
for x=0 to tilex(mem)-1
for z=0 to tilez(mem)-1
z2=(tilez(mem)-z)-1
write memblock float mem,12+(216*((x*tilez(mem))+z2))+28,(x*texx#)
value#=((z+1)*texz#) : if value#=0.00 then value#=1.00
write memblock float mem,12+(216*((x*tilez(mem))+z2))+32,value#
value#=((x+1)*texx#) : if value#=0.00 then value#=1.00
write memblock float mem,12+(216*((x*tilez(mem))+z2))+64,value#
write memblock float mem,12+(216*((x*tilez(mem))+z2))+68,(z*texz#)
value#=((x+1)*texx#) : if value#=0.00 then value#=1.00
write memblock float mem,12+(216*((x*tilez(mem))+z2))+100,value#
value#=((z+1)*texz#) : if value#=0.00 then value#=1.00
write memblock float mem,12+(216*((x*tilez(mem))+z2))+104,value#
write memblock float mem,12+(216*((x*tilez(mem))+z2))+136,(x*texx#)
value#=((z+1)*texz#) : if value#=0.00 then value#=1.00
write memblock float mem,12+(216*((x*tilez(mem))+z2))+140,value#
write memblock float mem,12+(216*((x*tilez(mem))+z2))+172,(x*texx#)
write memblock float mem,12+(216*((x*tilez(mem))+z2))+176,(z*texz#)
value#=((x+1)*texx#) : if value#=0.00 then value#=1.00
write memblock float mem,12+(216*((x*tilez(mem))+z2))+208,value#
write memblock float mem,12+(216*((x*tilez(mem))+z2))+212,(z*texz#)
next z
next x
endfunction
function positionmatrix(mem,x#,y#,z#)
position object object(mem),x#,y#,z#
endfunction
function ghostmatrix(mem,ghost)
ghost object on object(mem),ghost
endfunction
function smooth_matrix(t)
for times=1 to t
for x=0 to 64
for z=0 to 64
height#(x,z)=(height#(x,z)+height#(x+1,z)+height#(x,z+1)+height#(x+1,z+1))/4
next z
next x
next times
endfunction
function MakeBall(x as float, y as float, z as float, vx as float, vy as float, vz as float)
rem Make a new piece of collision gemoetry, store a link to it in col1
col1 = NDB_NewtonCreateSphere(1.0, 0)
rem Make the DBPro object to represent this collision geometry.
make object sphere Ball, 2.0
position object Ball, x, y, z
rem Make a Newton Rigid Body from this collision geomtery (col1),
rem and assign it to the DBPro object (Ball)
NDB_NewtonCreateBody Ball, col1
rem This tells Newton to destory the DBPro object then the rigid
rem body is destroyed.
NDB_NewtonBodySetDestructorCallback Ball
rem This tells Newton that we want it to move and rotate the
rem DBPro object for us when the rigid body is active.
NDB_NewtonBodySetTransformCallback Ball
rem This tells Newton that we want to apply a standard Gravitational
rem force on the object. future versions will have more options for this part.
NDB_NewtonBodySetForceAndTorqueCallback Ball
rem This sets the Mass, and Axis of Inertia settings for the body.
rem Thr first "1.0" is the mass, the rest are inertia settings.
NDB_NewtonBodySetMassMatrix Ball, 1.0, 1.0, 1.0, 1.0
rem This applies a material (we nade above) to the rigid body.
NDB_NewtonBodySetMaterialGroupID Ball, BallMat
rem Set the internal temp matrix to an identity matrix (no rotation)
NDB_SetMatrixIdentityMatrix
rem Set the internal temp vector to where we want to position the body.
NDB_SetVector x, y, z, 1.0
rem set the temp vector into the "posiion" row of the temp matrix.
NDB_SetMatrixPosRow
rem Set the temp matrix as the translation matrix for our body
rem This sets the initial position and rotation of the body/object.
NDB_NewtonBodySetMatrix Ball
rem Set the internal temp vector to our velocity settings.
NDB_SetVector vx, vy, vz
rem Sets the velocity of Body "Ball" to the contents of the temp vector
rem which we set in the lin above.
NDB_NewtonBodySetVelocity Ball
rem We aren't making any other spheres, so we can release the link to the
rem cllision geometry
NDB_NewtonReleaseCollision col1
rem inc the Ball counter so the next ball is a new object.
inc Ball
endfunction
And don't worry, the dlls are all in the right folders.
coincidence? how about pure cosmic convergence!