rlm@73: #+title: First attempt at a creature! rlm@73: #+author: Robert McIntyre rlm@73: #+email: rlm@mit.edu rlm@73: #+description: rlm@73: #+keywords: simulation, jMonkeyEngine3, clojure rlm@73: #+SETUPFILE: ../../aurellem/org/setup.org rlm@73: #+INCLUDE: ../../aurellem/org/level-0.org rlm@73: rlm@73: * Intro rlm@73: So far, I've made the following senses -- rlm@73: - Vision rlm@73: - Hearing rlm@73: - Touch rlm@73: - Proprioception rlm@73: rlm@73: And one effector: rlm@73: - Movement rlm@73: rlm@73: However, the code so far has only enabled these senses, but has not rlm@73: actually implemented them. For example, there is still a lot of work rlm@73: to be done for vision. I need to be able to create an /eyeball/ in rlm@73: simulation that can be moved around and see the world from different rlm@73: angles. I also need to determine weather to use log-polar or cartesian rlm@73: for the visual input, and I need to determine how/wether to rlm@73: disceritise the visual input. rlm@73: rlm@73: I also want to be able to visualize both the sensors and the rlm@73: effectors in pretty pictures. This semi-retarted creature will by my rlm@73: first attempt at bringing everything together. rlm@73: rlm@73: * The creature's body rlm@73: rlm@73: Still going to do an eve-like body in blender, but due to problems rlm@73: importing the joints, etc into jMonkeyEngine3, I',m going to do all rlm@73: the connecting here in clojure code, using the names of the individual rlm@73: components and trial and error. Later, I'll maybe make some sort of rlm@73: creature-building modifications to blender that support whatever rlm@73: discreitized senses I'm going to make. rlm@73: rlm@73: #+name: body-1 rlm@73: #+begin_src clojure rlm@73: (ns cortex.silly rlm@73: "let's play!" rlm@73: {:author "Robert McIntyre"}) rlm@73: rlm@73: ;; TODO remove this! rlm@73: (require 'cortex.import) rlm@73: (cortex.import/mega-import-jme3) rlm@73: (use '(cortex world util body hearing touch vision)) rlm@73: rlm@73: (use '[clojure.contrib [seq :only [find-first]]]) rlm@73: rlm@73: rlm@73: (rlm.rlm-commands/help) rlm@73: rlm@73: (defn load-blender-model rlm@73: "Load a .blend file using an asset folder relative path." rlm@73: [^String model] rlm@73: (.loadModel rlm@73: (doto (asset-manager) rlm@73: (.registerLoader BlenderModelLoader (into-array String ["blend"]))) rlm@73: model)) rlm@73: rlm@74: (defn meta-data [blender-node key] rlm@74: (if-let [data (.getUserData blender-node "properties")] rlm@74: (.findValue data key) rlm@74: nil)) rlm@73: rlm@74: rlm@74: (defn hand [] rlm@74: (load-blender-model "Models/creature1/one.blend")) rlm@74: rlm@74: rlm@73: rlm@74: (def hand-names rlm@74: #{ rlm@74: "middle-1" rlm@74: "middle-2" rlm@74: "middle-3" rlm@74: "palm" rlm@74: "pinky-1" rlm@74: "pinky-2" rlm@74: "pinky-3" rlm@74: "pointer-1" rlm@74: "pointer-2" rlm@74: "pointer-3" rlm@74: "ring-1" rlm@74: "ring-2" rlm@74: "ring-3" rlm@74: "thumb-1" rlm@74: "thumb-2"}) rlm@74: rlm@74: (defn hand-pieces [] rlm@74: (filter rlm@74: (comp hand-names #(apply str (drop-last (.getName %)))) (node-seq (hand)))) rlm@74: rlm@74: (defn hand-joints [] rlm@74: (map #(.getWorldTranslation %) rlm@74: (filter #(re-matches #"joint\.\d\d\d" (.getName %)) rlm@74: (node-seq (hand))))) rlm@77: (defn worm [] rlm@77: (load-blender-model "Models/creature1/try-again.blend")) rlm@77: rlm@74: rlm@74: (defn worm-pieces [] rlm@74: (filter rlm@74: (comp #{"worm-2" "worm-1"} rlm@74: #(apply str (drop-last (.getName %)))) rlm@77: (node-seq (worm)))) rlm@74: rlm@74: (defn worm-joints [] rlm@77: (filter #(re-matches #"joint\.\d\d\d" (.getName %)) rlm@77: (node-seq (worm)))) rlm@74: rlm@77: (defn bullet-trans [] rlm@77: (let [obj-a (sphere 0.5 :color ColorRGBA/Red rlm@77: :position (Vector3f. -10 5 0)) rlm@77: obj-b (sphere 0.5 :color ColorRGBA/Blue rlm@77: :position (Vector3f. -10 -5 0) rlm@77: :mass 0) rlm@77: control-a (.getControl obj-a RigidBodyControl) rlm@77: control-b (.getControl obj-b RigidBodyControl) rlm@77: swivel rlm@77: (.toRotationMatrix rlm@77: (doto (Quaternion.) rlm@77: (.fromAngleAxis (/ Math/PI 2) rlm@77: Vector3f/UNIT_X)))] rlm@77: (doto rlm@77: (ConeJoint. rlm@77: control-a control-b rlm@77: (Vector3f. 0 5 0) rlm@77: (Vector3f. 0 -5 0) rlm@77: swivel swivel) rlm@77: (.setLimit (* 0.6 (/ Math/PI 4)) rlm@77: (/ Math/PI 4) rlm@77: (* Math/PI 0.8))) rlm@77: (world (nodify rlm@77: [obj-a obj-b]) rlm@77: standard-debug-controls rlm@77: enable-debug rlm@77: no-op))) rlm@74: rlm@74: rlm@77: (defn bullet-trans* [] rlm@77: (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red rlm@77: :position (Vector3f. 5 0 0) rlm@77: :mass 90) rlm@77: obj-b (sphere 0.5 :color ColorRGBA/Blue rlm@77: :position (Vector3f. -5 0 0) rlm@77: :mass 0) rlm@77: control-a (.getControl obj-a RigidBodyControl) rlm@77: control-b (.getControl obj-b RigidBodyControl) rlm@77: move-up? (atom nil) rlm@77: move-down? (atom nil) rlm@77: move-left? (atom nil) rlm@77: move-right? (atom nil) rlm@77: roll-left? (atom nil) rlm@77: roll-right? (atom nil) rlm@77: force 100 rlm@77: swivel rlm@77: (.toRotationMatrix rlm@77: (doto (Quaternion.) rlm@77: (.fromAngleAxis (/ Math/PI 2) rlm@77: Vector3f/UNIT_X))) rlm@77: x-move rlm@77: (doto (Matrix3f.) rlm@77: (.fromStartEndVectors Vector3f/UNIT_X rlm@77: (.normalize (Vector3f. 1 1 0)))) rlm@77: rlm@77: timer (atom 0)] rlm@77: (doto rlm@77: (ConeJoint. rlm@77: control-a control-b rlm@77: (Vector3f. -8 0 0) rlm@77: (Vector3f. 2 0 0) rlm@77: ;;swivel swivel rlm@77: ;;Matrix3f/IDENTITY Matrix3f/IDENTITY rlm@77: x-move Matrix3f/IDENTITY rlm@77: ) rlm@77: (.setCollisionBetweenLinkedBodys false) rlm@77: (.setLimit (* 1 (/ Math/PI 4)) ;; twist rlm@77: (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane rlm@77: (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane rlm@77: (world (nodify rlm@77: [obj-a obj-b]) rlm@77: (merge standard-debug-controls rlm@77: {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) rlm@77: "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) rlm@77: "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) rlm@77: "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) rlm@77: "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) rlm@77: "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) rlm@77: rlm@77: (fn [world] rlm@77: (enable-debug world) rlm@77: (set-gravity world Vector3f/ZERO) rlm@77: ) rlm@77: rlm@77: (fn [world _] rlm@77: rlm@77: (if @move-up? rlm@77: (.applyForce control-a rlm@77: (Vector3f. force 0 0) rlm@77: (Vector3f. 0 0 0))) rlm@77: (if @move-down? rlm@77: (.applyForce control-a rlm@77: (Vector3f. (- force) 0 0) rlm@77: (Vector3f. 0 0 0))) rlm@77: (if @move-left? rlm@77: (.applyForce control-a rlm@77: (Vector3f. 0 force 0) rlm@77: (Vector3f. 0 0 0))) rlm@77: (if @move-right? rlm@77: (.applyForce control-a rlm@77: (Vector3f. 0 (- force) 0) rlm@77: (Vector3f. 0 0 0))) rlm@77: rlm@77: (if @roll-left? rlm@77: (.applyForce control-a rlm@77: (Vector3f. 0 0 force) rlm@77: (Vector3f. 0 0 0))) rlm@77: (if @roll-right? rlm@77: (.applyForce control-a rlm@77: (Vector3f. 0 0 (- force)) rlm@77: (Vector3f. 0 0 0))) rlm@77: rlm@77: (if (zero? (rem (swap! timer inc) 100)) rlm@77: (.attachChild rlm@77: (.getRootNode world) rlm@77: (sphere 0.05 :color ColorRGBA/Yellow rlm@77: :physical? false :position rlm@77: (.getWorldTranslation obj-a))))) rlm@77: ) rlm@77: )) rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: (defn world-setup [joint] rlm@77: (let [top (doto rlm@77: (sphere 0.1 :physical? false :color ColorRGBA/Yellow rlm@77: :position (Vector3f. 0 7 0)) rlm@77: (.addControl rlm@77: (RigidBodyControl. rlm@77: (CapsuleCollisionShape. 0.5 1.5 1) (float 15)))) rlm@77: bottom (doto rlm@77: (sphere 0.1 :physical? false :color ColorRGBA/DarkGray rlm@77: :position (Vector3f. 0 -1 0)) rlm@77: (.addControl rlm@77: (RigidBodyControl. rlm@77: (CapsuleCollisionShape. 0.5 1.5 1) (float 0)))) rlm@77: table (box 10 2 10 :position (Vector3f. 0 -6 0) rlm@77: :color ColorRGBA/Gray :mass 0) rlm@77: a (.getControl top RigidBodyControl) rlm@77: b (.getControl bottom RigidBodyControl)] rlm@77: (cond rlm@77: (= joint :point) rlm@77: (doto rlm@77: (Point2PointJoint. a b rlm@77: (Vector3f. 0 -2 0) rlm@77: (Vector3f. 0 2 0)) rlm@77: (.setCollisionBetweenLinkedBodys false)) rlm@77: (= joint :hinge) rlm@77: (doto rlm@77: (HingeJoint. rlm@77: a b rlm@77: (Vector3f. 0 -2 0) rlm@77: (Vector3f. 0 2 0) rlm@77: (Vector3f. 0 0 1) rlm@77: (Vector3f. 0 0 1) rlm@77: rlm@77: ) rlm@77: (.setCollisionBetweenLinkedBodys false) rlm@77: ;;(.setLimit (- Math/PI) Math/PI) rlm@77: ) rlm@77: (= joint :cone) rlm@77: ;; note to self -- jbullet does NOT implement cone joints rlm@77: ;; correctly. You must use plain ol' bullet for this to work. rlm@77: ;; It's faster anyway, so whatever. rlm@77: rlm@77: (doto (ConeJoint. rlm@77: a b rlm@77: (Vector3f. 0 -5 0) rlm@77: (Vector3f. 0 2 0) rlm@77: rlm@77: (doto (Matrix3f.) rlm@77: (.fromStartEndVectors Vector3f/UNIT_X rlm@77: Vector3f/UNIT_Y)) rlm@77: (doto (Matrix3f.) rlm@77: (.fromStartEndVectors Vector3f/UNIT_X rlm@77: Vector3f/UNIT_Y)) rlm@77: ) rlm@77: ;;(.setAngularOnly true) rlm@77: rlm@77: (.setCollisionBetweenLinkedBodys false) rlm@77: (.setLimit (* 1 (/ Math/PI 4)) rlm@77: (* 1 (/ Math/PI 4)) rlm@77: (* 0 (/ Math/PI 4))) rlm@77: rlm@77: ) rlm@77: (= joint :six) rlm@77: (doto rlm@77: (SixDofJoint. rlm@77: a b rlm@77: (Vector3f. 0 -2 0) rlm@77: (Vector3f. 0 2 0) rlm@77: ;;(doto (Matrix3f.) rlm@77: ;; (.fromStartEndVectors Vector3f/UNIT_X rlm@77: ;; Vector3f/UNIT_Y)) rlm@77: ;;(doto (Matrix3f.) rlm@77: ;; (.fromStartEndVectors Vector3f/UNIT_X rlm@77: ;; Vector3f/UNIT_Y)) rlm@77: true) rlm@77: (.setCollisionBetweenLinkedBodys false) rlm@77: (.setAngularLowerLimit (Vector3f. 0 rlm@77: (- (/ Math/PI 2)) rlm@77: 0)) rlm@77: rlm@77: (.setAngularUpperLimit (Vector3f. 0 rlm@77: (/ Math/PI 2) rlm@77: 0)) rlm@77: (.setLinearLowerLimit (Vector3f. 0 0 0)) rlm@77: (.setLinearUpperLimit (Vector3f. 0 0 0)) rlm@77: rlm@77: ) rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: rlm@77: ) rlm@77: rlm@77: [top bottom table])) rlm@77: rlm@77: (defn speed-up [world] rlm@77: (.setMoveSpeed (.getFlyByCamera world) rlm@77: (float 100)) rlm@77: (.setRotationSpeed (.getFlyByCamera world) rlm@77: (float 20)) rlm@77: world) rlm@77: rlm@77: rlm@77: (defn test-joint [joint] rlm@77: (let [[top bottom floor] (world-setup joint) rlm@77: control (.getControl top RigidBodyControl) rlm@77: move-up? (atom false) rlm@77: move-down? (atom false) rlm@77: move-left? (atom false) rlm@77: move-right? (atom false) rlm@77: roll-left? (atom false) rlm@77: roll-right? (atom false) rlm@77: timer (atom 0)] rlm@77: rlm@77: (world rlm@77: (nodify [top bottom floor]) rlm@77: (merge standard-debug-controls rlm@77: {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) rlm@77: "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) rlm@77: "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) rlm@77: "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) rlm@77: "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) rlm@77: "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) rlm@77: rlm@77: (fn [world] rlm@77: (light-up-everything world) rlm@77: (enable-debug world) rlm@77: (set-gravity world (Vector3f. 0 0 0)) rlm@77: ) rlm@77: rlm@77: (fn [world _] rlm@77: (if (zero? (rem (swap! timer inc) 100)) rlm@77: (do rlm@77: ;; (println-repl @timer) rlm@77: (.attachChild (.getRootNode world) rlm@77: (sphere 0.05 :color ColorRGBA/Yellow rlm@77: :position (.getWorldTranslation top) rlm@77: :physical? false)))) rlm@77: (if @move-up? rlm@77: (.applyTorque control rlm@77: (.mult (.getPhysicsRotation control) rlm@77: (Vector3f. 0 0 10)))) rlm@77: (if @move-down? rlm@77: (.applyTorque control rlm@77: (.mult (.getPhysicsRotation control) rlm@77: (Vector3f. 0 0 -10)))) rlm@77: (if @move-left? rlm@77: (.applyTorque control rlm@77: (.mult (.getPhysicsRotation control) rlm@77: (Vector3f. 0 10 0)))) rlm@77: (if @move-right? rlm@77: (.applyTorque control rlm@77: (.mult (.getPhysicsRotation control) rlm@77: (Vector3f. 0 -10 0)))) rlm@77: (if @roll-left? rlm@77: (.applyTorque control rlm@77: (.mult (.getPhysicsRotation control) rlm@77: (Vector3f. -1 0 0)))) rlm@77: (if @roll-right? rlm@77: (.applyTorque control rlm@77: (.mult (.getPhysicsRotation control) rlm@77: (Vector3f. 1 0 0)))))))) rlm@77: rlm@77: rlm@77: rlm@77: (defn run [joint] (.start (test-joint joint))) rlm@77: (defn look [joint] (view (nodify (world-setup joint)))) rlm@77: rlm@77: (defn blender-to-jme rlm@77: "Convert from Blender coordinates to JME coordinates" rlm@77: [#^Vector3f in] rlm@77: (Vector3f. (.getX in) rlm@77: (.getZ in) rlm@77: (- (.getY in)))) rlm@77: rlm@77: rlm@77: (defn joint-targets rlm@77: "Return the two closest two objects to the joint object, ordered rlm@77: from bottom to top according to the joint's rotation." rlm@77: [#^Node parts #^Node joint] rlm@77: ;;(println (meta-data joint "joint")) rlm@77: (.getWorldRotation joint) rlm@74: (loop [radius (float 0.01)] rlm@74: (let [results (CollisionResults.)] rlm@74: (.collideWith rlm@74: parts rlm@77: (BoundingBox. (.getWorldTranslation joint) rlm@77: radius radius radius) rlm@74: results) rlm@74: (let [targets rlm@74: (distinct rlm@74: (map #(.getGeometry %) results))] rlm@74: (if (>= (count targets) 2) rlm@77: (sort-by rlm@77: #(.getY rlm@77: (.mult rlm@77: (.inverse (.getWorldRotation joint)) rlm@77: (.subtract (.getWorldTranslation %) rlm@77: (.getWorldTranslation joint)))) rlm@77: (take 2 targets)) rlm@74: (recur (float (* radius 2)))))))) rlm@74: rlm@77: (defn connect rlm@77: "here are some examples: rlm@77: {:type :point} rlm@77: {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} rlm@77: (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) rlm@74: rlm@77: {:type :cone :limit-xz 0] rlm@77: :limit-yz 0] rlm@77: :twist 0]} rlm@77: " rlm@77: ([#^Node obj-a #^Node obj-b #^Node joint] rlm@77: (let [center-a (.getWorldTranslation obj-a) rlm@77: center-b (.getWorldTranslation obj-b) rlm@77: joint-center (.getWorldTranslation joint) rlm@77: pivot-a (.subtract joint-center center-a) rlm@77: pivot-b (.subtract joint-center center-b) rlm@77: control-a (.getControl obj-a RigidBodyControl) rlm@77: control-b (.getControl obj-b RigidBodyControl)] rlm@77: ;; A side-effect of creating a joint registers rlm@77: ;; it with both physics objects which in turn rlm@77: ;; will register the joint with the physics system rlm@77: ;; when the simulation is started. rlm@77: (if-let [constraints rlm@77: (map-vals rlm@77: eval rlm@77: (read-string rlm@77: (meta-data (first (worm-joints)) "joint")))] rlm@74: rlm@77: (let [joint-type (:type constraints)] rlm@77: (cond (= :point joint-type) rlm@77: (do rlm@77: (println-repl "creating POINT joint") rlm@77: (Point2PointJoint. rlm@77: control-a rlm@77: control-b rlm@77: pivot-a rlm@77: pivot-b)) rlm@77: (= :hinge joint-type) rlm@77: (do rlm@77: (println-repl "creating HINGE joint") rlm@77: (let [axis (if-let rlm@77: [axis (:axis constraints)] rlm@77: axis rlm@77: Vector3f/UNIT_X) rlm@77: [limit-1 limit-2] (:limit constraints) rlm@77: hinge-axis rlm@77: (.mult rlm@77: (.getWorldRotation joint) rlm@77: (blender-to-jme axis))] rlm@77: (doto rlm@77: (HingeJoint. rlm@77: control-a rlm@77: control-b rlm@77: pivot-a rlm@77: pivot-b rlm@77: hinge-axis rlm@77: hinge-axis) rlm@77: (.setLimit limit-1 limit-2)))) rlm@77: (= :cone joint-type) rlm@77: (do rlm@77: (let [limit-xy (:limit-xz constraints) rlm@77: limit-yz (:limit-yz constraints) rlm@77: twist (:twist constraints)] rlm@77: rlm@77: (println-repl "creating CONE joint") rlm@77: (doto rlm@77: (ConeJoint. rlm@77: control-a rlm@77: control-b rlm@77: pivot-a rlm@77: pivot-b rlm@77: (doto (Matrix3f.) rlm@77: (.fromStartEndVectors rlm@77: Vector3f/UNIT_X rlm@77: (.normalize rlm@77: (.subtract (.getWorldTranslation joint) rlm@77: (.getWorldTranslation obj-a))))) rlm@77: (doto (Matrix3f.) rlm@77: (.fromStartEndVectors rlm@77: Vector3f/UNIT_X rlm@77: (.normalize rlm@77: (.subtract rlm@77: (.getWorldTranslation obj-b) rlm@77: (.getWorldTranslation joint)))))) rlm@77: (.setLimit (float limit-xy) rlm@77: (float limit-yz) rlm@77: (float twist))))) rlm@77: true rlm@77: (println-repl rlm@77: "joint-type " joint-type " not recognized"))) rlm@77: rlm@77: (println-repl "could not find joint meta-data!"))))) rlm@77: rlm@74: rlm@74: rlm@77: (defn physical-worm [#^Node pieces joints] rlm@74: (dorun rlm@74: (map rlm@74: (fn [geom] rlm@74: (let [physics-control rlm@74: (RigidBodyControl. rlm@74: (HullCollisionShape. rlm@74: (.getMesh geom)) rlm@77: (if-let [mass (meta-data geom "mass")] rlm@77: (do rlm@77: (println-repl rlm@77: "setting mass to " (float mass)) rlm@77: (float mass)) rlm@77: (float 1)))] rlm@77: rlm@74: (.addControl geom physics-control))) rlm@74: (filter #(isa? (class %) Geometry ) rlm@74: (node-seq pieces)))) rlm@77: rlm@74: (dorun rlm@74: (map rlm@77: (fn [joint] rlm@77: (let [[obj-a obj-b] rlm@77: (joint-targets pieces joint)] rlm@77: (connect obj-a obj-b joint))) rlm@77: joints)) rlm@77: pieces) rlm@74: rlm@77: (defn the-worm [] rlm@77: (physical-worm (worm) (worm-joints))) rlm@74: rlm@77: (defn test-worm [] rlm@74: (world rlm@77: (nodify [(the-worm) rlm@77: (box 10 2 10 :position (Vector3f. 0 -5 0) rlm@77: :color ColorRGBA/Gray :mass 0)]) rlm@74: standard-debug-controls rlm@77: (comp light-up-everything enable-debug rlm@77: (fn [world] rlm@77: (.setTimer world (NanoTimer.)) rlm@77: (speed-up world) rlm@77: ;;(set-gravity world (Vector3f. 0 0 0)) rlm@77: world rlm@77: )) rlm@77: no-op)) rlm@73: rlm@73: #+end_src rlm@73: rlm@75: #+results: body-1 rlm@77: : #'cortex.silly/test-try-again rlm@75: rlm@73: rlm@73: rlm@73: * COMMENT generate source rlm@73: #+begin_src clojure :tangle ../src/cortex/silly.clj rlm@73: <> rlm@73: #+end_src rlm@73: