Mercurial > cortex
view org/test-creature.org @ 78:77b506ac64f3
trying to make offset cone joints
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 04 Jan 2012 20:34:00 -0700 |
parents | 1f84f425e05d |
children | 01dbdb0d5500 |
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 joint-targets74 "Return the two closest two objects to the joint object, ordered75 from bottom to top according to the joint's rotation."76 [#^Node parts #^Node joint]77 ;;(println (meta-data joint "joint"))78 (.getWorldRotation joint)79 (loop [radius (float 0.01)]80 (let [results (CollisionResults.)]81 (.collideWith82 parts83 (BoundingBox. (.getWorldTranslation joint)84 radius radius radius)85 results)86 (let [targets87 (distinct88 (map #(.getGeometry %) results))]89 (if (>= (count targets) 2)90 (sort-by91 #(.getY92 (.mult93 (.inverse (.getWorldRotation joint))94 (.subtract (.getWorldTranslation %)95 (.getWorldTranslation joint))))96 (take 2 targets))97 (recur (float (* radius 2))))))))99 (defn connect100 "here are some examples:101 {:type :point}102 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}103 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)105 {:type :cone :limit-xz 0]106 :limit-yz 0]107 :twist 0]}"108 ([#^Node obj-a #^Node obj-b #^Node joint]109 (let [center-a (.getWorldTranslation obj-a)110 center-b (.getWorldTranslation obj-b)111 joint-center (.getWorldTranslation joint)112 pivot-a (.subtract joint-center center-a)113 pivot-b (.subtract joint-center center-b)114 control-a (.getControl obj-a RigidBodyControl)115 control-b (.getControl obj-b RigidBodyControl)]116 ;; A side-effect of creating a joint registers117 ;; it with both physics objects which in turn118 ;; will register the joint with the physics system119 ;; when the simulation is started.120 (if-let [constraints121 (map-vals122 eval123 (read-string124 (meta-data joint "joint")))]126 (let [joint-type (:type constraints)]127 (println-repl "creating joint between"128 (.getName obj-a) "and" (.getName obj-b))129 (cond (= :point joint-type)130 (do131 (println-repl "creating POINT joint")132 (Point2PointJoint.133 control-a134 control-b135 pivot-a136 pivot-b))137 (= :hinge joint-type)138 (do139 (println-repl "creating HINGE joint")140 (let [axis (if-let141 [axis (:axis constraints)]142 axis143 Vector3f/UNIT_X)144 [limit-1 limit-2] (:limit constraints)145 hinge-axis146 (.mult147 (.getWorldRotation joint)148 (blender-to-jme axis))]149 (doto150 (HingeJoint.151 control-a152 control-b153 pivot-a154 pivot-b155 hinge-axis156 hinge-axis)157 (.setLimit limit-1 limit-2))))158 (= :cone joint-type)159 (do160 (let [limit-xy (:limit-xz constraints)161 limit-yz (:limit-yz constraints)162 twist (:twist constraints)]164 (println-repl "creating CONE joint")165 (doto166 (ConeJoint.167 control-a168 control-b169 pivot-a170 pivot-b171 (doto (Matrix3f.)172 (.fromStartEndVectors173 Vector3f/UNIT_X174 (.normalize175 (.subtract (.getWorldTranslation joint)176 (.getWorldTranslation obj-a)))))177 (doto (Matrix3f.)178 (.fromStartEndVectors179 Vector3f/UNIT_X180 (.normalize181 (.subtract182 (.getWorldTranslation obj-b)183 (.getWorldTranslation joint))))))184 (.setLimit (float limit-xy)185 (float limit-yz)186 (float twist)))))187 true188 (println-repl189 "joint-type" joint-type "not recognized")))191 (println-repl "could not find joint meta-data!")))))193 (defn assemble-creature [#^Node pieces joints]194 (dorun195 (map196 (fn [geom]197 (let [physics-control198 (RigidBodyControl.199 (HullCollisionShape.200 (.getMesh geom))201 (if-let [mass (meta-data geom "mass")]202 (do203 (println-repl204 "setting" (.getName geom) "mass to" (float mass))205 (float mass))206 (float 1)))]208 (.addControl geom physics-control)))209 (filter #(isa? (class %) Geometry )210 (node-seq pieces))))212 (dorun213 (map214 (fn [joint]215 (let [[obj-a obj-b]216 (joint-targets pieces joint)]217 (connect obj-a obj-b joint)))218 joints))219 pieces)221 (defn blender-creature [blender-path]222 (let [model (load-blender-model blender-path)223 joints224 (if-let [joint-node (.getChild model "joints")]225 (seq (.getChildren joint-node))226 (do (println-repl "could not find joints node")227 []))]228 (assemble-creature model joints)))230 (def hand "Models/creature1/one.blend")232 (def worm "Models/creature1/try-again.blend")234 (defn test-creature [thing]235 (world236 (nodify [(blender-creature thing)237 (box 10 2 10 :position (Vector3f. 0 -5.5 0)238 :color ColorRGBA/Gray :mass 0)])239 standard-debug-controls240 (comp light-up-everything enable-debug241 (fn [world]242 (.setTimer world (NanoTimer.))243 ;;(set-gravity world (Vector3f. 0 0 0))244 (speed-up world)245 world246 ))247 no-op))249 (defn world-setup [joint]250 (let [top (doto251 (sphere 0.1 :physical? false :color ColorRGBA/Yellow252 :position (Vector3f. 0 7 0))253 (.addControl254 (RigidBodyControl.255 (CapsuleCollisionShape. 0.5 1.5 1) (float 15))))256 bottom (doto257 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray258 :position (Vector3f. 0 -1 0))259 (.addControl260 (RigidBodyControl.261 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))262 table (box 10 2 10 :position (Vector3f. 0 -6 0)263 :color ColorRGBA/Gray :mass 0)264 a (.getControl top RigidBodyControl)265 b (.getControl bottom RigidBodyControl)]266 (cond267 (= joint :point)268 (doto269 (Point2PointJoint. a b270 (Vector3f. 0 -2 0)271 (Vector3f. 0 2 0))272 (.setCollisionBetweenLinkedBodys false))273 (= joint :hinge)274 (doto275 (HingeJoint.276 a b277 (Vector3f. 0 -2 0)278 (Vector3f. 0 2 0)279 (Vector3f. 0 0 1)280 (Vector3f. 0 0 1)282 )283 (.setCollisionBetweenLinkedBodys false)284 ;;(.setLimit (- Math/PI) Math/PI)285 )286 (= joint :cone)287 ;; note to self -- jbullet does NOT implement cone joints288 ;; correctly. You must use plain ol' bullet for this to work.289 ;; It's faster anyway, so whatever.291 (doto (ConeJoint.292 a b293 (Vector3f. 0 -5 0)294 (Vector3f. 0 2 0)296 (doto (Matrix3f.)297 (.fromStartEndVectors Vector3f/UNIT_X298 Vector3f/UNIT_Y))299 (doto (Matrix3f.)300 (.fromStartEndVectors Vector3f/UNIT_X301 (.normalize302 (Vector3f. 5 5 0))))303 )304 ;;(.setAngularOnly true)306 (.setCollisionBetweenLinkedBodys false)307 (.setLimit (* 1 (/ Math/PI 4))308 (* 1 (/ Math/PI 2))309 (* 0 (/ Math/PI 4)))311 )312 (= joint :six)313 (doto314 (SixDofJoint.315 a b316 (Vector3f. 0 -2 0)317 (Vector3f. 0 2 0)318 ;;(doto (Matrix3f.)319 ;; (.fromStartEndVectors Vector3f/UNIT_X320 ;; Vector3f/UNIT_Y))321 ;;(doto (Matrix3f.)322 ;; (.fromStartEndVectors Vector3f/UNIT_X323 ;; Vector3f/UNIT_Y))324 true)325 (.setCollisionBetweenLinkedBodys false)326 (.setAngularLowerLimit (Vector3f. 0327 (- (/ Math/PI 2))328 0))330 (.setAngularUpperLimit (Vector3f. 0331 (/ Math/PI 2)332 0))333 (.setLinearLowerLimit (Vector3f. 0 0 0))334 (.setLinearUpperLimit (Vector3f. 0 0 0))336 )342 )344 [top bottom table]))347 (defn test-joint [joint]348 (let [[top bottom floor] (world-setup joint)349 control (.getControl top RigidBodyControl)350 move-up? (atom false)351 move-down? (atom false)352 move-left? (atom false)353 move-right? (atom false)354 roll-left? (atom false)355 roll-right? (atom false)356 timer (atom 0)]358 (world359 (nodify [top bottom floor])360 (merge standard-debug-controls361 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))362 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))363 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))364 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))365 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))366 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})368 (fn [world]369 (light-up-everything world)370 (enable-debug world)371 (set-gravity world (Vector3f. 0 0 0))372 )374 (fn [world _]375 (if (zero? (rem (swap! timer inc) 100))376 (do377 ;; (println-repl @timer)378 (.attachChild (.getRootNode world)379 (sphere 0.05 :color ColorRGBA/Yellow380 :position (.getWorldTranslation top)381 :physical? false))))382 (if @move-up?383 (.applyTorque control384 (.mult (.getPhysicsRotation control)385 (Vector3f. 0 0 10))))386 (if @move-down?387 (.applyTorque control388 (.mult (.getPhysicsRotation control)389 (Vector3f. 0 0 -10))))390 (if @move-left?391 (.applyTorque control392 (.mult (.getPhysicsRotation control)393 (Vector3f. 0 10 0))))394 (if @move-right?395 (.applyTorque control396 (.mult (.getPhysicsRotation control)397 (Vector3f. 0 -10 0))))398 (if @roll-left?399 (.applyTorque control400 (.mult (.getPhysicsRotation control)401 (Vector3f. -1 0 0))))402 (if @roll-right?403 (.applyTorque control404 (.mult (.getPhysicsRotation control)405 (Vector3f. 1 0 0))))))))406 #+end_src409 * COMMENT purgatory410 #+begin_src clojure411 (defn bullet-trans []412 (let [obj-a (sphere 0.5 :color ColorRGBA/Red413 :position (Vector3f. -10 5 0))414 obj-b (sphere 0.5 :color ColorRGBA/Blue415 :position (Vector3f. -10 -5 0)416 :mass 0)417 control-a (.getControl obj-a RigidBodyControl)418 control-b (.getControl obj-b RigidBodyControl)419 swivel420 (.toRotationMatrix421 (doto (Quaternion.)422 (.fromAngleAxis (/ Math/PI 2)423 Vector3f/UNIT_X)))]424 (doto425 (ConeJoint.426 control-a control-b427 (Vector3f. 0 5 0)428 (Vector3f. 0 -5 0)429 swivel swivel)430 (.setLimit (* 0.6 (/ Math/PI 4))431 (/ Math/PI 4)432 (* Math/PI 0.8)))433 (world (nodify434 [obj-a obj-b])435 standard-debug-controls436 enable-debug437 no-op)))440 (defn bullet-trans* []441 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red442 :position (Vector3f. 5 0 0)443 :mass 90)444 obj-b (sphere 0.5 :color ColorRGBA/Blue445 :position (Vector3f. -5 0 0)446 :mass 0)447 control-a (.getControl obj-a RigidBodyControl)448 control-b (.getControl obj-b RigidBodyControl)449 move-up? (atom nil)450 move-down? (atom nil)451 move-left? (atom nil)452 move-right? (atom nil)453 roll-left? (atom nil)454 roll-right? (atom nil)455 force 100456 swivel457 (.toRotationMatrix458 (doto (Quaternion.)459 (.fromAngleAxis (/ Math/PI 2)460 Vector3f/UNIT_X)))461 x-move462 (doto (Matrix3f.)463 (.fromStartEndVectors Vector3f/UNIT_X464 (.normalize (Vector3f. 1 1 0))))466 timer (atom 0)]467 (doto468 (ConeJoint.469 control-a control-b470 (Vector3f. -8 0 0)471 (Vector3f. 2 0 0)472 ;;swivel swivel473 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY474 x-move Matrix3f/IDENTITY475 )476 (.setCollisionBetweenLinkedBodys false)477 (.setLimit (* 1 (/ Math/PI 4)) ;; twist478 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane479 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane480 (world (nodify481 [obj-a obj-b])482 (merge standard-debug-controls483 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))484 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))485 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))486 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))487 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))488 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})490 (fn [world]491 (enable-debug world)492 (set-gravity world Vector3f/ZERO)493 )495 (fn [world _]497 (if @move-up?498 (.applyForce control-a499 (Vector3f. force 0 0)500 (Vector3f. 0 0 0)))501 (if @move-down?502 (.applyForce control-a503 (Vector3f. (- force) 0 0)504 (Vector3f. 0 0 0)))505 (if @move-left?506 (.applyForce control-a507 (Vector3f. 0 force 0)508 (Vector3f. 0 0 0)))509 (if @move-right?510 (.applyForce control-a511 (Vector3f. 0 (- force) 0)512 (Vector3f. 0 0 0)))514 (if @roll-left?515 (.applyForce control-a516 (Vector3f. 0 0 force)517 (Vector3f. 0 0 0)))518 (if @roll-right?519 (.applyForce control-a520 (Vector3f. 0 0 (- force))521 (Vector3f. 0 0 0)))523 (if (zero? (rem (swap! timer inc) 100))524 (.attachChild525 (.getRootNode world)526 (sphere 0.05 :color ColorRGBA/Yellow527 :physical? false :position528 (.getWorldTranslation obj-a)))))529 )530 ))534 #+end_src537 * COMMENT generate source538 #+begin_src clojure :tangle ../src/cortex/silly.clj539 <<body-1>>540 #+end_src