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@129: rlm@129: rlm@99: 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@104: effectors in pretty pictures. This semi-retarted creature will be 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@104: 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@158: discritized 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@158: (use '(cortex world util body hearing touch vision sense rlm@162: proprioception movement)) rlm@73: rlm@73: (rlm.rlm-commands/help) rlm@99: (import java.awt.image.BufferedImage) rlm@99: (import javax.swing.JPanel) rlm@99: (import javax.swing.SwingUtilities) rlm@99: (import java.awt.Dimension) rlm@99: (import javax.swing.JFrame) rlm@99: (import java.awt.Dimension) rlm@106: (import com.aurellem.capture.RatchetTimer) rlm@161: rlm@108: (use 'clojure.contrib.def) 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@78: (def hand "Models/creature1/one.blend") rlm@74: rlm@78: (def worm "Models/creature1/try-again.blend") rlm@78: rlm@90: (defn worm-model [] (load-blender-model worm)) rlm@90: rlm@80: (defn x-ray [#^ColorRGBA color] rlm@80: (doto (Material. (asset-manager) rlm@80: "Common/MatDefs/Misc/Unshaded.j3md") rlm@80: (.setColor "Color" color) rlm@80: (-> (.getAdditionalRenderState) rlm@80: (.setDepthTest false)))) rlm@80: rlm@91: (defn colorful [] rlm@91: (.getChild (worm-model) "worm-21")) rlm@90: rlm@90: (import jme3tools.converters.ImageToAwt) rlm@90: rlm@90: (import ij.ImagePlus) rlm@90: rlm@94: rlm@109: rlm@111: (defn test-eye [] rlm@117: (.getChild rlm@117: (.getChild (worm-model) "eyes") rlm@117: "eye")) rlm@111: rlm@111: rlm@123: rlm@128: ;; lower level --- nodes rlm@128: ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair rlm@128: rlm@128: ;; higher level -- organs rlm@128: ;; rlm@128: rlm@128: ;; higher level --- sense/effector rlm@128: ;; these are the functions that provide world i/o, chinese-room style rlm@128: rlm@123: (defn debug-hearing-window rlm@123: "view audio data" rlm@123: [height] rlm@123: (let [vi (view-image)] rlm@123: (fn [[coords sensor-data]] rlm@123: (let [image (BufferedImage. (count coords) height rlm@123: BufferedImage/TYPE_INT_RGB)] rlm@123: (dorun rlm@123: (for [x (range (count coords))] rlm@123: (dorun rlm@123: (for [y (range height)] rlm@123: (let [raw-sensor (sensor-data x)] rlm@188: (.setRGB image x y (gray raw-sensor))))))) rlm@126: rlm@123: (vi image))))) rlm@123: rlm@106: (defn test-creature [thing] rlm@106: (let [x-axis rlm@106: (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red) rlm@106: y-axis rlm@106: (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green) rlm@106: z-axis rlm@106: (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue) rlm@189: rlm@189: creature (doto (load-blender-model thing) (body!)) rlm@189: rlm@185: touch (touch! creature) rlm@188: touch-display (view-touch) rlm@189: rlm@188: vision (vision! creature) rlm@188: vision-display (view-vision) rlm@189: rlm@189: hearing (hearing! creature) rlm@189: hearing-display (view-hearing) rlm@189: rlm@173: prop (proprioception! creature) rlm@190: prop-display (view-proprioception) rlm@148: rlm@180: muscle-fns (movement! creature) rlm@189: rlm@189: rlm@189: me (sphere 0.5 :color ColorRGBA/Blue :physical? false) rlm@189: bell (AudioNode. (asset-manager) rlm@189: "Sounds/pure.wav" false) rlm@189: rlm@170: fix-display (runonce rlm@170: (fn [world] (add-camera! world (.getCamera world) no-op))) rlm@106: ] rlm@143: rlm@143: rlm@143: (apply rlm@143: world rlm@143: (with-movement rlm@143: (.getChild creature "worm-21") rlm@143: ["key-r" "key-t" rlm@143: "key-f" "key-g" rlm@143: "key-v" "key-b"] rlm@143: [10 10 10 10 1 1] rlm@143: [(nodify [creature rlm@143: (box 10 2 10 :position (Vector3f. 0 -9 0) rlm@143: :color ColorRGBA/Gray :mass 0) rlm@143: x-axis y-axis z-axis rlm@143: me rlm@143: ]) rlm@143: (merge standard-debug-controls rlm@143: {"key-return" rlm@143: (fn [_ value] rlm@143: (if value rlm@143: (do rlm@143: (println-repl "play-sound") rlm@148: (.play bell)))) rlm@148: "key-h" rlm@148: (fn [_ value] rlm@148: (if value rlm@148: (do rlm@148: (println-repl "muscle activating!") rlm@148: ((first muscle-fns) 199)))) rlm@148: rlm@148: }) rlm@143: (fn [world] rlm@143: (light-up-everything world) rlm@143: (enable-debug world) rlm@170: ;;(dorun (map #(% world) init-vision-fns)) rlm@164: ;;(dorun (map #(% world) init-hearing-fns)) rlm@143: rlm@169: (add-camera! world rlm@169: (add-eye! creature (test-eye)) rlm@143: (comp (view-image) BufferedImage!)) rlm@143: rlm@170: rlm@145: ;;(set-gravity world (Vector3f. 0 0 0)) rlm@143: ;;(com.aurellem.capture.Capture/captureVideo rlm@143: ;; world (file-str "/home/r/proj/ai-videos/hand")) rlm@143: ;;(.setTimer world (RatchetTimer. 60)) rlm@143: (speed-up world) rlm@148: (set-gravity world (Vector3f. 0 0 0)) rlm@143: ) rlm@143: (fn [world tpf] rlm@143: ;;(dorun rlm@143: ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world)))) rlm@143: rlm@190: (prop-display (prop)) rlm@143: rlm@188: (touch-display (map #(% (.getRootNode world)) touch)) rlm@188: rlm@188: (vision-display (map #(% world) vision)) rlm@189: rlm@189: (hearing-display (map #(% world) hearing)) rlm@143: rlm@143: ;;(println-repl (vision-data)) rlm@143: (.setLocalTranslation me (.getLocation (.getCamera world))) rlm@170: (fix-display world) rlm@143: rlm@143: )] rlm@106: ;;(let [timer (atom 0)] rlm@106: ;; (fn [_ _] rlm@106: ;; (swap! timer inc) rlm@106: ;; (if (= (rem @timer 60) 0) rlm@106: ;; (println-repl (float (/ @timer 60)))))) rlm@143: )))) rlm@83: rlm@109: rlm@116: rlm@116: ;; the camera will stay in its initial position/rotation with relation rlm@116: ;; to the spatial. rlm@116: rlm@116: rlm@162: ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli, rlm@162: ;;anchor-qualia, augment-organ, with-organ rlm@162: rlm@117: (defn follow-test rlm@117: "show a camera that stays in the same relative position to a blue cube." rlm@117: [] rlm@116: (let [camera-pos (Vector3f. 0 30 0) rlm@116: rock (box 1 1 1 :color ColorRGBA/Blue rlm@116: :position (Vector3f. 0 10 0) rlm@116: :mass 30 rlm@116: ) rlm@118: rot (.getWorldRotation rock) rlm@116: rlm@116: table (box 3 1 10 :color ColorRGBA/Gray :mass 0 rlm@116: :position (Vector3f. 0 -3 0))] rlm@116: rlm@116: (world rlm@116: (nodify [rock table]) rlm@116: standard-debug-controls rlm@116: (fn [world] rlm@116: (let rlm@116: [cam (doto (.clone (.getCamera world)) rlm@116: (.setLocation camera-pos) rlm@116: (.lookAt Vector3f/ZERO rlm@116: Vector3f/UNIT_X))] rlm@123: (bind-sense rock cam) rlm@116: rlm@116: (.setTimer world (RatchetTimer. 60)) rlm@169: (add-camera! world cam (comp (view-image) BufferedImage!)) rlm@169: (add-camera! world (.getCamera world) no-op)) rlm@116: ) rlm@118: (fn [_ _] (println-repl rot))))) rlm@116: rlm@118: rlm@123: rlm@87: #+end_src rlm@83: rlm@87: #+results: body-1 rlm@133: : #'cortex.silly/follow-test rlm@78: rlm@78: rlm@78: * COMMENT purgatory rlm@78: #+begin_src clojure 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@106: (defn test-joint [joint] rlm@106: (let [[origin top bottom floor] (world-setup joint) rlm@106: control (.getControl top RigidBodyControl) rlm@106: move-up? (atom false) rlm@106: move-down? (atom false) rlm@106: move-left? (atom false) rlm@106: move-right? (atom false) rlm@106: roll-left? (atom false) rlm@106: roll-right? (atom false) rlm@106: timer (atom 0)] rlm@106: rlm@106: (world rlm@106: (nodify [top bottom floor origin]) rlm@106: (merge standard-debug-controls rlm@106: {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) rlm@106: "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) rlm@106: "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) rlm@106: "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) rlm@106: "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) rlm@106: "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) rlm@106: rlm@106: (fn [world] rlm@106: (light-up-everything world) rlm@106: (enable-debug world) rlm@106: (set-gravity world (Vector3f. 0 0 0)) rlm@106: ) rlm@106: rlm@106: (fn [world _] rlm@106: (if (zero? (rem (swap! timer inc) 100)) rlm@106: (do rlm@106: ;; (println-repl @timer) rlm@106: (.attachChild (.getRootNode world) rlm@106: (sphere 0.05 :color ColorRGBA/Yellow rlm@106: :position (.getWorldTranslation top) rlm@106: :physical? false)) rlm@106: (.attachChild (.getRootNode world) rlm@106: (sphere 0.05 :color ColorRGBA/LightGray rlm@106: :position (.getWorldTranslation bottom) rlm@106: :physical? false)))) rlm@106: rlm@106: (if @move-up? rlm@106: (.applyTorque control rlm@106: (.mult (.getPhysicsRotation control) rlm@106: (Vector3f. 0 0 10)))) rlm@106: (if @move-down? rlm@106: (.applyTorque control rlm@106: (.mult (.getPhysicsRotation control) rlm@106: (Vector3f. 0 0 -10)))) rlm@106: (if @move-left? rlm@106: (.applyTorque control rlm@106: (.mult (.getPhysicsRotation control) rlm@106: (Vector3f. 0 10 0)))) rlm@106: (if @move-right? rlm@106: (.applyTorque control rlm@106: (.mult (.getPhysicsRotation control) rlm@106: (Vector3f. 0 -10 0)))) rlm@106: (if @roll-left? rlm@106: (.applyTorque control rlm@106: (.mult (.getPhysicsRotation control) rlm@106: (Vector3f. -1 0 0)))) rlm@106: (if @roll-right? rlm@106: (.applyTorque control rlm@106: (.mult (.getPhysicsRotation control) rlm@106: (Vector3f. 1 0 0)))))))) rlm@99: #+end_src rlm@99: rlm@99: rlm@99: * COMMENT generate source rlm@99: #+begin_src clojure :tangle ../src/cortex/silly.clj rlm@99: <> rlm@99: #+end_src rlm@99: rlm@99: rlm@94: rlm@94: