Nothing DarkPhysics related.
The variable
Cube in the function is 0 since it is a local variable. Either set it as a global variable:
Phy Set Ground Plane 1
Sync On
Sync Rate 60
Phy Start
Autocam Off
global Cube = 2
Position Camera 100,50,100
Point Camera 25,0,25
`Create Cubes
For X = 1 to 8
For Y = 1 to 4
For Z = 1 to 8
Make Object Cube Cube,2.5
Position Object Cube,X*8,Y*8,Z*8
Phy Make Rigid Body Dynamic Box Cube
Inc Cube,1
Next Z
Next Y
Next X
Do
If SpaceKey() = 1
createExplosion(50,5,50,1000,25)
Endif
Phy Update
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 Cube-1
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
or pass it to the function:
Phy Set Ground Plane 1
Sync On
Sync Rate 60
Phy Start
Autocam Off
Cube = 2
Position Camera 100,50,100
Point Camera 25,0,25
`Create Cubes
For X = 1 to 8
For Y = 1 to 4
For Z = 1 to 8
Make Object Cube Cube,2.5
Position Object Cube,X*8,Y*8,Z*8
Phy Make Rigid Body Dynamic Box Cube
Inc Cube,1
Next Z
Next Y
Next X
Do
If SpaceKey() = 1
createExplosion(50,5,50,1000,25,Cube)
Endif
Phy Update
Sync
Loop
function createExplosion(x#,y#,z#,force#,size#,Cube)
sqrSize# = size#*size#
rem cycle through all objects to receive the force
for i=2 to Cube-1
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