view org/test-creature.org @ 75:f4c77512808e

hand appears to work now!
author Robert McIntyre <rlm@mit.edu>
date Wed, 28 Dec 2011 06:53:34 -0700
parents fb810a2c50c2
children 1f84f425e05d
line wrap: on
line source
1 #+title: First attempt at a creature!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description:
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
9 * Intro
10 So far, I've made the following senses --
11 - Vision
12 - Hearing
13 - Touch
14 - Proprioception
16 And one effector:
17 - Movement
19 However, the code so far has only enabled these senses, but has not
20 actually implemented them. For example, there is still a lot of work
21 to be done for vision. I need to be able to create an /eyeball/ in
22 simulation that can be moved around and see the world from different
23 angles. I also need to determine weather to use log-polar or cartesian
24 for the visual input, and I need to determine how/wether to
25 disceritise the visual input.
27 I also want to be able to visualize both the sensors and the
28 effectors in pretty pictures. This semi-retarted creature will by my
29 first attempt at bringing everything together.
31 * The creature's body
33 Still going to do an eve-like body in blender, but due to problems
34 importing the joints, etc into jMonkeyEngine3, I',m going to do all
35 the connecting here in clojure code, using the names of the individual
36 components and trial and error. Later, I'll maybe make some sort of
37 creature-building modifications to blender that support whatever
38 discreitized senses I'm going to make.
40 #+name: body-1
41 #+begin_src clojure
42 (ns cortex.silly
43 "let's play!"
44 {:author "Robert McIntyre"})
46 ;; TODO remove this!
47 (require 'cortex.import)
48 (cortex.import/mega-import-jme3)
49 (use '(cortex world util body hearing touch vision))
51 (use '[clojure.contrib [seq :only [find-first]]])
54 (rlm.rlm-commands/help)
56 (defn load-blender-model
57 "Load a .blend file using an asset folder relative path."
58 [^String model]
59 (.loadModel
60 (doto (asset-manager)
61 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
62 model))
64 (defn meta-data [blender-node key]
65 (if-let [data (.getUserData blender-node "properties")]
66 (.findValue data key)
67 nil))
69 (defn hand2 []
70 (load-blender-model "Models/creature1/try-again.blend"))
72 (defn hand []
73 (load-blender-model "Models/creature1/one.blend"))
77 (def hand-names
78 #{
79 "middle-1"
80 "middle-2"
81 "middle-3"
82 "palm"
83 "pinky-1"
84 "pinky-2"
85 "pinky-3"
86 "pointer-1"
87 "pointer-2"
88 "pointer-3"
89 "ring-1"
90 "ring-2"
91 "ring-3"
92 "thumb-1"
93 "thumb-2"})
95 (defn hand-pieces []
96 (filter
97 (comp hand-names #(apply str (drop-last (.getName %)))) (node-seq (hand))))
99 (defn hand-joints []
100 (map #(.getWorldTranslation %)
101 (filter #(re-matches #"joint\.\d\d\d" (.getName %))
102 (node-seq (hand)))))
104 (defn worm-pieces []
105 (filter
106 (comp #{"worm-2" "worm-1"}
107 #(apply str (drop-last (.getName %))))
108 (node-seq (hand2))))
110 (defn worm-joints []
111 [Vector3f/ZERO])
115 (defn find-joint
116 [#^Node parts #^Vector3f joint-position]
117 (loop [radius (float 0.01)]
118 (let [results (CollisionResults.)]
119 (.collideWith
120 parts
121 (BoundingBox. joint-position radius radius radius)
122 results)
123 (let [targets
124 (distinct
125 (map #(.getGeometry %) results))]
126 (if (>= (count targets) 2)
127 (take 2 targets)
128 (recur (float (* radius 2))))))))
132 (defn connect-at-point
133 [obj-a obj-b point]
134 (let [center-a (.getWorldTranslation obj-a)
135 center-b (.getWorldTranslation obj-b)
136 pivot-a (.subtract point center-a)
137 pivot-b (.subtract point center-b)
138 ;; A side-effect of creating a joint registers
139 ;; it with both physics objects which in turn
140 ;; will register the joint with the physics system
141 ;; when the simulation is started.
142 joint (Point2PointJoint.
143 (.getControl obj-a RigidBodyControl)
144 (.getControl obj-b RigidBodyControl)
145 pivot-a
146 pivot-b)]
147 obj-a))
150 (defn physical-hand [#^Node pieces joints]
151 ;; Make each piece a physical entity in the simulation.
152 (dorun
153 (map
154 (fn [geom]
155 (let [physics-control
156 (RigidBodyControl.
157 (HullCollisionShape.
158 (.getMesh geom))
159 ;; TODO: fix this.
160 (float 1.0))]
161 (.addControl geom physics-control)))
162 (filter #(isa? (class %) Geometry )
163 (node-seq pieces))))
164 (dorun
165 (map
166 (fn [joint-position]
167 (let [[geom-a geom-b] (find-joint pieces joint-position)]
168 (connect-at-point geom-a geom-b joint-position)))
169 joints))
170 pieces)
173 (defn the-hand! [] (physical-hand (hand) (hand-joints)))
176 (defn test-hand []
177 (world
178 (nodify [(the-hand!)
179 (box 10 1 10 :position (Vector3f. 0 -10 0)
180 :color ColorRGBA/Gray
181 :mass 0)])
182 standard-debug-controls
183 enable-debug
184 (fn [_ _]
185 (Thread/sleep 100))))
193 #+end_src
195 #+results: body-1
196 : #'cortex.silly/test-hand
200 * COMMENT generate source
201 #+begin_src clojure :tangle ../src/cortex/silly.clj
202 <<body-1>>
203 #+end_src