Generate random bodies
This commit is contained in:
parent
35c0cc4446
commit
49116785ae
2 changed files with 40 additions and 8 deletions
37
app/Main.hs
37
app/Main.hs
|
@ -5,9 +5,38 @@ import Lib
|
||||||
import Linear.Affine
|
import Linear.Affine
|
||||||
import Linear.V3
|
import Linear.V3
|
||||||
|
|
||||||
|
import System.Random
|
||||||
|
import Control.Monad (replicateM)
|
||||||
|
|
||||||
import Graphics.Gloss hiding (Point)
|
import Graphics.Gloss hiding (Point)
|
||||||
|
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Random body generation
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
randomBody :: IO Body
|
||||||
|
randomBody = do
|
||||||
|
r <- randomIO :: IO Double
|
||||||
|
m <- randomIO :: IO Double
|
||||||
|
x <- randomIO :: IO Double
|
||||||
|
y <- randomIO :: IO Double
|
||||||
|
z <- randomIO :: IO Double
|
||||||
|
vx <- randomIO :: IO Double
|
||||||
|
vy <- randomIO :: IO Double
|
||||||
|
vz <- randomIO :: IO Double
|
||||||
|
-- Make radius proportional to mass for visualization
|
||||||
|
let radius = 20 * m
|
||||||
|
-- Scale mass
|
||||||
|
let mass = 1e3 * m
|
||||||
|
-- Scale position and speed
|
||||||
|
let posx = 1e3 * (2*x - 1)
|
||||||
|
let posy = 1e3 * (2*y - 1)
|
||||||
|
let speedx = 5e-5 * vx
|
||||||
|
let speedy = 5e-5 * vy
|
||||||
|
return $ Body "random" radius mass (P $ V3 posx posy 0) (V3 speedx speedy 0)
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- CSV export
|
-- CSV export
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -44,7 +73,7 @@ window :: Display
|
||||||
window = InWindow "Orbit" (width, height) (offset, offset)
|
window = InWindow "Orbit" (width, height) (offset, offset)
|
||||||
|
|
||||||
displayBody :: Body -> Picture
|
displayBody :: Body -> Picture
|
||||||
displayBody b = translate (realToFrac x/1e9) (realToFrac y/1e9) $ circle (realToFrac (bodyRadius b)/1e8)
|
displayBody b = translate (realToFrac x) (realToFrac y) $ circle (realToFrac (bodyRadius b))
|
||||||
where P (V3 x y _) = bodyPosition b
|
where P (V3 x y _) = bodyPosition b
|
||||||
|
|
||||||
displayBodies :: [Body] -> Picture
|
displayBodies :: [Body] -> Picture
|
||||||
|
@ -54,11 +83,13 @@ drawing :: Picture
|
||||||
drawing = color white $ circle 80
|
drawing = color white $ circle 80
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = simulate
|
main = do
|
||||||
|
bodies <- replicateM 300 randomBody
|
||||||
|
simulate
|
||||||
window
|
window
|
||||||
black
|
black
|
||||||
25
|
25
|
||||||
[sun, earth, moon, mercury, venus, mars]
|
bodies
|
||||||
displayBodies
|
displayBodies
|
||||||
(\_ dt bs -> updateAll (realToFrac dt*1e6) bs)
|
(\_ dt bs -> updateAll (realToFrac dt*1e6) bs)
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ executable orbit-exe
|
||||||
build-depends: base
|
build-depends: base
|
||||||
, orbit
|
, orbit
|
||||||
, linear
|
, linear
|
||||||
|
, random
|
||||||
, gloss
|
, gloss
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue