Mini-Tutorial: Dark Physics - Bullet Time
What is Needed:
DarkPhysics
This shows way to have
Bullet Time camera motions during a DarkPhysics simulation
Usually DarkPhysics exists in its own world, managing all the objects
within its Physics world getting updated just before the DBPro sync command.
Use the arrow keys to move the camera, pausing the DarkPhysics simulation
While the camera move around
` Bullet Time camera modification, of the Physx eplosion demo code
` by - Bill R. - San Francisco.
` Arrow keys - fly camera around during Bullet Time freeze
` create a set of spheres and apply forces on them
` to create an explosion effect
` set up the program
set display mode desktop width(),desktop height(),32,1
sync on
sync rate 60
autocam off
color backdrop 0
phy start
make light 1
set directional light 1, -5,-5,5
ink rgb(0,0,50)
box 0,0,1,1
get image 3,0,0,1,1
cls
` make the floor object
make object box 1,40,1,40
phy make rigid body static box 1
ghost object on 1, 0
texture object 1, 3
` make the four walls
make object box 2,60,1,50
xrotate object 2,60
position object 2,0,20,-25
phy make rigid body static box 2
ghost object on 2, 0
texture object 2, 3
make object box 3,50,1,60
zrotate object 3,-60
position object 3,-25,20,0
phy make rigid body static box 3
ghost object on 3, 0
texture object 3, 3
make object box 4,60,1,50
xrotate object 4,-60
position object 4,0,20,25
phy make rigid body static box 4
ghost object on 4, 0
texture object 4, 3
make object box 5,50,1,60
zrotate object 5,60
position object 5,25,20,0
phy make rigid body static box 5
ghost object on 5, 0
texture object 5, 3
` set up the pile of spheres
levels = 4
xcount = 5
zcount = 5
boxID = 10
boxsize#=1.0
for l=0 to levels-1
lf#=l
for x=-xcount to xcount
for z=-zcount to zcount
make object sphere boxID, 1
position object boxID, x*2, boxsize#*lf# + 1, z*2
phy make rigid body dynamic sphere boxID
color object boxID, rgb ( rnd ( 255 ), rnd ( 255 ), rnd ( 255 ) )
set object specular boxID, rgb ( rnd ( 255 ), rnd ( 255 ), rnd ( 255 ) )
set object specular power boxID, 255
set object ambient boxID,0
inc boxID
next z
next x
next l
global numBoxes as integer : numBoxes = boxID-1
` position the camera
position camera -20,40,-25 :`was -20,20,-25
point camera 0,0,0
xcount = (xcount+1)*2000
zcount = (zcount+1)*2000
count = 0
` main program loop
do
phyflag=1 :`just a flag, whether to allow physx update or not
keypress=scancode()
camspeed#=camera position y()/50.0 :`rotate the camera the same speed, whether you're near or far the objects
` If a key is pressed, pause Physx updates, and move camera with arrow keys
if keypress>0
phyflag=0
if keypress= 200 :`up arrow
move camera camspeed#
point camera 0,0,0
endif
if keypress= 203 :`left arrow
turn camera right -90
move camera camspeed#
turn camera left -90
point camera 0,0,0
endif
if keypress= 205 :`right arrow
turn camera left -90
move camera camspeed#
turn camera right -90
point camera 0,0,0
endif
if keypress= 208 :`down arrow
move camera -camspeed#
point camera 0,0,0
endif
endif
` Update Physx if Bullet Time is not reqested
if phyflag=1
inc count
` automatic explosion
if count > 100 and object exist(500)=0
x# = (rnd(xcount)-xcount/2)/500.0
y# = (rnd(1000)+500)/1000.0
z# = (rnd(zcount)-zcount/2)/500.0
make object sphere 500,12
color object 500, rgb ( rnd ( 255 ), rnd ( 255 ), rnd ( 255 ) )
position object 500,x#,y#,z#
set alpha mapping on 500,50
createExplosion(x#,y#,z#,180,12)
count = 0
explos = 100
endif
if explos>0
dec explos
scale object 500,explos,explos,explos
if explos=0 then delete object 500
endif
` update the simulation and screen only when arrow keys (Bullet Time) is not wanted
phy update
endif
sync
loop
function createExplosion(x#,y#,z#,force#,size#)
sqrSize# = size#*size#
rem cycle through all objects to receive the force
for i=2 to numBoxes
if Object exist(i)=1
difx# = (object position x(i)-x#)
dify# = (object position y(i)-y#)
difz# = (object position z(i)-z#)
dist# = difx#*difx# + dify#*dify# + difz#*difz#
rem if close enough, apply a force relative to objects distance from the exlposion
if dist#<sqrSize#
dist# = sqrt(dist#)
forcex# = difx#/dist# * force#*1
forcey# = dify#/dist# * force#*1
forcez# = difz#/dist# * force#*1
phy add rigid body force i,forcex#,forcey#,forcez#, 1
endif
endif
next i
endfunction
Kinda Cool!
How it Works:
By managing WHEN we call Phy Update, we can have more influence when
DarkPhysics updates its physics world
If a key is pressed, pause Physx updates, and move camera with arrow keys by
Setting the phyflag=0 variable to not update the Physics simulation