rlm@157: #+title: The Sense of Proprioception rlm@157: #+author: Robert McIntyre rlm@157: #+email: rlm@mit.edu rlm@157: #+description: proprioception for simulated creatures rlm@157: #+keywords: simulation, jMonkeyEngine3, clojure rlm@157: #+SETUPFILE: ../../aurellem/org/setup.org rlm@157: #+INCLUDE: ../../aurellem/org/level-0.org rlm@157: rlm@157: #+name: proprioception rlm@157: #+begin_src clojure rlm@157: (ns cortex.proprioception rlm@157: (:use (cortex world util sense body))) rlm@157: rlm@157: (cortex.import/mega-import-jme3) rlm@157: rlm@157: (defn right-handed? [vec1 vec2 vec3] rlm@157: (< 0 (.dot (.cross vec1 vec2) vec3))) rlm@157: rlm@157: (defn absolute-angle [vec1 vec2 axis] rlm@157: (let [angle (.angleBetween vec1 vec2)] rlm@157: (if (right-handed? vec1 vec2 axis) rlm@157: angle (- (* 2 Math/PI) angle)))) rlm@157: rlm@157: rlm@157: (defn joint-proprioception [#^Node parts #^Node joint] rlm@157: (let [[obj-a obj-b] (joint-targets parts joint) rlm@157: joint-rot (.getWorldRotation joint) rlm@157: x0 (.mult joint-rot Vector3f/UNIT_X) rlm@157: y0 (.mult joint-rot Vector3f/UNIT_Y) rlm@157: z0 (.mult joint-rot Vector3f/UNIT_Z)] rlm@157: (println-repl "x:" x0) rlm@157: (println-repl "y:" y0) rlm@157: (println-repl "z:" z0) rlm@157: (println-repl "init-a:" (.getWorldRotation obj-a)) rlm@157: (println-repl "init-b:" (.getWorldRotation obj-b)) rlm@157: rlm@157: (fn [] rlm@157: (let [rot-a (.clone (.getWorldRotation obj-a)) rlm@157: rot-b (.clone (.getWorldRotation obj-b)) rlm@157: x (.mult rot-a x0) rlm@157: y (.mult rot-a y0) rlm@157: z (.mult rot-a z0) rlm@157: rlm@157: X (.mult rot-b x0) rlm@157: Y (.mult rot-b y0) rlm@157: Z (.mult rot-b z0) rlm@157: heading (Math/atan2 (.dot X z) (.dot X x)) rlm@157: pitch (Math/atan2 (.dot X y) (.dot X x)) rlm@157: rlm@157: ;; rotate x-vector back to origin rlm@157: reverse rlm@157: (doto (Quaternion.) rlm@157: (.fromAngleAxis rlm@157: (.angleBetween X x) rlm@157: (let [cross (.normalize (.cross X x))] rlm@157: (if (= 0 (.length cross)) y cross)))) rlm@157: roll (absolute-angle (.mult reverse Y) y x)] rlm@157: rlm@157: [heading pitch roll])))) rlm@157: rlm@157: (defn proprioception rlm@157: "Create a function that provides proprioceptive information about an rlm@157: entire body." rlm@157: [#^Node creature] rlm@157: ;; extract the body's joints rlm@157: (let [joints (creature-joints creature) rlm@157: senses (map (partial joint-proprioception creature) joints)] rlm@157: (fn [] rlm@157: (map #(%) senses)))) rlm@157: rlm@157: rlm@157: rlm@157: rlm@157: rlm@157: #+end_src rlm@157: rlm@157: rlm@157: rlm@157: rlm@157: rlm@157: rlm@157: rlm@157: rlm@157: * COMMENT generate source rlm@157: #+begin_src clojure :tangle ../src/cortex/proprioception.clj rlm@157: <> rlm@157: #+end_src