Skip to content

Commit

Permalink
added trails
Browse files Browse the repository at this point in the history
  • Loading branch information
unclebob committed Jun 13, 2010
1 parent 19b8a6c commit 6074fde
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions README
Expand Up @@ -19,6 +19,7 @@ Commands:
q Quit
+ zoom in
- zoom out
t toggle trails
<space> Center the sun.


21 changes: 18 additions & 3 deletions src/orbit/world.clj
Expand Up @@ -11,7 +11,7 @@

(def center (position/make 500 500))

(defstruct controls :magnification :center)
(defstruct controls :magnification :center :trails :clear)

(defn size-by-mass [{m :mass}]
(+ 3 (Math/sqrt m))
Expand Down Expand Up @@ -53,6 +53,7 @@
(doseq [obj world]
(draw-object g obj controls)
)
(.clearRect g 0 0 1000 20)
(.drawString g (str "Objects: " (count world)) 20 20)
)
)
Expand All @@ -70,10 +71,17 @@
]
(alter controls #(assoc % :magnification new-mag))
(alter controls #(assoc % :center sun-position))
(alter controls #(assoc % :clear true))
)
)
)

(defn reset-screen-state [controls]
(dosync (alter controls #(assoc % :clear false))))

(defn toggle-trail [controls]
(dosync (alter controls #(assoc % :trails (not (:trails @controls))))))

(defn- quit-key? [c]
(= \q c)
)
Expand All @@ -90,11 +98,17 @@
(= \space c)
)

(defn- trail-key? [c]
(= \t c)
)

(defn world-panel [frame world controls]
(proxy [JPanel ActionListener KeyListener] []
(paintComponent [g]
(proxy-super paintComponent g)
(when (or (:clear @controls) (not (:trails @controls)))
(proxy-super paintComponent g))
(draw-world g @world @controls)
(reset-screen-state controls)
)
(actionPerformed [e]
(update-world world)
Expand All @@ -109,6 +123,7 @@
(plus-key? c) (magnify 1.1 controls world)
(minus-key? c) (magnify 0.9 controls world)
(space-key? c) (magnify 1.0 controls world)
(trail-key? c) (toggle-trail controls)
)
(.repaint this)
)
Expand Down Expand Up @@ -170,7 +185,7 @@

(defn world-frame []
(let [
controls (ref (struct controls 1 center))
controls (ref (struct controls 1 center false false))
world (ref (create-world))
frame (JFrame. "Orbit")
panel (world-panel frame world controls)
Expand Down
2 changes: 1 addition & 1 deletion src/physics/object.clj
Expand Up @@ -53,7 +53,7 @@
v (:velocity o)
av (vector/add v (vector/scale f (/ 1.0 m)))
]
(-> o (assoc :velocity av) (assoc :force (vector/make)))
(assoc o :velocity av)
)
)

Expand Down
1 change: 0 additions & 1 deletion src/physics/object_test.clj
Expand Up @@ -80,7 +80,6 @@
ao (object/accelerate o)
]
(is (vector-close-to (vector/make 1.5 1.5) (:velocity ao)))
(is (= (vector/make) (:force ao)))
)
)

Expand Down

0 comments on commit 6074fde

Please sign in to comment.