PasteRack.org
Paste # 13362
2019-01-13 16:09:42

Fork as a new paste.

Paste viewed 231 times.


Embed:

solar

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

=>