usingMakieusingAbstractPlotting:textsliderusingGeometryTypes,FileIOusingLinearAlgebra"""
example by @pbouffard from JuliaPlots/Makie.jl#307
https://github.com/pbouffard/miniature-garbanzo/
"""constrearth_km=6371.1f0constmearth_kg=5.972e24constrmoon_km=1738.1f0constmmoon_kg=7.34e22constrship_km=500f0constmship_kg=1000.0constdbetween_km=378_000.0f0constradius_mult=1.0f0consttimestep=1.0f0mutable structShipmass_kg::Float32position_m::Vec3f0velocity_mps::Vec3f0color::Symbolmesh::Meshendfunctionmoveto!(ship::Ship,(x,y,z))ship.position_m=Vec3f0(x,y,z)translate!(ship.mesh,x,y,z)ship.position_m,ship.velocity_mpsendfunctionorbit(r;steps=80)return[Point3f0(r*cos(x),r*sin(x),0)forxinrange(0,stop=2pi,length=steps)]endfunctionloadordownload(localfname,url)returnisfile(localfname)?load(localfname):load(download(url,localfname))end# http://corysimon.github.io/articles/uniformdistn-on-sphere/functionmakestars(n)returnmap(1:n)doiv=[0,0,0]# initialize so we go into the while loopwhilenorm(v)<.0001v=randn(Point3f0)endv=v/norm(v)# normalize to unit normvendendfunctionmakecontrols()orbit_slider,orbit_speed=textslider(0f0:10f0,"Speed",start=1.0)scale_slider,scale=textslider(1f0:20f0,"Scale",start=10.0)returnorbit_slider,orbit_speed,scale_slider,scaleendfunctionmakeships(scene,N)returnmap(1:N)doism=mesh!(scene,Sphere(Point3f0(0),rship_km*radius_mult),color=:green,show_axis=false)[end]ship=Ship(mship_kg,[0,0,0],[0,0,0],:green,sm)moveto!(ship,(100000f0*randn(Float32),dbetween_km/2+randn(Float32)*50000f0,50000f0*randn(Float32)))ship.velocity_mps=[40000*randn(Float32),-1000*randn(Float32),1000*randn(Float32)]shipendendfunctionmakeobjects(scene)earthfname="bluemarble-2048.png"earthurl="https://svs.gsfc.nasa.gov/vis/a000000/a002900/a002915/"*earthfnameearthbitmap=loadordownload(earthfname,earthurl)earth=mesh!(scene,Sphere(Point3f0(0),rearth_km*radius_mult),color=earthbitmap)[end]moonfname="phases.0001_print.jpg"moonurl="https://svs.gsfc.nasa.gov/vis/a000000/a004600/a004675/"*moonfnamemoonbitmap=loadordownload(moonfname,moonurl)moon=mesh!(scene,Sphere(Point3f0(0),rmoon_km*radius_mult),color=moonbitmap)[end]translate!(moon,Vec3f0(0,dbetween_km,0))orb=lines!(scene,orbit(dbetween_km),color=:gray)# stars = scatter!(1000000*makestars(1000), color=:white, markersize=2000)returnearth,moon#, stars#, shipsendfunctionspin(scene,orbit_speed,scale,moon,earth,ships)center!(scene)# update far / near automatically.# could also be done manually:# cam.near[] = 100# cam.far[] = 1.5*dbetween_kmupdate_cam!(scene,Vec3f0(1.3032e5,7.12119e5,3.60022e5),Vec3f0(0,0,0))θ=0.0# wait untill window is openwhile!isopen(scene)sleep(0.1)endwhileisopen(scene)scale!(moon,(scale[],scale[],scale[]))scale!(earth,(scale[],scale[],scale[]))forshipinshipsscale!(ship.mesh,(scale[],scale[],scale[]))endtranslate!(moon,Vec3f0(-dbetween_km*sin(θ/28),dbetween_km*cos(θ/28),0))rotate!(earth,Vec3f0(0,0,1),θ)rotate!(moon,Vec3f0(0,0,1),π/2+θ/28)sleep(0.01)θ+=0.05*orbit_speed[]timestep=10*orbit_speed[]gravity(timestep,ships,earth,moon)endendfunctiongravity(timestep,ships,earth,moon)forshipinshipsrship=ship.position_m#translation(ship)[]rearth=translation(earth)[]rmoon=translation(moon)[]rship_earth=(rearth-rship)*1e3# convert to mrship_moon=(rmoon-rship)*1e3# convert to mG=6.67408e-11# m^3⋅kg^-2⋅s^-1nrship_earth=max(0.1,norm(rship_earth))nrship_moon=max(0.1,norm(rship_moon))fearth=G*(mship_kg*mearth_kg)/(nrship_earth^2)*rship_earth/nrship_earthfmoon=G*(mship_kg*mmoon_kg)/(nrship_moon^2)*rship_earth/nrship_earthftot=fearth+fmoonship.velocity_mps+=timestep*ftotdrship=timestep*ship.velocity_mps/1e3rship=rship+drshipmoveto!(ship,rship)endenduniverse=Scene(backgroundcolor=:black,show_axis=false,resolution=(2048,1024))ships=makeships(universe,600)earth,moon=makeobjects(universe);orbit_slider,orbit_speed,scale_slider,scale=makecontrols();controls=(orbit_slider,scale_slider);scene=hbox(vbox(controls...),universe)universe.center=falsetask=@asyncspin(universe,orbit_speed,scale,moon,earth,ships)scene.center=falseRecordEvents(scene,"output")