Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added trails

  • Loading branch information...
commit 6074fde6f3e4990ec7fa1fb376be439934e53802 1 parent 19b8a6c
unclebob authored
View
1  README
@@ -19,6 +19,7 @@ Commands:
q Quit
+ zoom in
- zoom out
+ t toggle trails
<space> Center the sun.
View
21 src/orbit/world.clj
@@ -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))
@@ -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)
)
)
@@ -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)
)
@@ -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)
@@ -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)
)
@@ -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)
View
2  src/physics/object.clj
@@ -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)
)
)
View
1  src/physics/object_test.clj
@@ -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)))
)
)
Please sign in to comment.
Something went wrong with that request. Please try again.