This should make things better...
+I've Translated the comments to english
+There are still some problems with the random generator, but it's working much better now
REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART
-------------------------------- www.english.pharosfx.com -------------------------------
___
/ / / _ __ __ __ __ _ _
/__/ /_ __/ /_/ / / /_ /_ /
/ / / /_/ / /_/ __/ / _/_
...presents...
------------------------------------= Gravitation/Eletromag =----------------------------
This is a bidimensional example on how to implement a gravitational or electromagnetic forcefield.
Background Requirements:
-Little NOTION of what is a vector
What I'll try to teach here :
+How to program dynamics
----------------------------------------------------------------------------------------
REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMEND
Rem Setup
sync on :`make matrix 1,1000,1000,10,10: position matrix 1,-500,0,-500
color backdrop 0 : randomize timer()
Rem This is a 2d vector type
type vetor2
x as float
z as float
endtype
Rem G is our own gravitational constant
G as float : G = 0.2
Rem This is the number of bodies in the simulation
Numero_de_corpos = 100
Rem Bodie type,
type corpo
p as vetor2:rem position
v as vetor2:rem velocity
a as vetor2:rem acceleration
f as vetor2:rem Force, the sum of all forces applied to the object
massa as float:Rem mass
objeto as integer:Rem object number
endtype
Rem Make an array of bodies
dim corpos(Numero_de_corpos) as corpo
`remstart
`---------------------------------------
`ADD YOUR SYSTEM GENERATION CODE BELLOW
`---------------------------------------
for bd = 1 to Numero_de_corpos
size =10+ rnd(40)
make object sphere bd , size
corpos(bd).objeto = bd
corpos(bd).p.x = rnd(1000)-500
corpos(bd).p.z = rnd(1000)-500
corpos(bd).v.x = (rnd(10)/100.0)
corpos(bd).v.z = (rnd(10)/100.0)
corpos(bd).massa = size*4
next bd
`---------------------------------------
`ENDOFGENERATION
`---------------------------------------
position camera 0,1000,0
dx#=90.0
`Main loop
do
set cursor 0,0
`Calculating all forces between the objects
for t = 1 to Numero_de_corpos
`We start by 'zero'ing the forces on the current obj
corpos(t).f.x = 0 :corpos(t).f.z = 0
`All objects interact with one another
for o = 1 to Numero_de_corpos
Rem object does not interact with self (t <> o)
if t <> o and (object visible(corpos(o).objeto) )
modulo as float :rem the force's absolute value
distquad as float:rem distance squared
angulo as float:rem angle between the objects
distquad = (corpos(t).p.x - corpos(o).p.x)*(corpos(t).p.x - corpos(o).p.x) + (corpos(t).p.z - corpos(o).p.z)*(corpos(t).p.z - corpos(o).p.z)
rem the atractive force between two objects is equal to the gravitational constant * the mass of the body * the mass of the other body / by the distance squared...
modulo = (G * corpos(t).massa * corpos(o).massa) / distquad
`This is what is like when worlds COLLIDE!!!
Rem We can't let the objects overlap, the attractive forces would ... Just think of fusion...
if sqrt(distquad )< object collision radius( corpos(t).objeto) + object collision radius( corpos(o).objeto)
rem if the current body crashed with one smaller then itself
if corpos(t).massa >= corpos(o).massa
`Concervation of linear momentum:
corpos(t).v.x = corpos(t).v.x + (corpos(o).v.x * corpos(o).massa /corpos(t).massa )
corpos(t).v.z = corpos(t).v.z + (corpos(o).v.z * corpos(o).massa /corpos(t).massa )
rem other obj is 'glued' to current
corpos(t).massa = corpos(t).massa + corpos(o).massa
corpos(o).massa = 0
hide object corpos(o).objeto
modulo = 0
endif
endif
`find the angle between the objects
angulo = atanfull(corpos(o).p.z - corpos(t).p.z,corpos(o).p.x - corpos(t).p.x)
`find the force's components in x and z (* cos,*sin)
corpos(t).f.x = corpos(t).f.x + cos(angulo)* modulo
corpos(t).f.z = corpos(t).f.z + sin(angulo)* modulo
endif
next o
next t
Rem we cannot use the same for;next for the positioning, it would be physically inaccurate
for t = 1 to Numero_de_corpos
`Force = Mass * Acceleration
corpos(t).a.x = corpos(t).f.x/ corpos(t).massa
corpos(t).a.z = corpos(t).f.z/ corpos(t).massa
`add acc to velocity
corpos(t).v.x = corpos(t).v.x + corpos(t).a.x
corpos(t).v.z = corpos(t).v.z + corpos(t).a.z
`add velocity to position
corpos(t).p.x = corpos(t).p.x +corpos(t).v.x
corpos(t).p.z = corpos(t).p.z+ corpos(t).v.z
`Posicionar o objeto
position object corpos(t).objeto,corpos(t).p.x,0,corpos(t).p.z
next t
`cheap camera system...
speed# = 1
if mouseclick()=1 then Move camera speed#
if mouseclick()=2 Then Move camera -speed#
dx#=dx#+mousemovey()/4 : cy#=wrapvalue(cy#+mousemovex()*0.2)
if dx#>90 then dx#=90
if dx#<-90 then dx#=-90
cx#=wrapvalue(dx#)
` Finally, rotate the camera to set his final position
Rotate camera cx#, cy#, cz#
if spacekey() then input "",obnb :position camera object position x(obnb),camera position Y(),object position z(obnb)
sync
loop
EDITED: fixed a few things
EDITED: As you might have noticed mass does attract mass and this has some interesting effects depending on the initial state of the system
Generator 1
for bd = 1 to Numero_de_corpos/10
size =10+ rnd(20)
make object sphere bd , size
corpos(bd).objeto = bd
corpos(bd).p.x = rnd(1000)-500
corpos(bd).p.z = rnd(1000)-500
corpos(bd).v.x = 0:`(rnd(10)/100.0)
corpos(bd).v.z = 0:`(rnd(10)/100.0)
corpos(bd).massa = (size/2)*(size/2)
next bd
for bd = Numero_de_corpos/10+1 to Numero_de_corpos
size =rnd(5)+1
make object sphere bd , size
corpos(bd).objeto = bd
corpos(bd).p.x = rnd(1000)-500
corpos(bd).p.z = rnd(1000)-500
corpos(bd).v.x = 0:`(rnd(10)/100.0)
corpos(bd).v.z = 0:`(rnd(10)/100.0)
corpos(bd).massa = (size/2)*(size/2)
next bd
Solar System Example:
A much more familiar picture of gravitation you can see by adding a small number(only one here) of HEAVY bodies
`Solar System
G = 0.003
size as float
for bd = 1 to 1
corpos(bd).objeto = bd
size = 10
make object sphere bd , size
corpos(bd).massa = size^5
next bd
for bd = 2 to Numero_de_corpos
corpos(bd).objeto = bd
size = 2+rnd(5)
make object sphere bd , size
corpos(bd).v.x = -sin(bd*360.0/(Numero_de_corpos))
corpos(bd).v.z = cos(bd*360.0/(Numero_de_corpos))
corpos(bd).p.x = 100*cos(bd*360.0/(Numero_de_corpos))*(rnd(2.0)+1.0)
corpos(bd).p.z = 100*sin(bd*360.0/(Numero_de_corpos))*(rnd(2.0)+1.0)
corpos(bd).massa = (size/2)*(size/2)
next bd
Now it's your turn, post your cool system configuration