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: (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@78: (defn blender-to-jme rlm@78: "Convert from Blender coordinates to JME coordinates" rlm@78: [#^Vector3f in] rlm@78: (Vector3f. (.getX in) rlm@78: (.getZ in) rlm@78: (- (.getY in)))) rlm@74: rlm@78: (defn joint-targets rlm@78: "Return the two closest two objects to the joint object, ordered rlm@78: from bottom to top according to the joint's rotation." rlm@78: [#^Node parts #^Node joint] rlm@78: ;;(println (meta-data joint "joint")) rlm@78: (.getWorldRotation joint) rlm@78: (loop [radius (float 0.01)] rlm@78: (let [results (CollisionResults.)] rlm@78: (.collideWith rlm@78: parts rlm@78: (BoundingBox. (.getWorldTranslation joint) rlm@78: radius radius radius) rlm@78: results) rlm@78: (let [targets rlm@78: (distinct rlm@78: (map #(.getGeometry %) results))] rlm@78: (if (>= (count targets) 2) rlm@78: (sort-by rlm@78: #(.getY rlm@78: (.mult rlm@78: (.inverse (.getWorldRotation joint)) rlm@78: (.subtract (.getWorldTranslation %) rlm@78: (.getWorldTranslation joint)))) rlm@78: (take 2 targets)) rlm@78: (recur (float (* radius 2)))))))) rlm@74: rlm@78: (defn connect rlm@78: "here are some examples: rlm@78: {:type :point} rlm@78: {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} rlm@78: (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) rlm@74: rlm@78: {:type :cone :limit-xz 0] rlm@78: :limit-yz 0] rlm@78: :twist 0]}" rlm@78: ([#^Node obj-a #^Node obj-b #^Node joint] rlm@78: (let [center-a (.getWorldTranslation obj-a) rlm@78: center-b (.getWorldTranslation obj-b) rlm@78: joint-center (.getWorldTranslation joint) rlm@78: pivot-a (.subtract joint-center center-a) rlm@78: pivot-b (.subtract joint-center center-b) rlm@78: control-a (.getControl obj-a RigidBodyControl) rlm@78: control-b (.getControl obj-b RigidBodyControl)] rlm@78: ;; A side-effect of creating a joint registers rlm@78: ;; it with both physics objects which in turn rlm@78: ;; will register the joint with the physics system rlm@78: ;; when the simulation is started. rlm@78: (if-let [constraints rlm@78: (map-vals rlm@78: eval rlm@78: (read-string rlm@78: (meta-data joint "joint")))] rlm@74: rlm@78: (let [joint-type (:type constraints)] rlm@78: (println-repl "creating joint between" rlm@78: (.getName obj-a) "and" (.getName obj-b)) rlm@78: (cond (= :point joint-type) rlm@78: (do rlm@78: (println-repl "creating POINT joint") rlm@78: (Point2PointJoint. rlm@78: control-a rlm@78: control-b rlm@78: pivot-a rlm@78: pivot-b)) rlm@78: (= :hinge joint-type) rlm@78: (do rlm@78: (println-repl "creating HINGE joint") rlm@78: (let [axis (if-let rlm@78: [axis (:axis constraints)] rlm@78: axis rlm@78: Vector3f/UNIT_X) rlm@78: [limit-1 limit-2] (:limit constraints) rlm@78: hinge-axis rlm@78: (.mult rlm@78: (.getWorldRotation joint) rlm@78: (blender-to-jme axis))] rlm@78: (doto rlm@78: (HingeJoint. rlm@78: control-a rlm@78: control-b rlm@78: pivot-a rlm@78: pivot-b rlm@78: hinge-axis rlm@78: hinge-axis) rlm@78: (.setLimit limit-1 limit-2)))) rlm@78: (= :cone joint-type) rlm@78: (do rlm@78: (let [limit-xy (:limit-xz constraints) rlm@78: limit-yz (:limit-yz constraints) rlm@78: twist (:twist constraints)] rlm@78: rlm@78: (println-repl "creating CONE joint") rlm@78: (doto rlm@78: (ConeJoint. rlm@78: control-a rlm@78: control-b rlm@78: pivot-a rlm@78: pivot-b rlm@78: (doto (Matrix3f.) rlm@78: (.fromStartEndVectors rlm@78: Vector3f/UNIT_X rlm@78: (.normalize rlm@78: (.subtract (.getWorldTranslation joint) rlm@78: (.getWorldTranslation obj-a))))) rlm@78: (doto (Matrix3f.) rlm@78: (.fromStartEndVectors rlm@78: Vector3f/UNIT_X rlm@78: (.normalize rlm@78: (.subtract rlm@78: (.getWorldTranslation obj-b) rlm@78: (.getWorldTranslation joint)))))) rlm@78: (.setLimit (float limit-xy) rlm@78: (float limit-yz) rlm@78: (float twist))))) rlm@78: true rlm@78: (println-repl rlm@78: "joint-type" joint-type "not recognized"))) rlm@78: rlm@78: (println-repl "could not find joint meta-data!"))))) rlm@74: rlm@78: (defn assemble-creature [#^Node pieces joints] rlm@78: (dorun rlm@78: (map rlm@78: (fn [geom] rlm@78: (let [physics-control rlm@78: (RigidBodyControl. rlm@78: (HullCollisionShape. rlm@78: (.getMesh geom)) rlm@78: (if-let [mass (meta-data geom "mass")] rlm@78: (do rlm@78: (println-repl rlm@78: "setting" (.getName geom) "mass to" (float mass)) rlm@78: (float mass)) rlm@78: (float 1)))] rlm@78: rlm@78: (.addControl geom physics-control))) rlm@78: (filter #(isa? (class %) Geometry ) rlm@78: (node-seq pieces)))) rlm@77: rlm@78: (dorun rlm@78: (map rlm@78: (fn [joint] rlm@78: (let [[obj-a obj-b] rlm@78: (joint-targets pieces joint)] rlm@78: (connect obj-a obj-b joint))) rlm@78: joints)) rlm@78: pieces) rlm@74: rlm@78: (defn blender-creature [blender-path] rlm@78: (let [model (load-blender-model blender-path) rlm@78: joints rlm@78: (if-let [joint-node (.getChild model "joints")] rlm@78: (seq (.getChildren joint-node)) rlm@78: (do (println-repl "could not find joints node") rlm@78: []))] rlm@78: (assemble-creature model joints))) rlm@74: rlm@78: (def hand "Models/creature1/one.blend") rlm@74: rlm@78: (def worm "Models/creature1/try-again.blend") rlm@78: rlm@78: (defn test-creature [thing] rlm@78: (world rlm@78: (nodify [(blender-creature thing) rlm@78: (box 10 2 10 :position (Vector3f. 0 -5.5 0) rlm@78: :color ColorRGBA/Gray :mass 0)]) rlm@78: standard-debug-controls rlm@78: (comp light-up-everything enable-debug rlm@78: (fn [world] rlm@78: (.setTimer world (NanoTimer.)) rlm@78: ;;(set-gravity world (Vector3f. 0 0 0)) rlm@78: (speed-up world) rlm@78: world rlm@78: )) rlm@78: no-op)) rlm@78: rlm@78: (defn world-setup [joint] rlm@78: (let [top (doto rlm@78: (sphere 0.1 :physical? false :color ColorRGBA/Yellow rlm@78: :position (Vector3f. 0 7 0)) rlm@78: (.addControl rlm@78: (RigidBodyControl. rlm@78: (CapsuleCollisionShape. 0.5 1.5 1) (float 15)))) rlm@78: bottom (doto rlm@78: (sphere 0.1 :physical? false :color ColorRGBA/DarkGray rlm@78: :position (Vector3f. 0 -1 0)) rlm@78: (.addControl rlm@78: (RigidBodyControl. rlm@78: (CapsuleCollisionShape. 0.5 1.5 1) (float 0)))) rlm@78: table (box 10 2 10 :position (Vector3f. 0 -6 0) rlm@78: :color ColorRGBA/Gray :mass 0) rlm@78: a (.getControl top RigidBodyControl) rlm@78: b (.getControl bottom RigidBodyControl)] rlm@78: (cond rlm@78: (= joint :point) rlm@78: (doto rlm@78: (Point2PointJoint. a b rlm@78: (Vector3f. 0 -2 0) rlm@78: (Vector3f. 0 2 0)) rlm@78: (.setCollisionBetweenLinkedBodys false)) rlm@78: (= joint :hinge) rlm@78: (doto rlm@78: (HingeJoint. rlm@78: a b rlm@78: (Vector3f. 0 -2 0) rlm@78: (Vector3f. 0 2 0) rlm@78: (Vector3f. 0 0 1) rlm@78: (Vector3f. 0 0 1) rlm@78: rlm@78: ) rlm@78: (.setCollisionBetweenLinkedBodys false) rlm@78: ;;(.setLimit (- Math/PI) Math/PI) rlm@78: ) rlm@78: (= joint :cone) rlm@78: ;; note to self -- jbullet does NOT implement cone joints rlm@78: ;; correctly. You must use plain ol' bullet for this to work. rlm@78: ;; It's faster anyway, so whatever. rlm@78: rlm@78: (doto (ConeJoint. rlm@78: a b rlm@78: (Vector3f. 0 -5 0) rlm@78: (Vector3f. 0 2 0) rlm@78: rlm@78: (doto (Matrix3f.) rlm@78: (.fromStartEndVectors Vector3f/UNIT_X rlm@78: Vector3f/UNIT_Y)) rlm@78: (doto (Matrix3f.) rlm@78: (.fromStartEndVectors Vector3f/UNIT_X rlm@78: (.normalize rlm@78: (Vector3f. 5 5 0)))) rlm@78: ) rlm@78: ;;(.setAngularOnly true) rlm@78: rlm@78: (.setCollisionBetweenLinkedBodys false) rlm@78: (.setLimit (* 1 (/ Math/PI 4)) rlm@78: (* 1 (/ Math/PI 2)) rlm@78: (* 0 (/ Math/PI 4))) rlm@78: rlm@78: ) rlm@78: (= joint :six) rlm@78: (doto rlm@78: (SixDofJoint. rlm@78: a b rlm@78: (Vector3f. 0 -2 0) rlm@78: (Vector3f. 0 2 0) rlm@78: ;;(doto (Matrix3f.) rlm@78: ;; (.fromStartEndVectors Vector3f/UNIT_X rlm@78: ;; Vector3f/UNIT_Y)) rlm@78: ;;(doto (Matrix3f.) rlm@78: ;; (.fromStartEndVectors Vector3f/UNIT_X rlm@78: ;; Vector3f/UNIT_Y)) rlm@78: true) rlm@78: (.setCollisionBetweenLinkedBodys false) rlm@78: (.setAngularLowerLimit (Vector3f. 0 rlm@78: (- (/ Math/PI 2)) rlm@78: 0)) rlm@78: rlm@78: (.setAngularUpperLimit (Vector3f. 0 rlm@78: (/ Math/PI 2) rlm@78: 0)) rlm@78: (.setLinearLowerLimit (Vector3f. 0 0 0)) rlm@78: (.setLinearUpperLimit (Vector3f. 0 0 0)) rlm@78: rlm@78: ) rlm@78: rlm@78: rlm@78: rlm@78: rlm@78: rlm@78: ) rlm@78: rlm@78: [top bottom table])) rlm@78: rlm@78: rlm@78: (defn test-joint [joint] rlm@78: (let [[top bottom floor] (world-setup joint) rlm@78: control (.getControl top RigidBodyControl) rlm@78: move-up? (atom false) rlm@78: move-down? (atom false) rlm@78: move-left? (atom false) rlm@78: move-right? (atom false) rlm@78: roll-left? (atom false) rlm@78: roll-right? (atom false) rlm@78: timer (atom 0)] rlm@78: rlm@78: (world rlm@78: (nodify [top bottom floor]) rlm@78: (merge standard-debug-controls rlm@78: {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) rlm@78: "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) rlm@78: "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) rlm@78: "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) rlm@78: "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) rlm@78: "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) rlm@78: rlm@78: (fn [world] rlm@78: (light-up-everything world) rlm@78: (enable-debug world) rlm@78: (set-gravity world (Vector3f. 0 0 0)) rlm@78: ) rlm@78: rlm@78: (fn [world _] rlm@78: (if (zero? (rem (swap! timer inc) 100)) rlm@78: (do rlm@78: ;; (println-repl @timer) rlm@78: (.attachChild (.getRootNode world) rlm@78: (sphere 0.05 :color ColorRGBA/Yellow rlm@78: :position (.getWorldTranslation top) rlm@78: :physical? false)))) rlm@78: (if @move-up? rlm@78: (.applyTorque control rlm@78: (.mult (.getPhysicsRotation control) rlm@78: (Vector3f. 0 0 10)))) rlm@78: (if @move-down? rlm@78: (.applyTorque control rlm@78: (.mult (.getPhysicsRotation control) rlm@78: (Vector3f. 0 0 -10)))) rlm@78: (if @move-left? rlm@78: (.applyTorque control rlm@78: (.mult (.getPhysicsRotation control) rlm@78: (Vector3f. 0 10 0)))) rlm@78: (if @move-right? rlm@78: (.applyTorque control rlm@78: (.mult (.getPhysicsRotation control) rlm@78: (Vector3f. 0 -10 0)))) rlm@78: (if @roll-left? rlm@78: (.applyTorque control rlm@78: (.mult (.getPhysicsRotation control) rlm@78: (Vector3f. -1 0 0)))) rlm@78: (if @roll-right? rlm@78: (.applyTorque control rlm@78: (.mult (.getPhysicsRotation control) rlm@78: (Vector3f. 1 0 0)))))))) rlm@78: #+end_src rlm@78: rlm@78: rlm@78: * COMMENT purgatory rlm@78: #+begin_src clojure 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@73: #+end_src 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: