Hmmmm been a bit since anyone's posted.. I'm pretty sure when Narf says 'timing issues' he means the value of time# in this:
Quote: "NDB_NewtonUpdate time#"
I haven't had any problems with it really, so the only thing I can think of is you're using a different sync rate than 60
Anyways, while I've been absent from my newton project lately (been working on my music) I had a chance to fiddle with it tonight--for those of you wondering at the JetPac demo, I have a modded version here. Main changes are useage of the omega value for rotation instead of applying a vector, which is kinda a juryrigged (and slow!) approach. and uhm.. I think that's it
The differences are obious--120 boxes in motion with (fairly) minimal slowdown. Besides, in a situation when you have a wall flying into 120 pieces as you ram through it you'd want a little bullettime so you can gloat
Code is in this snippet and also in the code box (it's the same for both)
Rem Project: Simple controllabe JePac pr whatever 3rd Persons view demo
Rem Created: 2005-02-20 17:14:47
Rem Kjelle Mickelsson kjelle69@telia.com
`modded by Hawkeye (w00t!)
Rem ***** Main Source File *****
Rem Control Character with Arrowkeys and Space
sync on
sync rate 60
autocam off
Rem Setup Light
set ambient light 100
Rem This command must be called before calling any other newton commands !
NDB_Newtoncreate
Rem Here we set the minimum framerate which newton will allow without having to make
Rem substep calculation.
set camera range 1,1000
`--------BBOX THINGEE
`Maximum number of boxes = maxy*maxx
maxy=6
maxx=22
`---------MASS + SIZES
`bbox
boxmass#=1
boxsize#=1.8
`fps Object
fpsmass#=40
fpssize#=2
`----------FORCE VALUES
Rem The force which we will use to control the fps object later on
Rem We set it to 40 (Newton)
Force#=40
`this is the turning force that I'll apply to the fpsobject when I want it to turn
turn_force#=4
Rem Gravity (-9.8)
NDB_SetVector 0.0, -9.8, 0.0
NDB_SetStandardGravity
Rem Array to keep track of all boxes, Each bbox(x) will become a newton rigid body.
dim bbox(200)
Rem Create Boxes, both DBPro Object and Newton Object
for y = 0 to maxy
for x=1 to maxx
obj=x+(y*maxx)
Rem DBPro object
make object box obj,boxsize#,boxsize#,boxsize#
color object obj,RGB(100,100,x*2.5)
set object ambience obj, 80
Rem Newton Collision object same size as DBPro Object
col=NDB_NewtonCreateBox(boxsize#,boxsize#,boxsize#)
Rem Here bbox becomes a newton collision object
bbox(obj) = NDB_NewtonCreateBody(col)
Rem Connect Newton and DBPro object This means that the callback system will
Rem Automaticall update position of the graphical shape (The box)
NDB_BodySetDBProData bbox(obj), obj
Rem Set Mass of NewtonObject
NDB_NewtonBodySetMassMatrix bbox(obj),boxmass#
Rem Position Objects
Rem First three Floats in the Buildmatrix represents the Global rotation
Rem Next three Floats in the Buildmatrix represents the Global position
NDB_BuildMatrix 0.0, 0.0, 0.0, x*2.0, y*2, 0.0
Rem This command reads the matrix created above and rotates and position the object
Rem bbox(obj) regarding to the content of the matrix
NDB_NewtonBodySetMatrix bbox(obj)
Rem Release collision to free up memory, Newton object stays active !
NDB_NewtonReleaseCollision col
Rem Make sure the Gravity in the Newton World affects each bbox
NDB_BodySetGravity bbox(obj),1
next x
next y
Rem Make World
obj=2000
Rem Create a box which will act as floor
make object box obj,1000,1,1000
color object obj,RGB(200,200,2.5)
set object ambience obj, 80
Rem Make a Treecollision object of the box, treecollisions is Static objects.
Rem They can be moved with Body Set matrix command, but there is no automatic callback to
Rem reposition the DBPro object automatically
REm External forces have no impact on them either.
col=NDB_NewtonCreateTreeCollision(obj)
Room = NDB_NewtonCreateBody(col)
NDB_BodySetDBProData Room, obj
Rem This line positions the object
Rem First three Floats in the Buildmatrix represents the Global rotation
Rem Next three Floats in the Buildmatrix represents the Global position
NDB_BuildMatrix 0.0, 0.0, 0.0, 0.0,-5.0,0.0
NDB_NewtonBodySetMatrix Room
Rem Release collision to free up memory, Newton object stays active !
NDB_NewtonReleaseCollision col
Rem Remeber that this is a Treecollision object, which means that DBPro does not
Rem automatically updates the position , we have to do it ourselves !
Position object NDB_GetDBPro(Room),0,-5,0
Rem Make FPS Character
obj=2001
Rem A bit boring, This is not Duke Nukem, but just a box, however with some Imagination
Rem the box can become whatever you want it to be !
make object box obj,fpssize#,fpssize#,fpssize#
color object obj,RGB(100,200,2.5)
set object ambience obj, 80
Rem Lets put some shading for nice looks, take away this command to increase performance
set shadow shading on obj
Rem Create a Newtoncollision in the shape of a box
col=NDB_NewtonCreatebox(fpssize#,fpssize#,fpssize#)
Rem Here fps becomes a newton collision object
fps = NDB_NewtonCreateBody(col)
Rem Connect Newton and DBPro object This means that the callback system will
Rem Automaticall update position of the graphical shape (The box)
NDB_BodySetDBProData fps, obj
Rem Set Mass of NewtonObject
NDB_NewtonBodySetMassMatrix fps,fpsmass#
Rem Gravity affected = Yes If you put a 0 instead it will not be affected of gravity.
NDB_BodySetGravity fps,1
Rem The Autofreeze command with a 0 makes sure that Newton always have the object 'Active'
Rem The default value is Autofreeze on which means that Newton does not update the object
Rem if it comes to rest. We want to move the fps object with the addforce command so we
Rem Dont want it to freeze
NDB_NewtonBodySetAutoFreeze fps,0
Rem Position Objects
Rem First three Floats in the Buildmatrix represents the Global rotation
Rem Next three Floats in the Buildmatrix represents the Global position
NDB_BuildMatrix 0.0, 0.0, 0.0, 10.0,0.0,-25.0
NDB_NewtonBodySetMatrix fps
Rem Free up some memory
NDB_NewtonReleaseCollision col
Rem Fix the box to an upvector joint
NDB_SetVector 0,1,0
UpvectorJoint = NDB_NewtonConstraintCreateUpVector( fps )
Rem Generate Materials
Rem Without materials you cant change the friction elasticity and softness of
Rem different Rigid bodies in your system.
Rem First we creates some identification variables to store the materials in
Rem Defaultmat (Which is the default material always present in Newton
Rem BoxMat which becomes the material of each bbox
Rem FpsMat which is to be the material of the fps object
DefaultMat = NDB_NewtonMaterialGetDefaultGroupID()
BoxMat = NDB_NewtonMaterialCreateGroupID()
FpsMat = NDB_NewtonMaterialCreateGroupID()
Rem Here we set the Static and the Dynamic friction between the Defaultmaterial
Rem And the bbox material.
Rem The Floor in our case will have the defaultmaterial because we dont specify
Rem any material for that object. = Gets the defaultgroupid automatically.
NDB_NewtonMaterialSetDefaultFriction DefaultMat, BoxMat, 0.5, 0.5
Rem Here the elasticity between the materials is specified
NDB_NewtonMAterialSetDefaultElasticity DefaultMat, BoxMat, 0.5
Rem Here the softness between the materials is specified
NDB_NewtonMaterialSetDefaultSoftness DefaultMat, BoxMat, .5
Rem Here we specify how the boxes will interact with eachother !
NDB_NewtonMaterialSetDefaultFriction BoxMat, BoxMat, 0.5,0.5
Rem Here the elasticity between the boxes is specified
NDB_NewtonMAterialSetDefaultElasticity BoxMat, BoxMat, 0.3
Rem Here the softness between the boxes is specified
NDB_NewtonMaterialSetDefaultSoftness BoxMat, BoxMat, .5
Rem Here we specify the friction between the floor and the fps object
NDB_NewtonMaterialSetDefaultFriction DefaultMat, FpsMat, .2,.2
Rem Here the elasticity between the floor and the fps object
NDB_NewtonMAterialSetDefaultElasticity DefaultMat, FpsMat, 0.5
Rem Here the softness between the floor and the fps object
NDB_NewtonMaterialSetDefaultSoftness DefaultMat, FpsMat, .5
Rem Now we have to make sure each rigid body gets the proper material assignment
Rem First we iterate through each bbox
for y = 0 to maxy
for x=1 to maxx
obj=x+(y*maxx)
NDB_NewtonBodySetMaterialGroupID bbox(obj), boxMat
next x
next y
Rem Than we set the material FpsMat to the fps object !
NDB_NewtonBodySetMaterialGroupID fps, FpsMat
Rem Now we Specify which objects will affect eachother with the stated material parameters
Rem Floor against bbox
NDB_NewtonMaterialSetCollisionCallback DefaultMat, BoxMat
Rem Floor against fps object
NDB_NewtonMaterialSetCollisionCallback DefaultMat, FpsMat
Rem Boxes against boxes
NDB_NewtonMaterialSetCollisionCallback BoxMat, BoxMat
Rem Fps objct against boxes , thats all !
NDB_NewtonMaterialSetCollisionCallback FpsMat, BoxMat
Rem Main loop
do
Rem Calculate the time since the last call to update newton objects !
time# = NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
Rem Just print out how many times we have called newton since the program started.
text 10,10," Cycles "+str$(Looptimes)
text 10,20," Control Character with Arrowkeys and Space"
Rem Increment Looptimes with 1
inc Looptimes
Rem Position the DBPro camera something like a 3rd person view option in a fps game :-)
position camera Object Position x (NDB_GetDBPro(fps)),Object Position y (NDB_GetDBPro(fps))+5,Object Position z (NDB_GetDBPro(fps))
rotate camera object angle x (NDB_GetDBPro(fps)),object angle y (NDB_GetDBPro(fps)),object angle z (NDB_GetDBPro(fps))
Rem Back Off Camera a bit
move camera -20
Rem Here we set the forces on the fps object (Movement)
Rem The first vector 1 is in this case (NDB_BodyAddForceLocal) saying
Rem WHERE on the fps object the force will be set.
Rem I have made a slight offset in the direction of the front of the object
Rem Which I will use to make the object turn. (I am making sure that
Rem the forces will be pushing it on the 'nose')
Rem (0.3 units in the Z direction, object front direction)
NDB_SetVector 1,0,0,0.009
Rem The second vector is telling NDB_BodyAddForceLocal how BIG the force will be
Rem Here we just reset it to 0 if no movement is made.
NDB_SetVector 2,0,0,0
Rem In this input sequence we are setting the second vector to the size of
Rem the force.
if upkey() then NDB_SetVector 2,0,0,force#
if downkey() then NDB_SetVector 2,0,0,-force#
if spacekey() then NDB_SetVector 2,0,force#*1.2,0
if shiftkey() then NDB_SetVector 2,0,-force#*1.2,0
Rem This commands read the contents of the Vector one and two and
Rem Adds the force with the position (Point in the objects Local space) (Vector 1)
Rem and the force vector (Vector 2 also in the objects local space )
NDB_BodyAddForceLocal fps
if leftkey()=1 then NDB_SetVector 0.0, -turn_force#, 0.0
if rightkey()=1 then NDB_SetVector 0.0, turn_force#, 0.0
if leftkey()=0 and rightkey()=0 then NDB_SetVector 0,0,0
if leftkey()=1 and rightkey()=1 then NDB_SetVector 0,0,0
NDB_NewtonBodySetOmega fps
Rem this is a useful debugging command which means that if you push the Ctrl key
Rem The world will be drawn as NEWTON sees it, regardless of how darkbasic is
Rem drawing the objects
if controlkey() then NDB_DebugDrawNewtonLines
Rem DBPro sync command
sync
loop
Rem This function block is needed when using treecollisions as we use DBPro memblocks to
Rem make some calculations
function NeverCalled()
if memblock exist(1) then delete memblock 1
endfunction