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@192: #+name: integration rlm@73: #+begin_src clojure rlm@192: (ns cortex.integration rlm@73: "let's play!" rlm@192: {:author "Robert McIntyre"} rlm@192: (:use (cortex world util body rlm@192: hearing touch vision sense proprioception movement)) rlm@192: (:import (com.jme3.math ColorRGBA Vector3f)) rlm@192: (:import com.jme3.audio.AudioNode) rlm@192: (:import com.aurellem.capture.RatchetTimer)) rlm@73: rlm@78: (def hand "Models/creature1/one.blend") rlm@74: rlm@78: (def worm "Models/creature1/try-again.blend") rlm@78: 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@192: me (sphere 0.5 :color ColorRGBA/Blue :physical? false) rlm@192: bell (AudioNode. (asset-manager) rlm@192: "Sounds/pure.wav" false) rlm@192: rlm@192: fix-display rlm@192: (runonce (fn [world] rlm@192: (add-camera! world (.getCamera world) no-op))) rlm@189: creature (doto (load-blender-model thing) (body!)) rlm@189: rlm@192: ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;; 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@191: muscle-exertion (atom 0) rlm@191: muscles (movement! creature) rlm@192: muscle-display (view-movement)] rlm@189: 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@192: me]) 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@191: (swap! muscle-exertion (partial + 20)))) rlm@191: "key-n" rlm@191: (fn [_ value] rlm@191: (if value rlm@192: (swap! muscle-exertion (fn [v] (- v 20)))))}) rlm@143: (fn [world] rlm@143: (light-up-everything world) rlm@143: (enable-debug world) rlm@169: (add-camera! world rlm@192: (add-eye! creature rlm@192: (.getChild rlm@192: (.getChild creature "eyes") "eye")) rlm@143: (comp (view-image) BufferedImage!)) rlm@192: (.setTimer world (RatchetTimer. 60)) rlm@143: (speed-up world) rlm@148: (set-gravity world (Vector3f. 0 0 0)) rlm@192: (comment rlm@192: (com.aurellem.capture.Capture/captureVideo rlm@192: world (file-str "/home/r/proj/ai-videos/hand")))) rlm@143: (fn [world tpf] rlm@190: (prop-display (prop)) rlm@188: (touch-display (map #(% (.getRootNode world)) touch)) rlm@188: (vision-display (map #(% world) vision)) rlm@189: (hearing-display (map #(% world) hearing)) rlm@191: (muscle-display (map #(% @muscle-exertion) muscles)) rlm@143: (.setLocalTranslation me (.getLocation (.getCamera world))) rlm@192: (fix-display world))])))) 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@192: rlm@192: (defn follow-test rlm@192: "Show a camera that stays in the same relative position to a blue cube." rlm@192: [] rlm@192: (let [camera-pos (Vector3f. 0 30 0) rlm@192: rock (box 1 1 1 :color ColorRGBA/Blue rlm@192: :position (Vector3f. 0 10 0) rlm@192: :mass 30 rlm@192: ) rlm@192: rot (.getWorldRotation rock) rlm@192: rlm@192: table (box 3 1 10 :color ColorRGBA/Gray :mass 0 rlm@192: :position (Vector3f. 0 -3 0))] rlm@192: rlm@192: (world rlm@192: (nodify [rock table]) rlm@192: standard-debug-controls rlm@192: (fn [world] rlm@192: (let rlm@192: [cam (doto (.clone (.getCamera world)) rlm@192: (.setLocation camera-pos) rlm@192: (.lookAt Vector3f/ZERO rlm@192: Vector3f/UNIT_X))] rlm@192: (bind-sense rock cam) rlm@192: rlm@192: (.setTimer world (RatchetTimer. 60)) rlm@192: (add-camera! world cam (comp (view-image) BufferedImage!)) rlm@192: (add-camera! world (.getCamera world) no-op)) rlm@192: ) rlm@192: (fn [_ _] (println-repl rot))))) rlm@99: #+end_src rlm@192: rlm@99: rlm@99: * COMMENT generate source rlm@192: #+begin_src clojure :tangle ../src/cortex/integration.clj rlm@192: <> rlm@99: #+end_src rlm@99: rlm@99: rlm@94: rlm@94: