Mercurial > cortex
view org/proprioception.org @ 160:33278bf028e7
refactored joints
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 03 Feb 2012 06:47:05 -0700 |
parents | 84c67be00abe |
children | 1943b3f581c2 |
line wrap: on
line source
1 #+title: The Sense of Proprioception2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: proprioception for simulated creatures5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org9 #+name: proprioception10 #+begin_src clojure11 (ns cortex.proprioception12 (:use (cortex world util sense body)))14 (cortex.import/mega-import-jme3)16 (defn right-handed? [vec1 vec2 vec3]17 (< 0 (.dot (.cross vec1 vec2) vec3)))19 (defn absolute-angle [vec1 vec2 axis]20 (let [angle (.angleBetween vec1 vec2)]21 (if (right-handed? vec1 vec2 axis)22 angle (- (* 2 Math/PI) angle))))25 (defn joint-proprioception [#^Node parts #^Node joint]26 (let [[obj-a obj-b] (joint-targets parts joint)27 joint-rot (.getWorldRotation joint)28 x0 (.mult joint-rot Vector3f/UNIT_X)29 y0 (.mult joint-rot Vector3f/UNIT_Y)30 z0 (.mult joint-rot Vector3f/UNIT_Z)]31 (println-repl "x:" x0)32 (println-repl "y:" y0)33 (println-repl "z:" z0)34 (println-repl "init-a:" (.getWorldRotation obj-a))35 (println-repl "init-b:" (.getWorldRotation obj-b))37 (fn []38 (let [rot-a (.clone (.getWorldRotation obj-a))39 rot-b (.clone (.getWorldRotation obj-b))40 x (.mult rot-a x0)41 y (.mult rot-a y0)42 z (.mult rot-a z0)44 X (.mult rot-b x0)45 Y (.mult rot-b y0)46 Z (.mult rot-b z0)47 heading (Math/atan2 (.dot X z) (.dot X x))48 pitch (Math/atan2 (.dot X y) (.dot X x))50 ;; rotate x-vector back to origin51 reverse52 (doto (Quaternion.)53 (.fromAngleAxis54 (.angleBetween X x)55 (let [cross (.normalize (.cross X x))]56 (if (= 0 (.length cross)) y cross))))57 roll (absolute-angle (.mult reverse Y) y x)]59 [heading pitch roll]))))61 (defn proprioception62 "Create a function that provides proprioceptive information about an63 entire body."64 [#^Node creature]65 ;; extract the body's joints66 (let [joints (creature-joints creature)67 senses (map (partial joint-proprioception creature) joints)]68 (fn []69 (map #(%) senses))))75 #+end_src84 * COMMENT generate source85 #+begin_src clojure :tangle ../src/cortex/proprioception.clj86 <<proprioception>>87 #+end_src