# HG changeset patch # User Robert McIntyre # Date 1328363227 25200 # Node ID 0b9ae09eaec3ddf0b1cac62a7b7ac65d13603a0c # Parent 136349ac69723d05a559e6e35066d656c889d56e renamed functions in body diff -r 136349ac6972 -r 0b9ae09eaec3 org/body.org --- a/org/body.org Sat Feb 04 06:29:20 2012 -0700 +++ b/org/body.org Sat Feb 04 06:47:07 2012 -0700 @@ -5,8 +5,8 @@ #+SETUPFILE: ../../aurellem/org/setup.org #+INCLUDE: ../../aurellem/org/level-0.org -* Proprioception -#+name: proprioception +* Making a solid, connected body. +#+name: joints #+begin_src clojure (ns cortex.body (:use (cortex world util sense)) @@ -20,23 +20,9 @@ com.jme3.bounding.BoundingBox com.jme3.scene.Node)) +(use 'clojure.contrib.def) (cortex.import/mega-import-jme3) -(defn jme-to-blender - "Convert from JME coordinates to Blender coordinates" - [#^Vector3f in] - (Vector3f. (.getX in) - (- (.getZ in)) - (.getY in))) - -(defn blender-to-jme - "Convert from Blender coordinates to JME coordinates" - [#^Vector3f in] - (Vector3f. (.getX in) - (.getZ in) - (- (.getY in)))) - - (defn joint-targets "Return the two closest two objects to the joint object, ordered from bottom to top according to the joint's rotation." @@ -65,13 +51,6 @@ (take 2 targets)) (recur (float (* radius 2)))))))) -(defn creature-joints - "Return the children of the creature's \"joints\" node." - [#^Node creature] - (if-let [joint-node (.getChild creature "joints")] - (seq (.getChildren joint-node)) - (do (println-repl "could not find JOINTS node") []))) - (defmulti joint-dispatch "Translate blender pseudo-joints into real JME joints." (fn [constraints & _] @@ -158,7 +137,10 @@ (float twist))))) (defn connect - "here are some examples: + "Create a joint between 'obj-a and 'obj-b at the location of + 'joint. The type of joint is determined by the metadata on 'joint. + + Here are some examples: {:type :point} {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) @@ -192,10 +174,16 @@ joint-rotation)) (println-repl "could not find joint meta-data!")))) +(defvar + ^{:arglists '([creature])} + joints + (sense-nodes "joints") + "Return the children of the creature's \"joints\" node.") - - -(defn assemble-creature [#^Node pieces joints] +(defn physical! + "Iterate through the nodes in creature and make them real physical + objects in the simulation." + [#^Node creature] (dorun (map (fn [geom] @@ -212,94 +200,38 @@ (.addControl geom physics-control))) (filter #(isa? (class %) Geometry ) - (node-seq pieces)))) + (node-seq creature))))) + +(defn joints! + "Connect the solid parts of the creature with physical joints. The + joints are taken from the \"joints\" node in the creature." + [#^Node creature] (dorun (map (fn [joint] - (let [[obj-a obj-b] (joint-targets pieces joint)] + (let [[obj-a obj-b] (joint-targets creature joint)] (connect obj-a obj-b joint))) - joints)) - pieces) + (joints creature)))) +(defn body! + "Endow the creature with a physical body connected with joints. The + particulars of the joints and the masses of each pody part are + determined in blender." + [#^Node creature] + (physical! creature) + (joints! creature)) -;; TODO implement a function that adds joints in the style of the -;; other sense functions -(import java.awt.image.BufferedImage) - -(defn draw-sprite [image sprite x y color ] - (dorun - (for [[u v] sprite] - (.setRGB image (+ u x) (+ v y) color)))) - -(defn view-angle - "create a debug view of an angle" - [color] - (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB) - previous (atom [25 25]) - sprite [[0 0] [0 1] - [0 -1] [-1 0] [1 0]]] - (fn [angle] - (let [angle (float angle)] - (let [position - [(+ 25 (int (* 20 (Math/cos angle)))) - (+ 25 (int (* -20 (Math/sin angle))))]] - (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000) - (draw-sprite image sprite (position 0) (position 1) color) - (reset! previous position)) - image)))) - -(defn proprioception-debug-window - [] - (let [heading (view-angle 0xFF0000) - pitch (view-angle 0x00FF00) - roll (view-angle 0xFFFFFF) - v-heading (view-image) - v-pitch (view-image) - v-roll (view-image) - ] - (fn [prop-data] - (dorun - (map - (fn [[h p r]] - (v-heading (heading h)) - (v-pitch (pitch p)) - (v-roll (roll r))) - prop-data))))) - - #+end_src +#+results: joints +: #'cortex.body/body! + #+results: proprioception : #'cortex.body/proprioception-debug-window -* Motor Control -#+name: motor-control -#+begin_src clojure -(in-ns 'cortex.body) - -;; surprisingly enough, terristerial creatures only move by using -;; torque applied about their joints. There's not a single straight -;; line of force in the human body at all! (A straight line of force -;; would correspond to some sort of jet or rocket propulseion.) - -(defn vector-motor-control - "Create a function that accepts a sequence of Vector3f objects that - describe the torque to be applied to each part of the body." - [body] - (let [nodes (node-seq body) - controls (keep #(.getControl % RigidBodyControl) nodes)] - (fn [torques] - (map #(.applyTorque %1 %2) - controls torques)))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -#+end_src - -## note -- might want to add a lower dimensional, discrete version of -## this if it proves useful from a x-modal clustering perspective. - * Examples #+name: test-body @@ -1032,8 +964,7 @@ * COMMENT generate Source #+begin_src clojure :tangle ../src/cortex/body.clj -<> -<> +<> #+end_src #+begin_src clojure :tangle ../src/cortex/test/body.clj diff -r 136349ac6972 -r 0b9ae09eaec3 org/proprioception.org --- a/org/proprioception.org Sat Feb 04 06:29:20 2012 -0700 +++ b/org/proprioception.org Sat Feb 04 06:47:07 2012 -0700 @@ -18,12 +18,6 @@ (:import com.jme3.scene.Node) (:import (com.jme3.math Vector3f Quaternion))) -(defvar - ^{:arglists '([creature])} - joints - (sense-nodes "joints") - "Return the children of the creature's \"joints\" node.") - (defn right-handed? "true iff the three vectors form a right handed coordinate system. The three vectors do not have to be normalized or @@ -89,6 +83,50 @@ (joints creature))] (fn [] (map #(%) senses)))) + + +(import java.awt.image.BufferedImage) + +(defn draw-sprite [image sprite x y color ] + (dorun + (for [[u v] sprite] + (.setRGB image (+ u x) (+ v y) color)))) + +(defn view-angle + "create a debug view of an angle" + [color] + (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB) + previous (atom [25 25]) + sprite [[0 0] [0 1] + [0 -1] [-1 0] [1 0]]] + (fn [angle] + (let [angle (float angle)] + (let [position + [(+ 25 (int (* 20 (Math/cos angle)))) + (+ 25 (int (* -20 (Math/sin angle))))]] + (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000) + (draw-sprite image sprite (position 0) (position 1) color) + (reset! previous position)) + image)))) + +(defn proprioception-debug-window + [] + (let [heading (view-angle 0xFF0000) + pitch (view-angle 0x00FF00) + roll (view-angle 0xFFFFFF) + v-heading (view-image) + v-pitch (view-image) + v-roll (view-image) + ] + (fn [prop-data] + (dorun + (map + (fn [[h p r]] + (v-heading (heading h)) + (v-pitch (pitch p)) + (v-roll (roll r))) + prop-data))))) + #+end_src * COMMENT generate source diff -r 136349ac6972 -r 0b9ae09eaec3 org/test-creature.org --- a/org/test-creature.org Sat Feb 04 06:29:20 2012 -0700 +++ b/org/test-creature.org Sat Feb 04 06:47:07 2012 -0700 @@ -71,15 +71,6 @@ (.registerLoader BlenderModelLoader (into-array String ["blend"]))) model)) -(declare blender-creature) - -(defn blender-creature - "Return a creature with all joints in place." - [blender-path] - (let [model (load-blender-model blender-path) - joints (creature-joints model)] - (assemble-creature model joints))) - (def hand "Models/creature1/one.blend") (def worm "Models/creature1/try-again.blend") @@ -178,7 +169,9 @@ (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green) z-axis (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue) - creature (blender-creature thing) + creature (doto + (load-blender-model thing) + (body!)) touch-nerves (touch creature) touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves) vision-data (vision! creature)