I have no clue what you just did with that code...I tried running it and all I saw was a black screen with the fps in the top left corner. But how the spheres are attracted to eachother is it gets their x speed, and adds the difference of their x position, and multiplies it by 1400-their x distnace. Then it divides that by the attraction amount, then multiplies by the other objects mass. Well thats what this updated code does. Its hard to understand if you don't know what the Universal Law of Gravity is, or how it works.
(universal gravitation constant =(6.6742 ± 0.0010) × 10^−11 N m^2 kg^-2)
Well, here is the new code. It's a little slower, but their distances now have a little bit of an affect.
REM Project: Physics-Spheres
REM Created: 6/19/2005 10:55:44 PM
REM
REM ***** Main Source File *****
REM
`Just the beginning stuff
sync on : sync rate 100
autocam off : backdrop on : color backdrop rgb(0,0,0) : set camera fov 30 : set camera range .01,100000
randomize timer()
`Variable Type
type vars
x#
z#
xspeed#
zspeed#
mass
alive
endtype
position camera 0,2000,0
xrotate camera 90
global randomy=1050
global randomx=1400
global dead=0
global last=0
global check=0
global replay=0
global attraction=100000000
global dim sphere(100) as vars
`This just makes and places the objects
for a=1 to 100
make object sphere a,10
position object a,rnd(randomx)-int(randomx/2),0,rnd(randomy)-int(randomy/2)
sphere(a).x#=object position x(a)
sphere(a).z#=object position z(a)
sphere(a).xspeed#=rnd(10)-5
sphere(a).zspeed#=rnd(10)-5
sphere(a).mass=10
set object collision on a
set object collision to spheres a
sphere(a).alive=1
next a
do
`This just stores the positions so you don't have to keep rechecking
for a=1 to 100
sphere(a).x#=object position x(a)
sphere(a).z#=object position z(a)
next a
`This is how the objects attract to one another. If you decrease attraction, your objects
`go towards eachother way to fast. You could just copy and past the remmed code
`but it will slow way down
for a=1 to 100
for b=1 to 100
if ((b>a or b<a) and sphere(b).alive=1)
`*((7500-(sqrt((sphere(1).x#-sphere(2).x#)^2+(sphere(1).z#-sphere(2).z#)^2)))/7500)
sphere(a).xspeed#=sphere(a).xspeed#+(((((1400-sphere(b).x#-sphere(a).x#)*(sphere(b).x#-sphere(a).x#)*.01)/attraction)*sphere(b).mass))
sphere(a).zspeed#=sphere(a).zspeed#+(((((1050-sphere(b).z#-sphere(a).z#)*(sphere(b).z#-sphere(a).z#)*.01)/attraction)*sphere(b).mass))
endif
next b
next a
`Simple movement
for a=1 to 100
if (sphere(a).alive=1)
position object a,sphere(a).x#+sphere(a).xspeed#,0,sphere(a).z#+sphere(a).zspeed#+0.0
endif
next a
`You could really do without this, it just kinda makes the next piece of code a little more
`up to date though
for a=1 to 100
if (sphere(a).alive=1)
sphere(a).x#=object position x(a)
sphere(a).z#=object position z(a)
endif
next a
`This is the code we were talking about above. Just incase the objects go outside of the
`bounds, this reverses their movement so they come back in. You could see what the program
remstart does without it...
remend
for a=1 to 100
if (sphere(a).alive=1)
if (sphere(a).x#>700 or sphere(a).x#<-700) THEN sphere(a).xspeed#=sphere(a).xspeed#*-1
if (sphere(a).z#>525 or sphere(a).z#<-525) THEN sphere(a).zspeed#=sphere(a).zspeed#*-1
endif
next a
`This is the collision testing. The object with the bigger mass is the object which will
`stay alive
for a=1 to 100
for b=1 to 100
if (b>a or b<a)
if (object collision (a,b))
if (sphere(a).mass>sphere(b).mass)
hide object b
set object collision off b
sphere(b).alive=0
dead=dead+1
sphere(a).mass=sphere(a).mass+(sphere(b).mass)
scale object a,sphere(a).mass+100,sphere(a).mass+100,sphere(a).mass+100
position object a,(sphere(a).x#+sphere(b).x#)/2,0,(sphere(a).z#+sphere(b).z#)/2
sphere(a).xspeed#=(sphere(a).xspeed#+sphere(b).xspeed#)/2
sphere(a).zspeed#=(sphere(a).zspeed#+sphere(b).zspeed#)/2
else
hide object a
set object collision off a
sphere(a).alive=0
dead=dead+1
sphere(b).mass=sphere(b).mass+(sphere(a).mass)
scale object b,sphere(b).mass+100,sphere(b).mass+100,sphere(b).mass+100
position object b,(sphere(a).x#+sphere(b).x#)/2,0,(sphere(a).z#+sphere(b).z#)/2
sphere(b).xspeed#=(sphere(a).xspeed#+sphere(b).xspeed#)/2
sphere(b).zspeed#=(sphere(a).zspeed#+sphere(b).zspeed#)/2
endif
endif
endif
next b
next a
`If only 1 ball remains, then it resets just about everything...except for the collision
`because if you re-did that right now, the balls would already be touching due to their
`placement when we make them explode, and then we would be screwed
if (dead=99)
while (last=0)
check=check+1
if (sphere(check).alive=1) THEN last=check
endwhile
for a=1 to 100
sphere(a).x#=sphere(check).x#
sphere(a).z#=sphere(check).z#
position object a,sphere(a).x#,0,sphere(a).z#
sphere(a).alive=1
sphere(a).mass=10
sphere(a).xspeed#=rnd(20)-10
sphere(a).zspeed#=rnd(20)-10
scale object a,100,100,100
show object a
next a
replay=1
dead=0
last=0
check=0
endif
`This gives your program about 1 second for the balls to go flying before they can interact
`with eachother really.
if (replay=1)
if (check=100)
for b=1 to 100
set object collision on b
next b
check=0
replay=0
else
check=check+1
endif
endif
set cursor 0,0
print screen fps()
`THE END!!!:D
sync : loop