Skip to content

Commit

Permalink
Basic motion functions implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Rees committed Apr 17, 2011
1 parent 4bb2017 commit d0fd077
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,6 +1,6 @@
(defproject coloured-balls "0.0.1-SNAPSHOT"
:description "A visual game challenge for the Clojure dojo"
:main coloured-balls.core
:dependencies [[org.clojure/clojure "1.2.0"]
:dependencies [[org.clojure/clojure "1.2.0"] [org.clojure/clojure-contrib "1.2.0"]
[midje "1.1-alpha-3"]
[org.clojars.automata/rosado.processing "1.1.0"]])
14 changes: 12 additions & 2 deletions src/coloured_balls/motion.clj
@@ -1,4 +1,14 @@
(ns coloured-balls.motion)
(ns coloured-balls.motion
(:use [clojure.contrib.generic.math-functions :only (floor, cos, sin)]))

(defn degrees-to-radians [degrees]
(* (/ Math/PI 180) degrees))

(defn move [item]
{:x 3})
(let [heading (degrees-to-radians (:heading item))
velocity (:velocity item)
old-x (:x item)
old-y (:y item)
new-x (floor (* velocity (cos heading)))
new-y (floor (* velocity (sin heading)))]
(conj {:x (+ old-x new-x) :y (+ old-y new-y)} (dissoc item :x :y))))
8 changes: 7 additions & 1 deletion test/coloured_balls/core_test.clj
Expand Up @@ -2,4 +2,10 @@
(:use coloured-balls.motion coloured-balls.collision
midje.sweet))

(fact (:x (move {:x 0 :y 0 :heading 45 :velocity 5})) => 3)
(fact (:x (move {:x 0 :y 0 :heading 45 :velocity 5})) => 3)

(fact (:y (move {:x 0 :y 0 :heading 45 :velocity 5})) => 3)

(fact (:x (move {:x 4 :y 4 :heading 225 :velocity 5})) => 0)

(fact (:y (move {:x 4 :y 4 :heading 225 :velocity 5})) => 0)
7 changes: 7 additions & 0 deletions test/coloured_balls/motion_test.clj
@@ -0,0 +1,7 @@
(ns coloured-balls.core-test
(:use coloured-balls.motion
midje.sweet))

(fact (direction {:heading 45}) => [1 1])

(fact (direction {:heading 225}) => [-1 -1])

0 comments on commit d0fd077

Please sign in to comment.