Mercurial > cortex
view org/test-creature.org @ 80:7af5ef686539
got a finger working!
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 05 Jan 2012 02:35:07 -0700 |
parents | 01dbdb0d5500 |
children | 10f495560c59 |
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 (rlm.rlm-commands/help)53 (defn load-blender-model54 "Load a .blend file using an asset folder relative path."55 [^String model]56 (.loadModel57 (doto (asset-manager)58 (.registerLoader BlenderModelLoader (into-array String ["blend"])))59 model))61 (defn meta-data [blender-node key]62 (if-let [data (.getUserData blender-node "properties")]63 (.findValue data key)64 nil))66 (defn blender-to-jme67 "Convert from Blender coordinates to JME coordinates"68 [#^Vector3f in]69 (Vector3f. (.getX in)70 (.getZ in)71 (- (.getY in))))73 (defn jme-to-blender74 "Convert from JME coordinates to Blender coordinates"75 [#^Vector3f in]76 (Vector3f. (.getX in)77 (- (.getZ in))78 (.getY in)))80 (defn joint-targets81 "Return the two closest two objects to the joint object, ordered82 from bottom to top according to the joint's rotation."83 [#^Node parts #^Node joint]84 ;;(println (meta-data joint "joint"))85 (.getWorldRotation joint)86 (loop [radius (float 0.01)]87 (let [results (CollisionResults.)]88 (.collideWith89 parts90 (BoundingBox. (.getWorldTranslation joint)91 radius radius radius)92 results)93 (let [targets94 (distinct95 (map #(.getGeometry %) results))]96 (if (>= (count targets) 2)97 (sort-by98 #(let [v99 (jme-to-blender100 (.mult101 (.inverse (.getWorldRotation joint))102 (.subtract (.getWorldTranslation %)103 (.getWorldTranslation joint))))]104 (println-repl (.getName %) ":" v)105 (.dot (Vector3f. 1 1 1)106 v))107 (take 2 targets))108 (recur (float (* radius 2))))))))110 (defn connect111 "here are some examples:112 {:type :point}113 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}114 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)116 {:type :cone :limit-xz 0]117 :limit-xy 0]118 :twist 0]} (use XZY rotation mode in blender!)"119 ([#^Node obj-a #^Node obj-b #^Node joint]120 (let [center-a (.getWorldTranslation obj-a)121 center-b (.getWorldTranslation obj-b)122 joint-center (.getWorldTranslation joint)123 pivot-a (.subtract joint-center center-a)124 pivot-b (.subtract joint-center center-b)125 control-a (.getControl obj-a RigidBodyControl)126 control-b (.getControl obj-b RigidBodyControl)]127 ;; A side-effect of creating a joint registers128 ;; it with both physics objects which in turn129 ;; will register the joint with the physics system130 ;; when the simulation is started.131 (if-let [constraints132 (map-vals133 eval134 (read-string135 (meta-data joint "joint")))]137 (let [joint-type (:type constraints)]138 (println-repl "creating joint between"139 (.getName obj-a) "and" (.getName obj-b))140 (cond (= :point joint-type)141 (do142 (println-repl "creating POINT joint")143 (Point2PointJoint.144 control-a145 control-b146 pivot-a147 pivot-b))148 (= :hinge joint-type)149 (do150 (println-repl "creating HINGE joint")151 (let [axis (if-let152 [axis (:axis constraints)]153 axis154 Vector3f/UNIT_X)155 [limit-1 limit-2] (:limit constraints)156 hinge-axis157 (.mult158 (.getWorldRotation joint)159 (blender-to-jme axis))]160 (doto161 (HingeJoint.162 control-a163 control-b164 pivot-a165 pivot-b166 hinge-axis167 hinge-axis)168 (.setLimit limit-1 limit-2))))169 (= :cone joint-type)170 (do171 (let [limit-xy (:limit-xz constraints)172 limit-yz (:limit-xy constraints)173 twist (:twist constraints)]175 (println-repl "creating CONE joint")176 (let [vector-1177 (.mult (.getWorldRotation joint)178 Vector3f/UNIT_X)179 vector-2180 (.normalize181 (.subtract182 (.getWorldTranslation joint)183 (.getWorldTranslation obj-a)))184 ]185 (println-repl186 "vector-1 :" vector-1)187 (println-repl188 "vector-2 :" vector-2)192 (doto193 (ConeJoint.194 control-a195 control-b196 pivot-a197 pivot-b199 ;;(doto (Matrix3f.)200 ;; (.fromStartEndVectors201 ;; Vector3f/UNIT_X202 ;; (.normalize203 ;; (.subtract204 ;; (.getWorldTranslation joint)205 ;; (.getWorldTranslation obj-a)))))207 (.toRotationMatrix (.getWorldRotation joint))209 (.toRotationMatrix210 (.fromAngleAxis (Quaternion.)211 (.angleBetween Vector3f/UNIT_X pivot-a)212 (.cross Vector3f/UNIT_X pivot-a)))214 ;; (doto (Matrix3f.)215 ;; (.fromStartEndVectors216 ;; Vector3f/UNIT_X217 ;; (.normalize218 ;; vector-1)))220 ;; (doto (Matrix3f.)221 ;; (.fromStartEndVectors222 ;; Vector3f/UNIT_X223 ;; vector-2))224 )225 (.setLimit (float limit-xy)226 (float limit-yz)227 (float twist))))))228 true229 (println-repl230 "joint-type" joint-type "not recognized")))232 (println-repl "could not find joint meta-data!")))))234 (defn assemble-creature [#^Node pieces joints]235 (dorun236 (map237 (fn [geom]238 (let [physics-control239 (RigidBodyControl.240 (HullCollisionShape.241 (.getMesh geom))242 (if-let [mass (meta-data geom "mass")]243 (do244 (println-repl245 "setting" (.getName geom) "mass to" (float mass))246 (float mass))247 (float 1)))]249 (.addControl geom physics-control)))250 (filter #(isa? (class %) Geometry )251 (node-seq pieces))))253 (dorun254 (map255 (fn [joint]256 (let [[obj-a obj-b]257 (joint-targets pieces joint)]258 (connect obj-a obj-b joint)))259 joints))260 pieces)262 (defn blender-creature [blender-path]263 (let [model (load-blender-model blender-path)264 joints265 (if-let [joint-node (.getChild model "joints")]266 (seq (.getChildren joint-node))267 (do (println-repl "could not find joints node")268 []))]269 (assemble-creature model joints)))271 (def hand "Models/creature1/one.blend")273 (def worm "Models/creature1/try-again.blend")275 (defn x-ray [#^ColorRGBA color]276 (doto (Material. (asset-manager)277 "Common/MatDefs/Misc/Unshaded.j3md")278 (.setColor "Color" color)279 (-> (.getAdditionalRenderState)280 (.setDepthTest false))))282 (defn test-creature [thing]283 (let [x-axis284 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)285 y-axis286 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)287 z-axis288 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]289 (world290 (nodify [(blender-creature thing)291 (box 10 2 10 :position (Vector3f. 0 -5.5 0)292 :color ColorRGBA/Gray :mass 0)293 x-axis y-axis z-axis294 ])295 standard-debug-controls296 (comp light-up-everything enable-debug297 (fn [world]298 (.setTimer world (NanoTimer.))299 (set-gravity world (Vector3f. 0 0 0))300 (speed-up world)301 world302 ))303 no-op)))305 (defn world-setup [joint]306 (let [top (doto307 (sphere 0.1 :physical? false :color ColorRGBA/Yellow308 :position (Vector3f. 0 7 0))309 (.addControl310 (RigidBodyControl.311 (CapsuleCollisionShape. 0.5 1.5 1) (float 15))))312 bottom (doto313 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray314 :position (Vector3f. 0 -1 0))315 (.addControl316 (RigidBodyControl.317 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))318 table (box 10 2 10 :position (Vector3f. 0 -6 0)319 :color ColorRGBA/Gray :mass 0)320 a (.getControl top RigidBodyControl)321 b (.getControl bottom RigidBodyControl)]322 (cond323 (= joint :point)324 (doto325 (Point2PointJoint. a b326 (Vector3f. 0 -2 0)327 (Vector3f. 0 2 0))328 (.setCollisionBetweenLinkedBodys false))329 (= joint :hinge)330 (doto331 (HingeJoint.332 a b333 (Vector3f. 0 -2 0)334 (Vector3f. 0 2 0)335 (Vector3f. 0 0 1)336 (Vector3f. 0 0 1)338 )339 (.setCollisionBetweenLinkedBodys false)340 ;;(.setLimit (- Math/PI) Math/PI)341 )342 (= joint :cone)343 ;; note to self -- jbullet does NOT implement cone joints344 ;; correctly. You must use plain ol' bullet for this to work.345 ;; It's faster anyway, so whatever.347 (doto (ConeJoint.348 a b349 (Vector3f. 0 -5 0)350 (Vector3f. 0 2 0)352 (doto (Matrix3f.)353 (.fromStartEndVectors Vector3f/UNIT_X354 Vector3f/UNIT_Y))355 (doto (Matrix3f.)356 (.fromStartEndVectors Vector3f/UNIT_X357 (.normalize358 (Vector3f. 0 0 -1))))359 )360 ;;(.setAngularOnly true)362 (.setCollisionBetweenLinkedBodys false)363 (.setLimit (* 1 (/ Math/PI 4))364 (* 1 (/ Math/PI 2))365 (* 0 (/ Math/PI 4)))367 )368 (= joint :six)369 (doto370 (SixDofJoint.371 a b372 (Vector3f. 0 -2 0)373 (Vector3f. 0 2 0)374 ;;(doto (Matrix3f.)375 ;; (.fromStartEndVectors Vector3f/UNIT_X376 ;; Vector3f/UNIT_Y))377 ;;(doto (Matrix3f.)378 ;; (.fromStartEndVectors Vector3f/UNIT_X379 ;; Vector3f/UNIT_Y))380 true)381 (.setCollisionBetweenLinkedBodys false)382 (.setAngularLowerLimit (Vector3f. 0383 (- (/ Math/PI 2))384 0))386 (.setAngularUpperLimit (Vector3f. 0387 (/ Math/PI 2)388 0))389 (.setLinearLowerLimit (Vector3f. 0 0 0))390 (.setLinearUpperLimit (Vector3f. 0 0 0))392 )398 )400 [top bottom table]))403 (defn test-joint [joint]404 (let [[top bottom floor] (world-setup joint)405 control (.getControl top RigidBodyControl)406 move-up? (atom false)407 move-down? (atom false)408 move-left? (atom false)409 move-right? (atom false)410 roll-left? (atom false)411 roll-right? (atom false)412 timer (atom 0)]414 (world415 (nodify [top bottom floor])416 (merge standard-debug-controls417 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))418 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))419 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))420 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))421 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))422 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})424 (fn [world]425 (light-up-everything world)426 (enable-debug world)427 (set-gravity world (Vector3f. 0 0 0))428 )430 (fn [world _]431 (if (zero? (rem (swap! timer inc) 100))432 (do433 ;; (println-repl @timer)434 (.attachChild (.getRootNode world)435 (sphere 0.05 :color ColorRGBA/Yellow436 :position (.getWorldTranslation top)437 :physical? false))))438 (if @move-up?439 (.applyTorque control440 (.mult (.getPhysicsRotation control)441 (Vector3f. 0 0 10))))442 (if @move-down?443 (.applyTorque control444 (.mult (.getPhysicsRotation control)445 (Vector3f. 0 0 -10))))446 (if @move-left?447 (.applyTorque control448 (.mult (.getPhysicsRotation control)449 (Vector3f. 0 10 0))))450 (if @move-right?451 (.applyTorque control452 (.mult (.getPhysicsRotation control)453 (Vector3f. 0 -10 0))))454 (if @roll-left?455 (.applyTorque control456 (.mult (.getPhysicsRotation control)457 (Vector3f. -1 0 0))))458 (if @roll-right?459 (.applyTorque control460 (.mult (.getPhysicsRotation control)461 (Vector3f. 1 0 0))))))))462 #+end_src465 * COMMENT purgatory466 #+begin_src clojure467 (defn bullet-trans []468 (let [obj-a (sphere 0.5 :color ColorRGBA/Red469 :position (Vector3f. -10 5 0))470 obj-b (sphere 0.5 :color ColorRGBA/Blue471 :position (Vector3f. -10 -5 0)472 :mass 0)473 control-a (.getControl obj-a RigidBodyControl)474 control-b (.getControl obj-b RigidBodyControl)475 swivel476 (.toRotationMatrix477 (doto (Quaternion.)478 (.fromAngleAxis (/ Math/PI 2)479 Vector3f/UNIT_X)))]480 (doto481 (ConeJoint.482 control-a control-b483 (Vector3f. 0 5 0)484 (Vector3f. 0 -5 0)485 swivel swivel)486 (.setLimit (* 0.6 (/ Math/PI 4))487 (/ Math/PI 4)488 (* Math/PI 0.8)))489 (world (nodify490 [obj-a obj-b])491 standard-debug-controls492 enable-debug493 no-op)))496 (defn bullet-trans* []497 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red498 :position (Vector3f. 5 0 0)499 :mass 90)500 obj-b (sphere 0.5 :color ColorRGBA/Blue501 :position (Vector3f. -5 0 0)502 :mass 0)503 control-a (.getControl obj-a RigidBodyControl)504 control-b (.getControl obj-b RigidBodyControl)505 move-up? (atom nil)506 move-down? (atom nil)507 move-left? (atom nil)508 move-right? (atom nil)509 roll-left? (atom nil)510 roll-right? (atom nil)511 force 100512 swivel513 (.toRotationMatrix514 (doto (Quaternion.)515 (.fromAngleAxis (/ Math/PI 2)516 Vector3f/UNIT_X)))517 x-move518 (doto (Matrix3f.)519 (.fromStartEndVectors Vector3f/UNIT_X520 (.normalize (Vector3f. 1 1 0))))522 timer (atom 0)]523 (doto524 (ConeJoint.525 control-a control-b526 (Vector3f. -8 0 0)527 (Vector3f. 2 0 0)528 ;;swivel swivel529 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY530 x-move Matrix3f/IDENTITY531 )532 (.setCollisionBetweenLinkedBodys false)533 (.setLimit (* 1 (/ Math/PI 4)) ;; twist534 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane535 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane536 (world (nodify537 [obj-a obj-b])538 (merge standard-debug-controls539 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))540 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))541 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))542 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))543 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))544 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})546 (fn [world]547 (enable-debug world)548 (set-gravity world Vector3f/ZERO)549 )551 (fn [world _]553 (if @move-up?554 (.applyForce control-a555 (Vector3f. force 0 0)556 (Vector3f. 0 0 0)))557 (if @move-down?558 (.applyForce control-a559 (Vector3f. (- force) 0 0)560 (Vector3f. 0 0 0)))561 (if @move-left?562 (.applyForce control-a563 (Vector3f. 0 force 0)564 (Vector3f. 0 0 0)))565 (if @move-right?566 (.applyForce control-a567 (Vector3f. 0 (- force) 0)568 (Vector3f. 0 0 0)))570 (if @roll-left?571 (.applyForce control-a572 (Vector3f. 0 0 force)573 (Vector3f. 0 0 0)))574 (if @roll-right?575 (.applyForce control-a576 (Vector3f. 0 0 (- force))577 (Vector3f. 0 0 0)))579 (if (zero? (rem (swap! timer inc) 100))580 (.attachChild581 (.getRootNode world)582 (sphere 0.05 :color ColorRGBA/Yellow583 :physical? false :position584 (.getWorldTranslation obj-a)))))585 )586 ))590 #+end_src593 * COMMENT generate source594 #+begin_src clojure :tangle ../src/cortex/silly.clj595 <<body-1>>596 #+end_src