Mercurial > cortex
view org/test-creature.org @ 82:6b4ca076285e
making some progress on cone joints...
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 06 Jan 2012 02:03:43 -0700 |
parents | 10f495560c59 |
children | 14b604e955ed |
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-xz (:limit-xz constraints)172 limit-xy (:limit-xy constraints)173 twist (:twist constraints)]175 (println-repl "creating CONE joint")176 (let [frame-a177 (.toRotationMatrix178 (doto (Quaternion.)179 (.fromAngleAxis180 (float181 (.angleBetween182 pivot-a Vector3f/UNIT_X))183 (.cross (.normalize pivot-a)184 Vector3f/UNIT_X))))185 ]186 (println-repl187 "angle between pivot-a (" pivot-a ") and UNIT_X is"188 (.angleBetween Vector3f/UNIT_X (.normalize pivot-a)))192 (doto193 (ConeJoint.194 control-a195 control-b196 pivot-a197 pivot-b200 ;; ;; frame-in-A201 frame-a202 frame-a204 ;; (.toRotationMatrix205 ;; (doto (Quaternion.)206 ;; (.fromAngles207 ;; 0 0 (* -1 (/ Math/PI 2)))))210 ;; ;; frame-in-B211 ;; (.toRotationMatrix212 ;; (doto (Quaternion.)213 ;; (.fromAngles214 ;; 0 0 (* -1.2 (/ Math/PI 2)))))217 )218 (.setLimit (float limit-xz)219 (float limit-xy)220 (float twist))))))221 true222 (println-repl223 "joint-type" joint-type "not recognized")))225 (println-repl "could not find joint meta-data!"))))227 (defn assemble-creature [#^Node pieces joints]228 (dorun229 (map230 (fn [geom]231 (let [physics-control232 (RigidBodyControl.233 (HullCollisionShape.234 (.getMesh geom))235 (if-let [mass (meta-data geom "mass")]236 (do237 (println-repl238 "setting" (.getName geom) "mass to" (float mass))239 (float mass))240 (float 1)))]242 (.addControl geom physics-control)))243 (filter #(isa? (class %) Geometry )244 (node-seq pieces))))246 (dorun247 (map248 (fn [joint]249 (let [[obj-a obj-b]250 (joint-targets pieces joint)]251 (connect obj-a obj-b joint)))252 joints))253 pieces)255 (defn blender-creature [blender-path]256 (let [model (load-blender-model blender-path)257 joints258 (if-let [joint-node (.getChild model "joints")]259 (seq (.getChildren joint-node))260 (do (println-repl "could not find joints node")261 []))]262 (assemble-creature model joints)))264 (def hand "Models/creature1/one.blend")266 (def worm "Models/creature1/try-again.blend")268 (defn x-ray [#^ColorRGBA color]269 (doto (Material. (asset-manager)270 "Common/MatDefs/Misc/Unshaded.j3md")271 (.setColor "Color" color)272 (-> (.getAdditionalRenderState)273 (.setDepthTest false))))275 (defn test-creature [thing]276 (let [x-axis277 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)278 y-axis279 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)280 z-axis281 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]282 (world283 (nodify [(blender-creature thing)284 (box 10 2 10 :position (Vector3f. 0 -9 0)285 :color ColorRGBA/Gray :mass 0)286 x-axis y-axis z-axis287 ])288 standard-debug-controls289 (comp light-up-everything enable-debug290 (fn [world]291 (.setTimer world (NanoTimer.))292 (set-gravity world (Vector3f. 0 0 0))293 (speed-up world)294 world295 ))296 no-op)))298 (defn world-setup [joint]299 (let [top (doto300 (sphere 0.1 :physical? false :color ColorRGBA/Yellow301 :position (Vector3f. 0 7 0))302 (.addControl303 (RigidBodyControl.304 (CapsuleCollisionShape. 0.5 1.5 1) (float 15))))305 bottom (doto306 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray307 :position (Vector3f. 0 -1 0))308 (.addControl309 (RigidBodyControl.310 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))311 table (box 10 2 10 :position (Vector3f. 0 -6 0)312 :color ColorRGBA/Gray :mass 0)313 a (.getControl top RigidBodyControl)314 b (.getControl bottom RigidBodyControl)]315 (cond316 (= joint :point)317 (doto318 (Point2PointJoint. a b319 (Vector3f. 0 -2 0)320 (Vector3f. 0 2 0))321 (.setCollisionBetweenLinkedBodys false))322 (= joint :hinge)323 (doto324 (HingeJoint.325 a b326 (Vector3f. 0 -2 0)327 (Vector3f. 0 2 0)328 (Vector3f. 0 0 1)329 (Vector3f. 0 0 1)331 )332 (.setCollisionBetweenLinkedBodys false)333 ;;(.setLimit (- Math/PI) Math/PI)334 )335 (= joint :cone)336 ;; note to self -- jbullet does NOT implement cone joints337 ;; correctly. You must use plain ol' bullet for this to work.338 ;; It's faster anyway, so whatever.340 (doto (ConeJoint.341 a b342 (Vector3f. 0 -5 0)343 (Vector3f. 0 2 0)345 (doto (Matrix3f.)346 (.fromStartEndVectors Vector3f/UNIT_X347 Vector3f/UNIT_Y))348 (doto (Matrix3f.)349 (.fromStartEndVectors Vector3f/UNIT_X350 (.normalize351 (Vector3f. 0 0 -1))))352 )353 ;;(.setAngularOnly true)355 (.setCollisionBetweenLinkedBodys false)356 (.setLimit (* 1 (/ Math/PI 4))357 (* 1 (/ Math/PI 2))358 (* 0 (/ Math/PI 4)))360 )361 (= joint :six)362 (doto363 (SixDofJoint.364 a b365 (Vector3f. 0 -2 0)366 (Vector3f. 0 2 0)367 ;;(doto (Matrix3f.)368 ;; (.fromStartEndVectors Vector3f/UNIT_X369 ;; Vector3f/UNIT_Y))370 ;;(doto (Matrix3f.)371 ;; (.fromStartEndVectors Vector3f/UNIT_X372 ;; Vector3f/UNIT_Y))373 true)374 (.setCollisionBetweenLinkedBodys false)375 (.setAngularLowerLimit (Vector3f. 0376 (- (/ Math/PI 2))377 0))379 (.setAngularUpperLimit (Vector3f. 0380 (/ Math/PI 2)381 0))382 (.setLinearLowerLimit (Vector3f. 0 0 0))383 (.setLinearUpperLimit (Vector3f. 0 0 0))385 )391 )393 [top bottom table]))396 (defn test-joint [joint]397 (let [[top bottom floor] (world-setup joint)398 control (.getControl top RigidBodyControl)399 move-up? (atom false)400 move-down? (atom false)401 move-left? (atom false)402 move-right? (atom false)403 roll-left? (atom false)404 roll-right? (atom false)405 timer (atom 0)]407 (world408 (nodify [top bottom floor])409 (merge standard-debug-controls410 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))411 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))412 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))413 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))414 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))415 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})417 (fn [world]418 (light-up-everything world)419 (enable-debug world)420 (set-gravity world (Vector3f. 0 0 0))421 )423 (fn [world _]424 (if (zero? (rem (swap! timer inc) 100))425 (do426 ;; (println-repl @timer)427 (.attachChild (.getRootNode world)428 (sphere 0.05 :color ColorRGBA/Yellow429 :position (.getWorldTranslation top)430 :physical? false))))431 (if @move-up?432 (.applyTorque control433 (.mult (.getPhysicsRotation control)434 (Vector3f. 0 0 10))))435 (if @move-down?436 (.applyTorque control437 (.mult (.getPhysicsRotation control)438 (Vector3f. 0 0 -10))))439 (if @move-left?440 (.applyTorque control441 (.mult (.getPhysicsRotation control)442 (Vector3f. 0 10 0))))443 (if @move-right?444 (.applyTorque control445 (.mult (.getPhysicsRotation control)446 (Vector3f. 0 -10 0))))447 (if @roll-left?448 (.applyTorque control449 (.mult (.getPhysicsRotation control)450 (Vector3f. -1 0 0))))451 (if @roll-right?452 (.applyTorque control453 (.mult (.getPhysicsRotation control)454 (Vector3f. 1 0 0))))))))455 #+end_src458 * COMMENT purgatory459 #+begin_src clojure460 (defn bullet-trans []461 (let [obj-a (sphere 0.5 :color ColorRGBA/Red462 :position (Vector3f. -10 5 0))463 obj-b (sphere 0.5 :color ColorRGBA/Blue464 :position (Vector3f. -10 -5 0)465 :mass 0)466 control-a (.getControl obj-a RigidBodyControl)467 control-b (.getControl obj-b RigidBodyControl)468 swivel469 (.toRotationMatrix470 (doto (Quaternion.)471 (.fromAngleAxis (/ Math/PI 2)472 Vector3f/UNIT_X)))]473 (doto474 (ConeJoint.475 control-a control-b476 (Vector3f. 0 5 0)477 (Vector3f. 0 -5 0)478 swivel swivel)479 (.setLimit (* 0.6 (/ Math/PI 4))480 (/ Math/PI 4)481 (* Math/PI 0.8)))482 (world (nodify483 [obj-a obj-b])484 standard-debug-controls485 enable-debug486 no-op)))489 (defn bullet-trans* []490 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red491 :position (Vector3f. 5 0 0)492 :mass 90)493 obj-b (sphere 0.5 :color ColorRGBA/Blue494 :position (Vector3f. -5 0 0)495 :mass 0)496 control-a (.getControl obj-a RigidBodyControl)497 control-b (.getControl obj-b RigidBodyControl)498 move-up? (atom nil)499 move-down? (atom nil)500 move-left? (atom nil)501 move-right? (atom nil)502 roll-left? (atom nil)503 roll-right? (atom nil)504 force 100505 swivel506 (.toRotationMatrix507 (doto (Quaternion.)508 (.fromAngleAxis (/ Math/PI 2)509 Vector3f/UNIT_X)))510 x-move511 (doto (Matrix3f.)512 (.fromStartEndVectors Vector3f/UNIT_X513 (.normalize (Vector3f. 1 1 0))))515 timer (atom 0)]516 (doto517 (ConeJoint.518 control-a control-b519 (Vector3f. -8 0 0)520 (Vector3f. 2 0 0)521 ;;swivel swivel522 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY523 x-move Matrix3f/IDENTITY524 )525 (.setCollisionBetweenLinkedBodys false)526 (.setLimit (* 1 (/ Math/PI 4)) ;; twist527 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane528 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane529 (world (nodify530 [obj-a obj-b])531 (merge standard-debug-controls532 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))533 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))534 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))535 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))536 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))537 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})539 (fn [world]540 (enable-debug world)541 (set-gravity world Vector3f/ZERO)542 )544 (fn [world _]546 (if @move-up?547 (.applyForce control-a548 (Vector3f. force 0 0)549 (Vector3f. 0 0 0)))550 (if @move-down?551 (.applyForce control-a552 (Vector3f. (- force) 0 0)553 (Vector3f. 0 0 0)))554 (if @move-left?555 (.applyForce control-a556 (Vector3f. 0 force 0)557 (Vector3f. 0 0 0)))558 (if @move-right?559 (.applyForce control-a560 (Vector3f. 0 (- force) 0)561 (Vector3f. 0 0 0)))563 (if @roll-left?564 (.applyForce control-a565 (Vector3f. 0 0 force)566 (Vector3f. 0 0 0)))567 (if @roll-right?568 (.applyForce control-a569 (Vector3f. 0 0 (- force))570 (Vector3f. 0 0 0)))572 (if (zero? (rem (swap! timer inc) 100))573 (.attachChild574 (.getRootNode world)575 (sphere 0.05 :color ColorRGBA/Yellow576 :physical? false :position577 (.getWorldTranslation obj-a)))))578 )579 ))583 #+end_src586 * COMMENT generate source587 #+begin_src clojure :tangle ../src/cortex/silly.clj588 <<body-1>>589 #+end_src