def display_at(date): # insert planets Sun = star(1.989 * math.pow(10, 30)) Earth = planet(5.972 * math.pow(10, 24), 149598262.00, 0.017, 147098291.00, 365.25636, Sun) Venus = planet(5.972 * math.pow(10, 24), 108209475.00, 0.007, 107476170.00, 224.7057127, Sun) Mercury = planet(5.972 * math.pow(10, 24), 57909227.00, 0.206, 46001009.00, 87.95373149, Sun) # Moon = satellite(7.34767 * math.pow(10, 22), 384400, Earth) celestpos = Jacob(date, list_of_planets) # makes the sun shine, vp.sphere(color=vp.color.yellow, emissive=True) vp.local_light(pos=vp.vector(0, 0, 0), color=vp.color.yellow) # creates the visual representation of the planets earth = vp.sphere(texture=vp.textures.earth, pos=vp.vector(celestpos[0] / 10000000, celestpos[1] / 10000000, celestpos[2] / 10000000), make_trail=True, trail_type="points", interval=10, retain=25) mars = vp.sphere(color=vp.vector(1, 0, 0), pos=vp.vector(celestpos[3] / 10000000, celestpos[4] / 10000000, celestpos[5] / 10000000), make_trail=True, trail_type="points", interval=10, retain=25)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Give the star a special texture and make it a light source self.visual.texture = "http://i.imgur.com/yoEzbtg.jpg" self.visual.emissive = True vis.local_light(pos = self.visual.pos, color = vec(1, 1, 1))
def display(): vp.scene.append_to_title('\n') vp.button(text="credits", bind=showat, pos=vp.scene.title_anchor) vp.scene.append_to_caption('\n') vp.button(text="Pause", bind=Pause) vp.scene.append_to_caption('\n') t = vp.slider(min=-20000, max=20000, value=1, length=675, bind=setyear) vp.scene.append_to_caption('\n') print("B") celestpos = Jacob(t.value) # makes the sun shine vp.sphere(color=vp.color.yellow, emissive=True) vp.local_light(pos=vp.vector(0, 0, 0), color=vp.color.yellow) # creates the visual representation of the planets earth = vp.sphere(texture=vp.textures.earth, pos=vp.vector(celestpos[0] / 10000000, celestpos[1] / 10000000, celestpos[2] / 10000000), make_trail=True, trail_type="points", interval=10, retain=25) mars = vp.sphere(color=vp.vector(1, 0, 0), pos=vp.vector(celestpos[3] / 10000000, celestpos[4] / 10000000, celestpos[5] / 10000000), make_trail=True, trail_type="points", interval=10, retain=25) # venus = vp.sphere(color = vp.vector(1,1,.8), pos = vp.vector(celestpos[0] / 10000000,celestpos[0] / 10000000,celestpos[0] / 10000000), make_trail=True, trail_type="points", interval=10, retain=10) # mercury = vp.sphere(color = vp.vector(.3,.3,.3), pos = vp.vector(celestpos[0] / 10000000,celestpos[0] / 10000000,celestpos[0] / 10000000), make_trail=True, trail_type="points", interval=10, retain=5) # moon = vp.sphere(color = vp.vector(.3,.3,.3), pos = vp.vector(Moon.xPos / 10000000, Moon.yPos / 10000000,0)) vp.scene.append_to_caption('\n') sl = vp.slider(min=-20, max=20, value=1, length=675, bind=setspeed) while True: while pause == False: celestpos = Jacob(sl.value) # simulates motion # Moon.simulate_orbit(i, 0) # Earth.simulate_orbit(t, 0) # Venus.simulate_orbit(t, 0) # Mercury.simulate_orbit(t, 0) earth.pos = vp.vector(celestpos[0] / 10000000, celestpos[1] / 10000000, celestpos[2] / 10000000) mars.pos = vp.vector(celestpos[3] / 10000000, celestpos[4] / 10000000, celestpos[5] / 10000000) # venus.pos = vp.vector(Venus.xPos / 10000000,Venus.yPos / 10000000,0) # mercury.pos = vp.vector(Mercury.xPos / 10000000, Mercury.yPos / 10000000,0) # moon.pos = vp.vector(Moon.xPos / 10000000, Moon.yPos / 10000000,0) if (sl.value > 0): t.value += 1 time.sleep(0.01) else: t.value -= 1 time.sleep(.01)
def main(): prepare(3 * distance) context = Bunch( time=3/4*moon_day, life_time_step=0.01, live=False, lights_on=True, original_lights=scene.lights[:] ) earth = sphere(pos=vec(0, 0, 0), size=earth_radius * vec(1, 1, 1)) earth.shininess = 0 earth.texture = {'file': textures.earth} earth.rotate(earth_tilt, axis=vec(0, 0, 1)) moon = sphere(pos=vec(0, 0, 0), size=moon_radius * vec(1, 1, 1)) moon.shininess = 1 moon.texture = {'file': textures.moon} sun_x = 40 sun = sphere(pos=vec(sun_x, 0, 0), size=5 * vec(1, 1, 1), emissive=True) sun.texture = {'file': textures.sun} local_light(pos=sun.pos, color=color.red) local_light(pos=sun.pos + vec(-10, 0, 0), color=color.white) scene.bind('keydown', partial(control_by_key, context)) earth_rotate = 0 moon_rotate = 0 while True: rate(50) if context.live: context.time += context.life_time_step earth.rotate(-earth_tilt, axis=vec(0, 0, 1)) earth.rotate(-earth_rotate, axis=vec(0, 1, 0)) earth_rotate = 2 * pi * context.time earth.rotate(earth_rotate, axis=vec(0, 1, 0)) earth.rotate(earth_tilt, axis=vec(0, 0, 1)) moon.rotate(-moon_rotate, axis=vec(0, 1, 0)) moon_angle = -2 * pi * context.time / moon_day moon.pos = vec(distance * cos(moon_angle), 0, distance * sin(moon_angle)) moon_rotate = -pi/2 + 2*pi * context.time / moon_day moon.rotate(moon_rotate, axis=vec(0, 1, 0))
def run(self): atoms = [] for x in range(500): atom = Atom(f"H{x}") atom.size = random.randint(1, 100) atom.direction = self._rand_direction() atom.speed = random.randint(1, 10) atom.position = self.rand_coordinate() agent = AtomDrawerBox(atom) agent.draw() atoms.append(agent) scene.autoscale = False scene.width = 1200 scene.height = 800 lamp = local_light(pos=atoms[0].get_vector(), color=color.yellow) atoms[0].object.emissive = True atoms[0].object.color = color.white while True: rate(60) for atom in atoms: atom.update() lamp.pos = atoms[0].get_vector()
def main(): scene = vp.canvas(width=700, height=700, center=vp.vector(0, 0, 0)) scene.autoscale = False # Read galactic rotation file radius_data, velocity_data = np.loadtxt(rc_filename, unpack=True) radius_data = radius_data * 3.086e+16 # kpc to km # Interpolate the rotation curve; callable function rc_func = interpolate.interp1d(radius_data, velocity_data) # Plot RC data fig3 = plt.figure(figsize=(10, 5)) ax3 = fig3.add_subplot(111) ax3.plot(radius_data, velocity_data, 'o', color='blue') # Plot RC function rc_func_x = np.linspace(radius_data[0], radius_data[-1], 1000) rc_func_y = rc_func(rc_func_x) ax3.plot(rc_func_x, rc_func_y, '-', color='red') plt.show() scene.camera.pos = vp.vector(0, 0, radius_data[-1] * 1.5) scene.camera.axis = vp.vector(vp.vector(0, 0, 0) - scene.camera.pos) vp.local_light(pos=vp.vector(0, 0, 0), color=vp.color.blue) # Initialize all objects star_list = init_system(radius_data, velocity_data, rc_func) ''' Animation ''' if run_animation == True: i = 0 while True: vp.rate(1e10) for star in star_list: star.motion() if i % 100 == 0: print( 'Star positions update completed - Running next iteration') i += 1
def go(): r = np.array([1.017, 0.0]) # initial x,y position for earth v = np.array([0.0, 6.179]) # initial vx, vy # draw the scene, planet earth/path, sun/sunlight #scene = vp.canvas(title='Planetary motion', background=vec(.2,.5,1), forward=vec(0,2,-1)) planet = vp.sphere(pos=vec(r[0], r[1], 0), radius=0.1, make_trail=True, up=vec(0, 0, 1)) sun = vp.sphere(pos=vec(0, 0, 0), radius=0.2, color=vp.color.yellow) sunlight = vp.local_light(pos=vec(0, 0, 0), color=vp.color.yellow) #scn end t, h = 0.0, 0.001 while True: vp.rate(200) # limit animation speed r, v = leapfrog(earth, r, v, t, h) # integrate planet.pos = vec(r[0], r[1], 0) # move planet
def make_orbit_scene(solar_sys, satellite): rsun = solar_sys['sun_radius'] rearth = solar_sys['earth_radius'] psun = solar_sys['sun_dist'] * n.array(solar_sys['sun_vector']) pearth = (0, 0, 0) altitude = satellite.orbit['altitude'] psat = (0, 0, rearth + altitude) size_sat = satellite.properties['sizefactor'] up_sat = satellite.starting_orientation[4] scene = canvas() sun = sphere(pos=vecflip(psun, solar_sys['flip_o']), radius=rsun, emissive=True, color=color.yellow) earth = sphere(pos=vecflip(pearth, solar_sys['flip_o']), radius=rearth, color=color.green, make_trail=False) scene.lights = [] sunlight = local_light(pos=vecflip(psun, solar_sys['flip_o']), color=color.gray(0.4)) scene.ambient = color.gray(0.5) sat = box(pos=vecflip(psat, solar_sys['flip_o']), length=0.1 * size_sat, width=0.1 * size_sat, height=0.3 * size_sat, up=vecflip(up_sat, solar_sys['flip_o']), make_trail=True) solar_sys['sun'] = sun solar_sys['earth'] = earth satellite.sat3d = sat satellite.zrot = 0 return scene, solar_sys, satellite
def make_view_scene(solar_sys, satellite): psun = (0, solar_sys['sun_dist'], 0) up_sat = satellite.starting_orientation[4] sat_view_scene = canvas() sat_view = box(pos=vecflip((0, 0, 0), flip=solar_sys['flip_v']), width=0.1, length=0.1, height=0.3, up=vecflip(up_sat), make_trail=True) sat_view_scene.lights = [] sat_view_scene.ambient = color.gray(0.3) sun_view = local_light(pos=vecflip(psun, flip=solar_sys['flip_v']), color=color.gray(0.4)) # sat_view.axis = vecflip(sat_axis) solar_sys['sun_v'] = sun_view satellite.sat_v3d = sat_view return sat_view_scene, solar_sys, satellite
def __make_light(self): self.light = local_light(pos=self.pos, color=self.light_color)
import vpython as vp from numpy import pi scene = vp.canvas(title='Paha paikka', x=0, y=0, width=1280, height=768, center=vp.vector(0, 0, 0), background=vp.vector(0, 0, 0)) # Reset lightning scene.lights = [] vp.distant_light(direction=vp.vec(0.0, 1, 0.0), color=vp.vector(1, 1, 1)) vp.local_light(pos=vp.vec(0.0, 700, 0.0), color=vp.vector(1, 1, 1)) ## Create table #table = vp.box(pos=vp.vec(0,-15,0) # , length = 1400 # , height = 10 # , width = 1400 # , color=vp.vec(0,103/255,1/255) # , texture={'file':'./snooker_texture.jpg','place':'all'} # ) #table.shininess=10000 def draw_ball(pt, radius, color): vp.sphere(pos=vp.vector(pt[0], 0, pt[1]), radius=radius, color=vp.vector(color[0], color[1], color[2]))
dateErrorText = vp.wtext(text = "\n") vp.wtext(text = "\nThis is the current date: \n") vp.wtext(text = "-----------------\n") guidate = vp.wtext(text=" ") guidate.text = "| " + guiday + "/" + guimonth + "/" + guiyear + " |\n" vp.wtext(text = "-----------------\n") vp.wtext(text = "\nControl the runtime using the buttons and slider below: \n") ################################################################################ vp.button(text="Pause", bind=Pause) vp.button(text="Play", bind=Play) vp.button(text="Reset", bind=Reset) # makes the sun shine vp.sphere(color=vp.color.yellow, emissive=True, radius = 1) vp.local_light(pos=vp.vector(0, 0, 0), color=vp.color.yellow) ############## Setting body models initial values ########################## for planet in list_of_planets: planet.simulate_orbit(0) list_of_planet_models.append(vp.sphere(textures=vp.textures.earth, pos=vp.vector(planet.xPos / 10000000, planet.yPos / 10000000, planet.zPos / 10000000), make_trail=True, trail_type="curve", interval=10, retain=50, radius = 0.1)) list_of_planet_names.append(vp.label(text = planet.name, pos = list_of_planet_models[list_of_planets.index(planet)].pos, height = 15, yoffset = 50, space = 30, border = 4, font = 'sans')) for satellite in list_of_satellites: for planet in list_of_planets: if satellite.host == planet.name: satellite.simulate_orbit(0, planet) list_of_satellite_models.append(vp.sphere(textures=vp.textures.earth, pos=vp.vector(satellite.xPos / 10000000, satellite.yPos / 10000000, satellite.zPos / 10000000), make_trail=False, radius = 0.03)) list_of_satellite_names.append(vp.label(text = satellite.name, pos = list_of_satellite_models[list_of_satellites.index(satellite)].pos, height = 15, yoffset = 50, space = 30, border = 4, font = 'sans'))
axis=vp.vector(1, 0, 0), shaftwidth=.1, color=vp.color.red) vp.arrow(pos=vp.vector(0, 0, 0), axis=vp.vector(0, 1, 0), shaftwidth=.1, color=vp.color.green) vp.arrow(pos=vp.vector(0, 0, 0), axis=vp.vector(0, 0, 1), shaftwidth=.1, color=vp.color.blue) vp.label(pos=vp.vector(6, 0, z1), text='RPV Top 0') vp.label(pos=vp.vector(0, 6, z1), text='90') vp.label(pos=vp.vector(-6 * 4, 0, z1), text='180') vp.label(pos=vp.vector(0, -6 * 4, z1), text='270') vp.local_light(pos=vp.vector(0, 0, -10), color=vp.color.gray(0.2)) for j in range(len(x)): pt = array([x[j], y[j], z[j]]) if j != 0: pt_old = array([x[j - 1], y[j - 1], z[j - 1]]) temp = c_vel(pt_old, pt) else: temp = array([0, 0, 0]) # Calculate position of j2, j3, j4, end effector j2, j3, j4, pvt, ee = c_jpos(pt) vp.curve(pos=[(0, 0, 0), (j2[0], j2[1], j2[2]), (j3[0], j3[1], j3[2]), (j4[0], j4[1], j4[2]), (pvt[0], pvt[1], pvt[2]), (ee[0], ee[1], ee[2])], radius=0.01)
self.v = self.v + ((Ftotal / self.mass) * dt) self.p = self.v * self.mass self.planet.pos = self.planet.pos + (self.v * dt) self.label.pos = self.planet.pos self.label.text = self.name + "\n" + str(int(mag(self.v))) + "m/s" return solarObjects dt = 1 orbitalParent = Planet(pos=vector(0, 0, 0), mass=1e18, orbitalParentmass=0, orbitalParentpos=vector(1, 1, 1), orbitalParentv=vector(0, 0, 0)) orbitalParent.planet.emissive = True bigLight = local_light(pos=orbitalParent.planet.pos, color=orbitalParent.planet.color) solarObjects = [orbitalParent] radiusScale = 4 orbitalParent.v = vector(0, 0, 0) ct = 0 ctr = 0 while True: rate(200) ct += dt scene.title = "Time since last collision: " + str( ct) + "s\n" + "Record: " + str(ctr) + "s" if ct > ctr: ctr = ct scene.camera.follow(orbitalParent.planet) bigLight.pos = orbitalParent.planet.pos #orbitalParent.v = vector(0,0,0)
def _create_obj(self, entity: Entity, origin: Entity, texture: Optional[str]) -> vpython.sphere: obj = super(Star, self)._create_obj(entity, origin, texture) obj.emissive = True # The sun glows! self._light = vpython.local_light(pos=obj.pos) return obj
Nasteroids = 1000 # Number of random asteroids RandomA = False # If false, the semi-major axes for all asteroids are evenly distributed, instead of random rpos = 12 # Start angle for orbital position, in degrees ('None' for random positions) amin, amax = 3.0, 5.5 # bounds for semi-major axis distribution emin, emax = 0.0, 0.00 # Bounds for eccentricity variation (zero for circular orbits) Imin, Imax = -0.0, 0.0 # Bounds for inclination angle variation, in degrees (zero for orbits in the plane of the ecliptic) ############################ Set up display window and lighting #################################### win = 800 # Display window size, in pixels scene = vpython.canvas(title="Orbit", width=win, height=win) # Create window scene.lights = [] # Dump default scene lighting scene.ambient = vpython.color.gray(0.3) # Low level ambient lighting sunlight = vpython.local_light(pos=v( 0, 0, 0), color=vpython.color.white) # Sunlight from centre ########################### Class and function definitions ############################################# class Body(planephem.Planet): """Subclass the 'Planet' object in the ephemeris library to add methods for setting up the 3D Visual Python objects to display it. """ def __init__(self, *args, **keywords): planephem.Planet.__init__(self, *args, **keywords) self.color = vpython.color.white self.dradius = 1.0 self.texture = None self.rings = False self.up = (0, 0, 1) self.body = None