Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(empty-scene Width Height "black") renders as white in playground #320

Closed
spdegabrielle opened this issue Mar 5, 2023 · 5 comments
Closed

Comments

@spdegabrielle
Copy link
Contributor

spdegabrielle commented Mar 5, 2023

(empty-scene Width Height "black") renders as white

Sample code; tested/reproduced in in playground macOS / Safari and Firefox

RacketScript Playground Program; view at: http://play.racketscript.org/#gist/2ef0bae9c4b44483a79a5d7c7be65b91

Note: seems to run faster in firefox in the playground that is does in safari or DrRacket on macOS (M1)

#lang racketscript/base

(require racketscript/htdp/universe
         racketscript/htdp/image
         racketscript/interop
         racketscript/browser)
         
;;---------------------------------------------------------------------------------------------------

; The gravitational constant G
(define G 6.67428e-11)

; Assumed scale: 100 pixels = 1AU
(define AU (* 149.6e6 1000))
(define SCALE (/ 250 AU))

;Structure of body;position in m, vector in m/s, mass in kg, radius in m
(struct body (id px py vx vy mass radius color) #:mutable #:transparent) 
;The state of the solar system is represented as list of bodies, sized to be visible.
; this is the starting state
(define testCollPlanets
  (list
   (body "Sun" 0 0 0 0 1.98892e30 32 "yellow")
   (body "Mercury" (* -0.387098 AU) 0 0 -47.362e3 3.3011e23 4 "white")
   (body "Venus" (* 0.723 AU) 0 0 35.02e3 4.8685e24 8 "blue")
   (body "Earth" (* -1 AU) 0 0 -29.783e3 5.9742e24 8 "green")
   (body "Mars" (* 1.5236 AU) 0 0 24.077e3 6.4174e23 4 "Firebrick")
   ; Havoc
   ;(body "Havoc" (* -1.2 AU) 0 0 (* -10 1000) (* 8 (expt 10 28)) 16 "green")
   ;(body "Havoc2" (* -0.5 AU) 0 0 -30.362e3 3.3e23 4 "red")
   ;(body "Havoc3" (* -0.6 AU) 0 0 -27.362e3 3.3e23 4 "red")
   ;(body "Havoc4" (* -1.3 AU) 0 0 -17.362e3 3.3e23 4 "red")
   ;(body "Havoc5" (* -1.7 AU) 0 0 -07.362e3 3.3e23 4 "red")
   ))

(define timestep (* 12 3600)) ;Half a day

;; view setup
(define Width 1200)
(define xoffset (/ Width 2))
(define Height 960)
(define yoffset (/ Height 2))

;;---------------------------------------------------------------------------------------------------

;Calculate the force of attraction
(define (force g mass otherMass distance) 
  (/ (* g (* mass otherMass)) (expt distance 2)))

;Calculate direction of the force
(define (directionOfForce dx dy force) 
  (let ([theta (atan dy dx)])
    (list (* (cos theta) force) (* (sin theta) force))))

;; attraction : body otherBody -> 
;Creates a vector to adjust planet heading depending on all other bodies 
(define (attraction body otherBody) 
  (let* ([dx (- (body-px otherBody) (body-px body))]
         [dy (- (body-py otherBody) (body-py body))]
         [distance (sqrt (+ (expt dx 2) (expt dy 2)))]) ;Distance between bodys
    ;(displayln distance)
    (if (= distance 0) (print "Hitt!")
        (directionOfForce dx dy
                          (force G (body-mass body) (body-mass otherBody) distance)))))

;Creates a list of vectors, a vector for every body
(define (totalAttraction body bodies fxy) 
  (if (equal? bodies '())
      fxy
      (totalAttraction body (cdr bodies) (map + fxy (attraction body (car bodies))))))

;; gravity : (listof bodies) timestep ⇒ bodies
(define (gravity bodies timestep)
  (let* ([forces (for/list ([b bodies]) (totalAttraction b (remove b bodies) '(0 0)))]
         [vectors
          (for/list ([f forces][b bodies])
            (list (+ (body-vx b) (* (/ (car f) (body-mass b)) timestep))
                  (+ (body-vy b) (* (/ (car(cdr f)) (body-mass b)) timestep))))]
         [positions
          (for/list ([v vectors][b bodies])
            (list (+ (body-px b) (* (car v) timestep))
                  (+ (body-py b) (* (car (cdr v)) timestep))))])
    (for/list ([b bodies][v vectors][p positions])
      (body (body-id b) (car p) (car(cdr p)) (car v) (car(cdr v))
            (body-mass b) (body-radius b) (body-color b)))))

;; render-expr : bodies ⇒ scene
(define (render-expr bodies)
  (place-images
   (map (λ (b) (circle (body-radius b) "solid" (body-color b))) bodies)
   (map (λ (b) (make-posn (+ (* (body-px b) SCALE) xoffset )
                          (+ (* (body-py b) SCALE) yoffset ))) bodies)
   (empty-scene Width Height "black")))

;; tick-expr world ⇒ world
;; update velocities and positions
(define (tick-expr bodies)
  (gravity bodies timestep))

;;
(big-bang testCollPlanets
  (on-tick tick-expr)          
  (to-draw render-expr Width Height))
@spdegabrielle
Copy link
Contributor Author

I think I need to add a color parameter to

Perhaps

(define-proto EmptyScene
  (λ (width height borders?)
    #:with-this this
    (set-object! this
                 [type      "empty-scene"]
                 [width     width]
                 [height    height]
                 [color     "white"]
                 [borders?  borders?]))
  [render (λ (ctx x y)
            ;; TODO: borders?
            (void))])

@stchang
Copy link
Member

stchang commented Mar 21, 2023

Thanks for the report. It looks like EmptyScene is fake (because the render just calls void)

I'm wondering if we should just replace it with rectangle? Do you know what racket's 2htdp/image does?

@spdegabrielle
Copy link
Contributor Author

@spdegabrielle
Copy link
Contributor Author

spdegabrielle commented Mar 25, 2023

@stchang
Copy link
Member

stchang commented May 22, 2023

merged #321

@stchang stchang closed this as completed May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants