Mercurial > cortex
view org/test-creature.org @ 76:5e75b616ca60
going to work on constraints for the joints in the hand.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 28 Dec 2011 23:12:32 -0700 |
parents | f4c77512808e |
children | 1f84f425e05d |
line wrap: on
line source
1 #+title: First attempt at a creature!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description:5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org9 * Intro10 So far, I've made the following senses --11 - Vision12 - Hearing13 - Touch14 - Proprioception16 And one effector:17 - Movement19 However, the code so far has only enabled these senses, but has not20 actually implemented them. For example, there is still a lot of work21 to be done for vision. I need to be able to create an /eyeball/ in22 simulation that can be moved around and see the world from different23 angles. I also need to determine weather to use log-polar or cartesian24 for the visual input, and I need to determine how/wether to25 disceritise the visual input.27 I also want to be able to visualize both the sensors and the28 effectors in pretty pictures. This semi-retarted creature will by my29 first attempt at bringing everything together.31 * The creature's body33 Still going to do an eve-like body in blender, but due to problems34 importing the joints, etc into jMonkeyEngine3, I',m going to do all35 the connecting here in clojure code, using the names of the individual36 components and trial and error. Later, I'll maybe make some sort of37 creature-building modifications to blender that support whatever38 discreitized senses I'm going to make.40 #+name: body-141 #+begin_src clojure42 (ns cortex.silly43 "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-model57 "Load a .blend file using an asset folder relative path."58 [^String model]59 (.loadModel60 (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-names78 #{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 (filter97 (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 (filter106 (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-joint116 [#^Node parts #^Vector3f joint-position]117 (loop [radius (float 0.01)]118 (let [results (CollisionResults.)]119 (.collideWith120 parts121 (BoundingBox. joint-position radius radius radius)122 results)123 (let [targets124 (distinct125 (map #(.getGeometry %) results))]126 (if (>= (count targets) 2)127 (take 2 targets)128 (recur (float (* radius 2))))))))132 (defn connect-at-point133 [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 registers139 ;; it with both physics objects which in turn140 ;; will register the joint with the physics system141 ;; when the simulation is started.142 joint (Point2PointJoint.143 (.getControl obj-a RigidBodyControl)144 (.getControl obj-b RigidBodyControl)145 pivot-a146 pivot-b)]147 obj-a))150 (defn physical-hand [#^Node pieces joints]151 ;; Make each piece a physical entity in the simulation.152 (dorun153 (map154 (fn [geom]155 (let [physics-control156 (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 (dorun165 (map166 (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 (world178 (nodify [(the-hand!)179 (box 10 1 10 :position (Vector3f. 0 -10 0)180 :color ColorRGBA/Gray181 :mass 0)])182 standard-debug-controls183 enable-debug184 (fn [_ _]185 (Thread/sleep 100))))193 #+end_src195 #+results: body-1196 : #'cortex.silly/test-hand200 * COMMENT generate source201 #+begin_src clojure :tangle ../src/cortex/silly.clj202 <<body-1>>203 #+end_src