PasteRack.org
Paste # 72019
2017-05-23 16:25:29

Fork as a new paste.

Paste viewed 66 times.


Embed:

solar sim

  1. #lang racket/base
  2. (require 2htdp/universe 2htdp/image lang/posn)
  3. ; The gravitational constant G
  4. (define G 6.67428e-11)
  5.  
  6. ; Assumed scale: 100 pixels = 1AU
  7. (define AU (* 149.6e6 1000))
  8. (define SCALE (/ 250 AU))
  9.  
  10. (struct body (id px py vx vy mass radius color) #:mutable #:transparent) ;Structure of body
  11. ;position in m, vector in m/s, mass in kg, radius in m
  12.  
  13. (define (force g mass otherMass distance) ;Calculate the force of attraction
  14.   (/ (* g (* mass otherMass)) (expt distance 2)))
  15.  
  16. (define (directionOfForce dx dy force) ;Calculate direction of the force
  17.   (let ([theta (atan dy dx)])
  18.     (list (* (cos theta) force) (* (sin theta) force))))
  19.  
  20. (define (attraction body otherBody) ;Creates a vector to adjust planet heading depending on all other bodies
  21.   (let* ([dx (- (body-px otherBody) (body-px body))]
  22.          [dy (- (body-py otherBody) (body-py body))]
  23.          [distance (sqrt (+ (expt dx 2) (expt dy 2)))]) ;Distance between bodys
  24.     (if (= distance 0) (print "Hitt!")
  25.         (directionOfForce dx dy
  26.                           (force G (body-mass body) (body-mass otherBody) distance)))))
  27.  
  28. (define timestep (* 12 3600)) ;Half a day
  29.  
  30. (define (totalAttraction body bodies fxy) ;Creates a list of vectors, a vector for every body
  31.   (if (equal? bodies '())
  32.       fxy
  33.       (totalAttraction body (cdr bodies) (map + fxy (attraction body (car bodies))))))
  34.  
  35. ;; gravity
  36. ;; bodies  bodies
  37. (define (gravity bodies timestep)
  38.   (let* ([forces (for/list ([b bodies]) (totalAttraction b (remove b bodies) '(0 0)))]
  39.          [vectors (for/list ([f forces][b bodies]) (list (+ (body-vx b) (* (/ (car f) (body-mass b)) timestep))
  40.                                                          (+ (body-vy b) (* (/ (car(cdr f)) (body-mass b)) timestep))))]
  41.          [positions (for/list ([v vectors][b bodies]) (list (+ (body-px b) (* (car v) timestep))
  42.                                                             (+ (body-py b) (* (car (cdr v)) timestep))))])
  43.     (for/list ([b bodies][v vectors][p positions])
  44.       (body (body-id b) (car p) (car(cdr p)) (car v) (car(cdr v))
  45.             (body-mass b) (body-radius b) (body-color b)))))
  46.  
  47. ;(struct body (id px py vx vy mass radius color)) ;just a reminder of the struct
  48.  
  49. ;A list of bodies, size of planets is not real... you woldent se the planets.
  50. (define testCollPlanets (list
  51.                          (body "Sun" 0 0 0 0 (* 1.98892 (expt 10 30)) 100 "yellow")
  52.                          (body "Mercury" (* -0.387098 AU) 0 0 (* -47.362 1000) (* 3.3011 (expt 10 23)) 4 "red")
  53.                          (body "Venus" (* 0.723 AU) 0 0 (* 35.02 1000) (* 4.8685 (expt 10 24)) 8 "brown")
  54.                          (body "Earth" (* -1 AU) 0 0 (* -29.783 1000) (* 5.9742 (expt 10 24)) 8 "green")
  55.                          (body "Mars" (* -1.5236 AU) 0 0 (* -24.077 1000) (* 6.4174 (expt 10 23)) 4 "orange")
  56.                        ;  (body "Havoc" (* -1.2 AU) 0 0 (* -10 1000) (* 8 (expt 10 25)) 50 "green")
  57.                          ))
  58.  
  59. (define (printBodies bodies scale) ;To print the numbers for control
  60.   (if (equal? bodies '())
  61.       (printf "Done\n")
  62.       (let
  63.           ([ p (printf "Position XY ~a \n" (list (body-id (car bodies))
  64.                                                  (* (body-px (car bodies)) scale)
  65.                                                  (* (body-py (car bodies)) scale)
  66.                                                  (* (body-vx (car bodies)) scale)
  67.                                                  (* (body-vy (car bodies)) scale)))])
  68.         (printBodies (cdr bodies) scale))))
  69.  
  70. ;; setup
  71. (define Width 1200)
  72. (define xoffset (/ Width 2))
  73. (define Height 720)
  74. (define yoffset (/ Height 2))
  75.  
  76. ;; start-world (world left right ps vs as f)
  77. (define starting-state testCollPlanets);position velocity
  78.  
  79. ;; world -> scene
  80. (define (render-expr bodies)
  81.   ;(printBodies bodies SCALE)
  82.   (place-images
  83.    (map (λ (b) (circle (body-radius b) "solid" (body-color b))) bodies)
  84.    (map (λ (b) (make-posn (+ (* (body-px b) SCALE) xoffset ) (+ (* (body-py b) SCALE) yoffset ))) bodies)
  85.    (empty-scene Width Height "black")))
  86.  
  87. ;; world -> world
  88. ;; update velocities and positions
  89. (define (tick-expr bodies)
  90.   (gravity bodies timestep))
  91.  
  92. ;;
  93. (big-bang starting-state
  94.           (on-tick tick-expr)
  95.           (to-draw render-expr Width Height))

=>