News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
someone with mad math skills, help please
Author Message
Juergen
Member
***


Posts: 81
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #1
RE: someone with mad math skills, help please

It might be possible to tweak it a little, so that it works better, but the main problem will prevail. Such a simple simulation won't work very good with so many bodies and a random initial setup. At least not if you want to see anything with a decent machine.

Juergen

P.S.: For some reason I can't attach it.

So here it is:

Code:
demo_widget:color(0)
balls=34
bsize=10

function grav_loop()
local planet_new={},g,m,r,sqr_r,rx,ry,m_j
time_step=max_slider:value()
  for i=1,balls do -- for every planet
    g={0,0}
    m=planet[i][1] -- with mass m
    for j=1,balls do -- with every other planet
      if i~=j then -- planets do not influence itself
        rx=planet[i][2]-planet[j][2]
        ry=planet[i][3]-planet[j][3]
        sqr_r=rx*rx+ry*ry
        r=math.sqrt(sqr_r) -- distance
        m_j=planet[j][1]  -- mass of the second planet
        g[1]=g[1]+(r>1 and m_j/sqr_r*rx/r or m_j*rx) -- gravitational field value (acceleration of the mass for time_step)
        g[2]=g[2]+(r>1 and m_j/sqr_r*ry/r or m_j*rx) -- of course a vector, we also don't want to use too small r values --> planets normally have a radius. Also r=0 is forbidden.
     end
    end
    g[1]=g[1]*grav_const -- we take to liberty to change the gravitational constant.
    g[2]=g[2]*grav_const -- not possible in real life!
    planet_new[i]={planet[i][1], -- the mass doesn't change
                   planet[i][2]+planet[i][4]*time_step+g[1]*time_step*time_step*.5, -- x_1=x_0+v_0*t+g/2*t^2 which is the new position of the object
                   planet[i][3]+planet[i][5]*time_step+g[2]*time_step*time_step*.5, -- the same
                   planet[i][4]+g[1]*time_step, -- v_1=v_0+g*t , which is the new speed of the object
                   planet[i][5]+g[2]*time_step} -- of course the same
    ball[i]:position(planet_new[i][2]/scale,planet_new[i][3]/scale)
  end
planet=planet_new -- everything starts from the beginning
demo_widget:redraw()
grav_timer:doWait(.05)
end





fltk.fl_define_FL_PLASTIC_UP_BOX()
math.randomseed(os.time())
scale=1000
max_x=(demo_widget:w()-bsize)*scale
max_y=(demo_widget:h()-bsize)*scale
max_m=20000000
min_m=1000000
max_ivx=50
max_ivy=50
grav_const=-1
ball={}

planet={}
for i=1,balls do
-- planets[i]={mass,pos_x,pos_y,v_x,v_y}
planet[i]={math.random(min_m,max_m),math.random(1,max_x),math.random(1,max_y),math.random(-max_ivx,max_ivx),math.random(-max_ivy,max_ivy)}
ball[i]=fltk:Fl_Box(planet[i][2]/scale,planet[i][3]/scale,bsize,bsize)
if fltk._FL_PLASTIC_ROUND_UP_BOX then ball[i]:box(fltk._FL_PLASTIC_ROUND_UP_BOX) else ball[i]:box(fltk.FL_PLASTIC_ROUND_UP_BOX) end
ball[i]:color(planet[i][1]/max_m*255)
end

max_slider=fltk:Fl_Hor_Value_Slider(0,demo_widget:h()-20,demo_widget:w(),20)
max_slider:minimum(1)
max_slider:maximum(100)
max_slider:value(.1)
max_slider:step(.1)

grav_timer = murgaLua.createFltkTimer()
grav_timer:callback(grav_loop)
grav_timer:do_callback()

01-24-2008 08:31 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
RE: someone with mad math skills, help please - Juergen - 01-24-2008 08:31 AM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: