Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic motion functions implemented

  • Loading branch information...
commit d0fd077d098dcc3fed7b3a0ca2063d2048bf212b 1 parent 4bb2017
@rrees rrees authored
View
2  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"]])
View
14 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))))
View
8 test/coloured_balls/core_test.clj
@@ -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)
View
7 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])
Please sign in to comment.
Something went wrong with that request. Please try again.