Hey Codger, well done, nice demo!
I took the liberty of modifying your nice demo a little to use a material with no friction and 100% restitution (bounces don't drain the velocity), and set damping to zero. This eliminates the need for the 'MaintainVelocity' and makes it just use the pure physics. (And I fixed the leaks in the box corners =)
I kind of did this for Alfredo's (huertaaj) benefit mostly since he's brand new to DarkPhysics.
Here's the modified by VRMan version:
Rem Project: Glass Box by Codger
Rem Created: 11/23/2006 9:00:23 PM
` Modified by VRMan on 12/11/2006 to add a 'NoLossMaterial' to
` replace the MaintainVelocity function and some other minor changes.
Rem ***** Main Source File *****
randomize timer()
phy start
phy set gravity 0,0,0
SET AMBIENT LIGHT 50
autocam off
SYNC ON : SYNC RATE 60
GLOBAL MyTimer
` Not sure why this line won't compile for me:
` phy set continous cd 1.0
` I thought it was supposed to be in the 1.01 update? Oh well hehheh.
` VRMan Added:
` By using this material I added there's no need to call your MaintainVelocity function
` (turning off dampening for the spheres is required as well)
` and as a result I think it is more 'realistic' or 'authentic' or 'unadulterated' physics if you will. =)
global NoLossMat = 1
Phy Make Material NoLossMat, "TheImpossiblyPerfectMaterial"
` make the material have no friction during a collision
phy set material dynamic friction NoLossMat, 0.0
phy set material static friction NoLossMat, 0.0
` make the material never lose energy on bounces
phy set material restitution NoLossMat, 1.0
phy build material NoLossMat
`ID = 1 :Width = 80 : Height = 60 : Depth = 100 : Thickness = 5
`makebox(ID,Width, Height, Depth, Thickness, 0.0,0.0,0.0)
` VRMan changed the box to a cube and created it differently to fix the leaks in the corners
ID = 1 : BoxSize# = 100.0 : Thickness# = 3.0
MakeHollowCube(ID, BoxSize#, Thickness#, 0,0,0)
position camera 0, 0,0,-50
point camera 0,0,0,0
FirstSphere = 100 : LastSphere = 700
for ID = FirstSphere to LastSphere
make object sphere ID, ((rnd(20)+2)/10.0) ,10,10
color object ID, rgb(rnd(255),rnd(255),rnd(255))
position object ID,rnd(Width)-Width/2,rnd(Height)-Height/2,Rnd(Depth)-Depth/2
phy make rigid body dynamic sphere ID, NoLossMat
` Try to enable CCD per object also and I get the famous Skeleton Is False error message. Whatever heh.
`phy set rigid body ccd ID, 1
phy set rigid body linear velocity ID, rnd(100)-50,rnd(100)-50,rnd(100)-50
` VRMan added these next two commands as well so that the spheres don't slow down just flying around.
phy set rigid body linear damping ID, 0
phy set rigid body angular damping ID, 0
phy update
rem SYNC
next i
Mytimer = timer() + 5000
do
if shiftkey() then position camera 0, camera position x(), camera position y ()+.1, camera position z()
if controlkey() then position camera 0, camera position x(), camera position y ()-.1, camera position z()
CONTROL CAMERA USING ARROWKEYS 0, 1, 2
if scancode() = 0 and timer() < MyTimer
text 10,20, "FPS : "+ str$(screen FPS())
text 10,40, "Press spacekey to toggle box"
text 10,60, " Use arrow keys to move fwd,back, tuen left & right"
text 10,80, " Use shift, ctrl to move up & down"
endif
` for ID = FirstSphere to LastSphere
` VRMan commented all this out and added:
` by the way, I had made a function to use up the cycles you were using in MaintainVelocity to keep the
` occasionally fast moving spheres from escaping the box.
` But I thought it was a little much for a simple demo and omitted it after all.
` (the FasterThanCCD function just moved them back in when they escaped and reversed the velocity).
` Some still escape when their velocity is too high.
` FasterThanCCD(ID, Width, Height, Depth)
` MaintainVelocity(ID,100.0)
` next ID
if spacekey() and timer() > mytimer then FlipBox(1)
phy update
SYNC
loop
Function FlipBox(ID)
if object visible(ID)
For i = ID to ID+5
hide object i
next i
else
For i = ID to ID+5
show object i
next i
endif
mytimer = timer() + 500
Endfunction
Function MaintainVelocity(ID,MinVelocity#)
` VRMan's version doesn't call this anymore
x# = phy get rigid body linear velocity x(ID)
y# = phy get rigid body linear velocity y(ID)
z# = phy get rigid body linear velocity z(ID)
TotalVelocity# = abs(x#)+abs(y#)+Abs(z#)
If TotalVelocity# < MinVelocity#
phy add rigid body force ID, x#*MinVelocity#, y#*MinVelocity#,z#*MinVelocity#,0
endif
Endfunction
function MakeHollowCube(ID, theSize#, theThickness#, xp#, yp#, zp#)
` VRMan is making the box a slightly different way to get rid of the leaks at the corners
` the floor
make object box ID, theSize#, theThickness#, theSize#
position object ID, xp#, yp# - (theSize#/2.0), zp#
ghost object on ID
Phy make rigid body static box ID, NoLossMat
` the ceiling
inc ID
make object box ID, theSize#, theThickness#, theSize#
position object ID, xp#, yp# + (theSize#/2.0), zp#
ghost object on ID
Phy make rigid body static box ID, NoLossMat
` the left wall
inc ID
make object box ID, theThickness#, theSize#, theSize#
position object ID, xp# - (theSize#/2.0), yp#, zp#
ghost object on ID
Phy make rigid body static box ID, NoLossMat
` the right wall
inc ID
make object box ID, theThickness#, theSize#, theSize#
position object ID, xp# + (theSize#/2.0), yp#, zp#
ghost object on ID
Phy make rigid body static box ID, NoLossMat
` the front wall
inc ID
make object box ID, theSize#, theSize#, theThickness#
position object ID, xp#, yp#, zp# - (theSize#/2.0)
ghost object on ID
Phy make rigid body static box ID, NoLossMat
` the back wall
inc ID
make object box ID, theSize#, theSize#, theThickness#
position object ID, xp#, yp#, zp# + (theSize#/2.0)
ghost object on ID
Phy make rigid body static box ID, NoLossMat
endfunction
Function makebox(ID,x#,y#,z#,w#,xp#,yp#,zp#)
`VRMan's version doesn't call this anymore
rem this function creates a total of 6 objects
rem floor
make object box ID, x#,w#,z# : position object ID,0+xp#,(-y#/2.0)+yp#,0+zp# : Phy make rigid body static box ID : ghost object on ID
Rem Ceiling
inc ID,1 : make object box ID, x#,w#,z# : position object ID,0+xp#,(y#/2.0)+yp#,0+zp# : Phy make rigid body static box ID : ghost object on ID
Rem Front Wall
inc ID,1 : make object box ID, x#,y#,w# : position object ID,0+xp#,0+yp#,(-z#/2.0)+zp# : Phy make rigid body static box ID : ghost object on ID
Rem Rear Wall
inc ID,1 : make object box ID, x#,y#,w# : position object ID,0+xp#,0+yp#,(z#/2.0)+zp# : Phy make rigid body static box ID : ghost object on ID
Rem Right Wall
inc ID,1 : make object box ID, w#,y#,z# : position object ID,(x#/2.0)+xp#,0+yp#,0+zp# : Phy make rigid body static box ID : ghost object on ID
Rem left Wall
inc ID,1 : make object box ID, w#,y#,z# : position object ID,(-x#/2.0)+xp#,0+yp#,0+zp# : Phy make rigid body static box ID : ghost object on ID
Endfunction
And Alfredo, my latest email should be in your inbox, I look forward to hearing back from you tomorrow.
Best regards,
-=VRMan=-
World Famous 3D Screensavers
-- http://www.vrman3d.com --