Python Forum
Using an Array in a While Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using an Array in a While Loop
#8
(Jun-14-2017, 04:45 AM)Skaperen Wrote: there should be exactly one list (or object of whatever type) that contains all system objects/bodies. the sun and mercury should not be any different than the others although they will certainly have different mass, position, velocity, colour and name.
yes, I was thinking the same

from visual import * # I HATE this

myuniv = display(range=vector(7*10**12,7*10**12,7*10**12))

G = 6.674*10**-11  #Gravitational constant

# define objects
cosmic_objects = (('sun', {'r':9.9*10**10, 'mass':1.989*10**30, 'pos':vector(0,0,0), 'vel':vector(0,0,0), 'acc':vector(0,0,0), 'color':color.yellow}),
           ('mercury', {'r':2.44*10**6, 'mass':3.285*10**23,'pos':vector(5.791*10**10,0,0), 'vel': vector(0,4.87*10**4,0), 'acc':vector(0,0,0), 'color':color.red}),
           ('venus', {'r':6.052*10**6, 'mass':4.867*10**24, 'pos':vector(1.082*10**11,0,0), 'vel':vector(0,3.502*10**4,0), 'acc':vector(0,0,0), 'color':color.yellow}), # I added color here
           ('earth', {'r':6.371*10**6, 'mass':5.972*10**24, 'pos':vector(1.496*10**11,0,0), 'vel':vector(0,2.98*10**4,0), 'acc':vector(0,0,0), 'color':color.green}),
           ('mars', {'r':3.39*10**6, 'mass':6.39*10**23, 'pos':vector(2.279*10**11,0,0), 'vel':vector(0,2.401*10**4,0), 'acc':vector(0,0,0), 'color':color.red}),
           ('jupiter', {'r':6.99*10**7, 'mass':1.898*10**27, 'pos':vector(7.785*10**11,0,0), 'vel':vector(0,1.307*10**4,0), 'acc':vector(0,0,0), 'color':color.blue}),
           ('saturn', {'r':5.823*10**7, 'mass':5.68*10**26, 'pos':vector(1.429*10**12,0,0), 'vel':vector(0,9.69*10**3,0), 'acc':vector(0,0,0), 'color':color.yellow}),
           ('uranus', {'r':2.536*10**7, 'mass':8.68*10**25, 'pos':vector(2.871*10**12,0,0), 'vel':vector(0,6.81*10**3,0), 'acc':vector(0,0,0), 'color':color.cyan}),
           ('neptune', {'r':2.462*10**7, 'mass':1.024*10**26, 'pos':vector(4.498*10**12,0,00), 'vel':vector(0,5.43*10**3,0), 'acc':vector(0,0,0), 'color':color.blue})) # I added color here


#visualize objects
solar_system = {}
for name, obj in cosmic_objects:
    solar_system[name] = sphere(pos=obj['pos'], radius=obj['r'], color=obj['color'], make_trail=True)

t = 0.0
dt = 150000

while True:
    for pla_name, planet in cosmic_objects[1:]: # just the planets
        for obj_name, obj in sorted(cosmic_objects, key=lambda x:x[1]['mass'], reverse=True): # loop ove all objects, sorted by mass descending
            rate(1000000)
            t=t+dt
            if obj_name != pl_name: # check that obj and planet are no the same cosmic object
                r =  obj['pos'] - planet['pos'] # distance between obj and planet
                Fnet= ((G*obj['mass']*planet['mass'])/(mag(r)**2))* norm(r) #Calculating the force of gravity from the obj
                planet['acc'] = Fnet/planet['mass']
                planet['vel'] += planet['acc']*dt
                planet['pos'] += planet['vel']*dt
                solar_system[pl_name].pos = planet['pos']
Reply


Messages In This Thread
Using an Array in a While Loop - by JakeWitten - Jun-13-2017, 01:28 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-13-2017, 04:47 AM
RE: Using an Array in a While Loop - by j.crater - Jun-13-2017, 06:01 AM
RE: Using an Array in a While Loop - by buran - Jun-13-2017, 08:48 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-13-2017, 10:24 PM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 04:12 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-14-2017, 04:45 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 05:06 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 05:18 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-14-2017, 05:29 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 05:46 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 05:52 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 07:05 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 08:23 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 08:44 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 10:03 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 10:36 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 10:40 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 10:58 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 11:07 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 11:17 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 11:45 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 03:17 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020