You are on page 1of 19

Chapter 3

Application Asteroid field

Application Asteroid field


Asteroid field with gravitational forces
Asteroid list Position, velocity, mass Gravitational attraction Bouncing on the field borders (asteroid box!)

Application Asteroid field


module Chapter3 module SmallAsteroidFieldSimulation = open System open System.Threading open Chapter2.Math

Application Asteroid field


type Asteroid = { Position : Vector2<m> Velocity : Vector2<m/s> Mass : float<kg> Name : string }

Application Asteroid field


let dt = 60.0<s> let G = 6.67e-11<m^3*kg^-1*s^-2>

let let let let let

earth_radius = 6.37e6<m> field_size = earth_radius*60.0 max_velocity = 2.3e4<m/s> earth_mass = 5.97e24<kg> moon_mass = 7.35e22<kg>

Application Asteroid field


let create_field num_asteroids =

let lerp (x:float<'u>) (y:float<'u>) (a:float) = x*a+y*(1.0-a)

Application Asteroid field


let rand = Random()

Application Asteroid field


Lists
Empty list: [] List of integers with one element 1: [1] List of strings with 2 elements: [a; b]

List-comprehension syntax
Brackets Loop Yield

Application Asteroid field


[ for i = 1 to num_asteroids do let m = (lerp earth_mass moon_mass (rand.NextDouble()))* 1.0e-4 let x = lerp 0.0<m> field_size (rand.NextDouble()) let y = lerp 0.0<m> field_size (rand.NextDouble()) let vx = max_velocity* (rand.NextDouble()*2.0-1.0)*0.1 let vy = max_velocity* (rand.NextDouble()*2.0-1.0)*0.1

Application Asteroid field


yield { Position Velocity Mass = Name = }
] let f0 = create_field 20

= { X = x; Y = y } = { X = vx; Y = vy } m "a"

Application Asteroid field


Asteroid movement
along its velocity bouncing on the borders being subjected to gravitational attraction

Application Asteroid field


let clamp (p:Vector2<_>,v:Vector2<_>) = let p,v = if p.X < 0.0<_> then { p with X = 0.0<_> },{ v with X = -v.X } else p,v let p,v = if p.X > field_size then { p with X = field_size },{ v with X = -v.X } else p,v let p,v = if p.Y < 0.0<_> then { p with Y = 0.0<_> },{ v with Y = -v.Y } else p,v let p,v = if p.Y > field_size then { p with Y = field_size },{ v with Y = -v.Y } else p,v p,v

Application Asteroid field


let force (a:Asteroid,a':Asteroid) = let dir = a'.Position-a.Position let dist = dir.Length+1.0<m> G*a.Mass*a'.Mass*dir/(dist*dist*dist)

let simulation_step (asteroids:Asteroid list) = [ for a in asteroids do let forces = [ for a' in asteroids do if a' <> a then yield force(a,a') ] let F = List.sum forces let p',v' = clamp(a.Position,a.Velocity) yield { a with Position = p'+dt*v' Velocity = v'+dt*F/a.Mass } ]

let simulation_step (asteroids:Asteroid list) = [ for a in asteroids do let forces = [ for a' in asteroids do if a' <> a then yield force(a,a') ] let F = List.sum forces let p',v' = clamp(a.Position,a.Velocity) yield { a with Position = p'+dt*v' Velocity = v'+dt*F/a.Mass } ]

let simulation_step (asteroids:Asteroid list) = [ for a in asteroids do let forces = [ for a' in asteroids do if a' <> a then yield force(a,a') ] let F = List.sum forces let p',v' = clamp(a.Position,a.Velocity) yield { a with Position = p'+dt*v' Velocity = v'+dt*F/a.Mass } ]

let simulation_step (asteroids:Asteroid list) = [ for a in asteroids do let forces = [ for a' in asteroids do if a' <> a then yield force(a,a') ] let F = List.sum forces let p',v' = clamp(a.Position,a.Velocity) yield { a with Position = p'+dt*v' Velocity = v'+dt*F/a.Mass } ]

Application Asteroid field


let print_scene (asteroids:Asteroid list) = do Console.Clear() for i = 0 to 79 do Console.SetCursorPosition(i,0) Console.Write("*") Console.SetCursorPosition(i,23) Console.Write("*") for j = 0 to 23 do Console.SetCursorPosition(0,j) Console.Write("*") Console.SetCursorPosition(79,j) Console.Write("*") let set_cursor_on_body b = Console.SetCursorPosition( ((b.Position.X/4.0e8<m>)*78.0+1.0) |> int, ((b.Position.Y/4.0e8<m>)*23.0+1.0) |> int) for a in asteroids do do set_cursor_on_body a do Console.Write(a.Name) do Thread.Sleep(100)

Application Asteroid field


let simulation() = let rec simulation m = do print_scene m let m' = simulation_step m do simulation m' do simulation f0

You might also like