hi, i have just added the newtons physics to my level editor I am coding, but when it comes to clearing the objects it errors, stops responding totally!
here is the code:
Rem Project: 3dEditor2
Rem Created: Thursday, October 17, 2013
Rem ***** Main Source File *****
autocam Off
set display mode desktop width(),desktop height(),32
Sync On: Sync Rate 120: Sync
set window off
Color BackDrop Rgb( 0, 0, 0 )
Color Ambient Light 0 : Set Ambient Light 25
`phy start
activatephy()
global count=0
global pop=0
global lightnumber=1
global cursorobject = 6999
global parentobject = 7000
global objectnumber = 1
global height#=50.0
global smooth#=25.0
global distance#=180.0
global angle# =45.0
global cursx#
global cursy#
global cursz#
global currenttexture=2
global tick
global zoomfactor#
global sizex#=5.0
global sizey#=5.0
global sizez#=5.0
global addx#=0
global addz#=0
global s$="Saved!"
global load$ = "Loaded!"
global levelnumber = 1
global mass#=0.0
global l$
type blocks
objnumber as integer
xloc# as float
yloc# as float
zloc# as float
sx# as float
sy# as float
sz# as float
texturenumber as integer
scalex# as float
scaley# as float
scalez# as float
objectmass# as float
endtype
dim block(6001) as blocks
clear()
startup()
time# = NDB_GetElapsedTimeInSec()
time# = NDB_GetElapsedTimeInSec()
Do
time# = NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
print count
control()
set camera to follow 0,cursx#,cursy#,cursz#,angle#,distance#+zoomfactor#,height#,smooth#,1
BirdsEyeCam(1,cursx#,cursy#,cursz#,50)
`phy update
print mass#
sync
LOOP
function control()
rem ***** var for tracking height of cursor *****
sizey#=object size y (cursorobject)
sizex#=object size y (cursorobject)
sizez#=object size y (cursorobject)
rem ***** Mouse Wheel to zoom in/out *****
z=mousemovez()
if z=120
zoomfactor#=zoomfactor#+5
endif
if z=-120
zoomfactor#=zoomfactor#-5
endif
print z
rem ****************************************
rem ***
rem **** test for key presses *****
r=scancode()
print r
tick=tick+1
if tick>7
tick=0
select r
rem ***** move up *****
case 200
cursz#=cursz#+sizez#
endcase
rem ***** move down *****
case 208
cursz#=cursz#-sizez#
endcase
rem ***** move left *****
case 203
cursx#=cursx#-5
endcase
rem ***** move right *****
case 205
cursx#=cursx#+5
endcase
rem ***** Raise cursor ****
case 30
cursy#=cursy#+sizey#
endcase
rem ***** Lower cursor *****
case 44
cursy#=cursy#-sizey#
endcase
rem ***** space-bar to place object *****
case 57
make object box objectnumber, 100, 100, 100
scale object objectnumber, object size x(cursorobject, 1),object size y(cursorobject, 1),object size z(cursorobject, 1)
position object objectnumber,cursx#,cursy#,cursz#
`texture object objectnumber,currenttexture
block(objectnumber).xloc# = cursx#
block(objectnumber).yloc# = cursy#
block(objectnumber).zloc# = cursz#
block(objectnumber).objnumber = 1
block(objectnumber).texturenumber = currenttexture
block(objectnumber).sx#= object size x(objectnumber, 1)
block(objectnumber).sy#= object size y(objectnumber, 1)
block(objectnumber).sz#= object size z(objectnumber, 1)
block(objectnumber).objectmass#=mass#
rem ******** create the newton physics ******
`MakeBox(obj,cursx#,cursyy#,cursz#,block(objectnumber).sx#,block(objectnumber).sy#,block(objectnumber).sz#,rx#,ry#,rz#,mass#)
MakeBox(objectnumber,cursx#,cursy#,cursz#,block(objectnumber).sx#,block(objectnumber).sy#,block(objectnumber).sz#,0,0,0,mass#)
inc objectnumber
endcase
rem ***** Move camera up higher *****
case 26
height#=height#+sizey#
endcase
rem ***** make camera move lower *****
case 40
height#=height#-sizey#
endcase
rem ***** Make cursor object wider *****
case 77
addx#=addx#+200
scale object cursorobject,100+addx#,100,100+addz#
`sizex#=sizex#*2
endcase
rem ***** Make cursor object narrower *****
case 75
if addx#>0.0
addx#=addx#-200
scale object cursorobject,100+addx#,100,100+addz#
`sizex#=sizex#/2
endif
endcase
rem ***** Make cursor object longer
case 72
addz#=addz#+200
scale object cursorobject,100+addx#,100,100+addz#
endcase
case 80
if addz#>0.0
addz#=addz#-200
scale object cursorobject,100+addx#,100,100+addz#
endif
endcase
case 60
save(levelnumber)
text desktop width()/2,desktop height()/2,s$
sync
wait 1000
endcase
case 46
clear()
endcase
case 59
clear()
load(levelnumber)
text desktop width()/2,desktop height()/2,load$
endcase
case 38
addlight(lightnumber,cursx#,cursy#,cursz#)
endcase
case 25
activatephy()
endcase
case 67
if mass#>0.0
mass#=mass#-1.0
endif
endcase
case 68
if mass#<10
inc mass#
endif
endcase
endselect
endif
rem ***** Place cursor object *****
position object cursorobject,cursx#,cursy#,cursz#
endfunction
Function startup()
load image "wall1.png",2
load image "halo02.jpg",3
make camera 1
set camera view 1, desktop width()-550,0,desktop width(),350
make matrix 1,2000,2000,25,25
position matrix 1,0,0,0
make object box cursorobject,sizex#,sizey#,sizez#
position object cursorobject,cursx#,cursy#,cursz#
ghost object on cursorobject
ENDFUNCTION
Function BirdsEyeCam(camnum,X#,Y#,Z#,Height2#)
position camera camnum,x#,y#+height2#+height#,z#
point camera camnum,x#,y#,z#
endfunction
function save(levelnumber)
l$="level"
l$=l$+str$(levelnumber)
l$=l$+".lev"
if file exist(l$) = 1
delete file l$
ENDIF
open to write 1,l$
for n=1 to 6000
write float 1,block(n).xloc#
write float 1,block(n).yloc#
write float 1,block(n).zloc#
write long 1,block(n).objnumber
write long 1,block(n).texturenumber
write float 1,block(n).sx#
write float 1,block(n).sy#
write float 1,block(n).sz#
write float 1,block(n).objectmass#
NEXT
close file 1
ENDFUNCTION
function clear()
`NDB_NewtonDestroy
for n=1 to 6000
if object exist(n)
delete object n
block(n).xloc# = 0
block(n).yloc# = 0
block(n).zloc# = 0
block(n).objnumber = 0
block(n).texturenumber=0
block(n).scalex# = 0
block(n).scaley# = 0
block(n).scalez# = 0
block(n).objectmass#=0.0
ENDIF
NEXT
objectnumber=1
count=0
sync
ENDFUNCTION
function load(levelnumber)
l$="level"
l$=l$+str$(levelnumber)
l$=l$+".lev"
open to read 1,l$
for n=1 to 6000
read float 1,block(n).xloc#
read float 1,block(n).yloc#
read float 1,block(n).zloc#
read long 1,block(n).objnumber
read long 1,block(n).texturenumber
read float 1,block(n).sx#
read float 1,block(n).sy#
read float 1,block(n).sz#
read float 1,block(n).objectmass#
if block(n).objnumber > 0
r=block(n).objnumber
scx#=block(n).sx#
scy#=block(n).sy#
scz#= block(n).sz#
textnum=block(n).texturenumber
make object box objectnumber,100,100,100
scale object objectnumber, scx#, scy#, scz#
`texture object objectnumber,textnum
position object objectnumber,block(n).xloc#,block(n).yloc#,block(n).zloc#
MakeBox(objectnumber,cursx#,cursy#,cursz#,block(objectnumber).sx#,block(objectnumber).sy#,block(objectnumber).sz#,0,0,0,block(n).objectmass#)
count=count+1
inc objectnumber
endif
NEXT
close file 1
ENDFUNCTION
function addlight(lightnumber,cursx#,cursy#,cursz#)
make light lightnumber
position light lightnumber,cursx#,cursy#,cursz#
`POINT LIGHT lightnumber,cursx#,cursy#,cursz#
inc lightnumber
ENDFUNCTION
function activatephy()
`Initialize Newton, and create the Newton World.
NDB_NewtonCreate
`We need to set the World limits, this is very important to do otherwise there
`could be some unpredictable results
NDB_SetVector 1, -1000.0, -500.0, -1000.0
NDB_SetVector 2, 1000.0, 500.0, 1000.0
NDB_NewtonSetWorldSize
`Set the temp vector to our gravity constant (acceleration due to gravity)
`then set as our standard gravity force.
NDB_SetVector 0.0, -50.0, 0.0
NDB_SetStandardGravity
ENDFUNCTION
` FUNCTIONS ---------------------------------
`all of these functions are pretty similar, they just make different collision
`primitives. I'll explain the BOX in detail, and the others are all basically the same,
`except for the Convex Hulls, which I'll explain in detail as well.
function MakeBox(obj,x#,y#,z#,sx#,sy#,sz#,rx#,ry#,rz#,mass#)
`the basic process when making a Newton Rigid Body is this:
`Step 1- make a "collision object". this defines the shape of the body you want to make.
` in this case, we're making a simple box object with size sx#, sy#, sz#.
` the newly created box collision is saved into the variable "Col", so we can reference
` it later.
Col = NDB_NewtonCreateBox(sx#, sy#, sz#)
`Step 2- Now we make the actual Rigid Body. when we do so, we must tell Newton the shape of
` the body, which we get from the collision data we just made. so, we pass the "Col"
` variable into this function, and it returns an index to the new rigid body into the
` variable "Body". we'll use "Body" to refer to this Rigid Body for the rest of the
` function.
Body = NDB_NewtonCreateBody(Col)
`Step 3- set the initial position and rotation of the rigid body. Newton does not use
` angles like DBPro, instead it uses transformation matrices to describe the position
` and rotation of a body. But the wrapper has a function to create these matrices for you
` based on position and angles, like you would use in DBpro. you set both the position
` and the rotation in a single command. this command "BuildMatrix" takes these values,
` and creates an internal matrix from them. then you tell Newton to use that matrix
` to set the orientation of the rigid body.
NDB_BuildMatrix rx#, ry#, rz#, x#, y#, z#
NDB_NewtonBodySetMatrix Body
`Step 4- Now we need to set the mass of the object. This should be a realistic value for
` best results. also, there is another property of rigid bodies called "moment of inertia"
` which basically describes how easily a body will spin when hit by other bodies. You can
` imagine a long thin rod. it rolls very easily, but spinning it like a baton takes more energy.
` that's an example of moment of inertia. Anyway the good news is that I have put in helper
` functions into the wrapper to calculate these values for you, based on physics forumulas.
` you just pass the size of your body, and the mass, and it fills the internal vector with the proper
` Moment of inertia values. then if you immediately call the SetMassMatrix command, it will use the
` values stored in the temp vector, and voila! you have a properly-behaving object!
NDB_CalculateMIBoxSolid mass#, sx#, sy#, sz#
NDB_NewtonBodySetMassMatrix Body, mass#
`Step 5- This just releases the collision object we created, telling Newton "we're done with it".
` If you wanted to make another body from this same collision data (shape), you wouldn't call
` this command.
NDB_NewtonReleaseCollision Col
`Okay, so we've now created a rigid body in the Newton World. We've set it's size, shape, mass, and position.
`Now we need to make a standard 3D object so we can visualize this rigid body in DBPro. This part should be
`familiar to you if you have used DBPro much. I simply load in a box.x primitive, and scale it so it's the
`same size as our rigid body. then I position and rotate it so it's matching the orientation of the rigid body.
`load object "..\media\box.x", obj
`scale object obj, sx#*100.0, sy#*100.0, sz#*100.0
` position object obj, x#, y#, z#
`rotate object obj, rx#, ry#, rz#
`color object obj, 9
`set object ambience obj, 50
`Step6 - this is the last important part. Here we tell the wrapper what visual object we are using to represent
` our rigid body in DBPro. Then after this, the wrapper will automatically update the position and rotation
` of the DBPro object to represent the rigid body. this is the beauty of the Newton system.
` NOTE: in previous versions of the wrapper you also had to call a SetTransformCallback function. this command
` has been removed, because it didn't really need to be called by the user. so now, all you need
` to do is call the BodySetDBProData command, and Newton knows you want to let the wrapper control
` this object.
NDB_BodySetDBProData Body, obj
`finally this command makes the link even stronger between Rigid Body and DBPro object. not only will the wrapper
`position and rotate the object for you (as set above), calling this command will tell the wrapper to automatically
`delete the DBPro object when the rigid body is destroyed. so for example when you call NDB_NewtonDestroy to destroy
`the entire newton world, Newton goes through and destroys each rigid body one by one. when it destroys this body,
`it will also delete the DBpro object for you.
NDB_NewtonBodySetDestructorCallback Body
`finally, this sets the "gravity flag" for this body. basically if you set this to 1, Newton will apply the
`StandardGravity (that we set at the beginning of the program) to the body each time you call NewtonUpdate, making
`the body fall. if you set this to 0, the body will not be affected by gravity. you can change this at any time,
`turning gravity on and off for bodies in realtime.
` NOTE: in previous wrapper versions you also had to call a confusing command called "SetForceandTorqueCallback".
` that command is no longer necessary. just call the SetGravity command to turn gravity on or off.
NDB_BodySetGravity Body, 1
endfunction Body
if i rem out the line NDB_NewtonDestroy in the clear function it is then that the program crashes:
function clear()
`NDB_NewtonDestroy
for n=1 to 6000
if object exist(n)
delete object n
block(n).xloc# = 0
block(n).yloc# = 0
block(n).zloc# = 0
block(n).objnumber = 0
block(n).texturenumber=0
block(n).scalex# = 0
block(n).scaley# = 0
block(n).scalez# = 0
block(n).objectmass#=0.0
ENDIF
NEXT
objectnumber=1
count=0
sync
ENDFUNCTION
this requires no media you simply resize the block with the numpad keys, place a block with space-bar,move y axes with a-z, F2 saves level while f1 loads and c clears the level! camera controls via the mouse wheel and the [ and ' keys
Hail to the king, baby!
http://davidjohnwheeler.blogspot.co.uk