Mercurial > cortex
diff org/integration.org @ 192:deac7b708750
cleaned up test-creature.org and renamed to integreation.org.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 11:29:45 -0700 |
parents | org/test-creature.org@66fbab414d45 |
children | 305439cec54d |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/org/integration.org Sat Feb 04 11:29:45 2012 -0700 1.3 @@ -0,0 +1,347 @@ 1.4 +#+title: First attempt at a creature! 1.5 +#+author: Robert McIntyre 1.6 +#+email: rlm@mit.edu 1.7 +#+description: 1.8 +#+keywords: simulation, jMonkeyEngine3, clojure 1.9 +#+SETUPFILE: ../../aurellem/org/setup.org 1.10 +#+INCLUDE: ../../aurellem/org/level-0.org 1.11 + 1.12 + 1.13 + 1.14 + 1.15 +* Intro 1.16 +So far, I've made the following senses -- 1.17 + - Vision 1.18 + - Hearing 1.19 + - Touch 1.20 + - Proprioception 1.21 + 1.22 +And one effector: 1.23 + - Movement 1.24 + 1.25 +However, the code so far has only enabled these senses, but has not 1.26 +actually implemented them. For example, there is still a lot of work 1.27 +to be done for vision. I need to be able to create an /eyeball/ in 1.28 +simulation that can be moved around and see the world from different 1.29 +angles. I also need to determine weather to use log-polar or cartesian 1.30 +for the visual input, and I need to determine how/wether to 1.31 +disceritise the visual input. 1.32 + 1.33 +I also want to be able to visualize both the sensors and the 1.34 +effectors in pretty pictures. This semi-retarted creature will be my 1.35 +first attempt at bringing everything together. 1.36 + 1.37 +* The creature's body 1.38 + 1.39 +Still going to do an eve-like body in blender, but due to problems 1.40 +importing the joints, etc into jMonkeyEngine3, I'm going to do all 1.41 +the connecting here in clojure code, using the names of the individual 1.42 +components and trial and error. Later, I'll maybe make some sort of 1.43 +creature-building modifications to blender that support whatever 1.44 +discritized senses I'm going to make. 1.45 + 1.46 +#+name: integration 1.47 +#+begin_src clojure 1.48 +(ns cortex.integration 1.49 + "let's play!" 1.50 + {:author "Robert McIntyre"} 1.51 + (:use (cortex world util body 1.52 + hearing touch vision sense proprioception movement)) 1.53 + (:import (com.jme3.math ColorRGBA Vector3f)) 1.54 + (:import com.jme3.audio.AudioNode) 1.55 + (:import com.aurellem.capture.RatchetTimer)) 1.56 + 1.57 +(def hand "Models/creature1/one.blend") 1.58 + 1.59 +(def worm "Models/creature1/try-again.blend") 1.60 + 1.61 +(defn test-creature [thing] 1.62 + (let [x-axis 1.63 + (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red) 1.64 + y-axis 1.65 + (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green) 1.66 + z-axis 1.67 + (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue) 1.68 + 1.69 + me (sphere 0.5 :color ColorRGBA/Blue :physical? false) 1.70 + bell (AudioNode. (asset-manager) 1.71 + "Sounds/pure.wav" false) 1.72 + 1.73 + fix-display 1.74 + (runonce (fn [world] 1.75 + (add-camera! world (.getCamera world) no-op))) 1.76 + creature (doto (load-blender-model thing) (body!)) 1.77 + 1.78 + ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.79 + touch (touch! creature) 1.80 + touch-display (view-touch) 1.81 + 1.82 + vision (vision! creature) 1.83 + vision-display (view-vision) 1.84 + 1.85 + hearing (hearing! creature) 1.86 + hearing-display (view-hearing) 1.87 + 1.88 + prop (proprioception! creature) 1.89 + prop-display (view-proprioception) 1.90 + 1.91 + muscle-exertion (atom 0) 1.92 + muscles (movement! creature) 1.93 + muscle-display (view-movement)] 1.94 + 1.95 + (apply 1.96 + world 1.97 + (with-movement 1.98 + (.getChild creature "worm-21") 1.99 + ["key-r" "key-t" 1.100 + "key-f" "key-g" 1.101 + "key-v" "key-b"] 1.102 + [10 10 10 10 1 1] 1.103 + [(nodify [creature 1.104 + (box 10 2 10 :position (Vector3f. 0 -9 0) 1.105 + :color ColorRGBA/Gray :mass 0) 1.106 + x-axis y-axis z-axis 1.107 + me]) 1.108 + (merge standard-debug-controls 1.109 + {"key-return" 1.110 + (fn [_ value] 1.111 + (if value 1.112 + (do 1.113 + (println-repl "play-sound") 1.114 + (.play bell)))) 1.115 + "key-h" 1.116 + (fn [_ value] 1.117 + (if value 1.118 + (swap! muscle-exertion (partial + 20)))) 1.119 + "key-n" 1.120 + (fn [_ value] 1.121 + (if value 1.122 + (swap! muscle-exertion (fn [v] (- v 20)))))}) 1.123 + (fn [world] 1.124 + (light-up-everything world) 1.125 + (enable-debug world) 1.126 + (add-camera! world 1.127 + (add-eye! creature 1.128 + (.getChild 1.129 + (.getChild creature "eyes") "eye")) 1.130 + (comp (view-image) BufferedImage!)) 1.131 + (.setTimer world (RatchetTimer. 60)) 1.132 + (speed-up world) 1.133 + (set-gravity world (Vector3f. 0 0 0)) 1.134 + (comment 1.135 + (com.aurellem.capture.Capture/captureVideo 1.136 + world (file-str "/home/r/proj/ai-videos/hand")))) 1.137 + (fn [world tpf] 1.138 + (prop-display (prop)) 1.139 + (touch-display (map #(% (.getRootNode world)) touch)) 1.140 + (vision-display (map #(% world) vision)) 1.141 + (hearing-display (map #(% world) hearing)) 1.142 + (muscle-display (map #(% @muscle-exertion) muscles)) 1.143 + (.setLocalTranslation me (.getLocation (.getCamera world))) 1.144 + (fix-display world))])))) 1.145 +#+end_src 1.146 + 1.147 +#+results: body-1 1.148 +: #'cortex.silly/follow-test 1.149 + 1.150 + 1.151 +* COMMENT purgatory 1.152 +#+begin_src clojure 1.153 + 1.154 +(defn bullet-trans* [] 1.155 + (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red 1.156 + :position (Vector3f. 5 0 0) 1.157 + :mass 90) 1.158 + obj-b (sphere 0.5 :color ColorRGBA/Blue 1.159 + :position (Vector3f. -5 0 0) 1.160 + :mass 0) 1.161 + control-a (.getControl obj-a RigidBodyControl) 1.162 + control-b (.getControl obj-b RigidBodyControl) 1.163 + move-up? (atom nil) 1.164 + move-down? (atom nil) 1.165 + move-left? (atom nil) 1.166 + move-right? (atom nil) 1.167 + roll-left? (atom nil) 1.168 + roll-right? (atom nil) 1.169 + force 100 1.170 + swivel 1.171 + (.toRotationMatrix 1.172 + (doto (Quaternion.) 1.173 + (.fromAngleAxis (/ Math/PI 2) 1.174 + Vector3f/UNIT_X))) 1.175 + x-move 1.176 + (doto (Matrix3f.) 1.177 + (.fromStartEndVectors Vector3f/UNIT_X 1.178 + (.normalize (Vector3f. 1 1 0)))) 1.179 + 1.180 + timer (atom 0)] 1.181 + (doto 1.182 + (ConeJoint. 1.183 + control-a control-b 1.184 + (Vector3f. -8 0 0) 1.185 + (Vector3f. 2 0 0) 1.186 + ;;swivel swivel 1.187 + ;;Matrix3f/IDENTITY Matrix3f/IDENTITY 1.188 + x-move Matrix3f/IDENTITY 1.189 + ) 1.190 + (.setCollisionBetweenLinkedBodys false) 1.191 + (.setLimit (* 1 (/ Math/PI 4)) ;; twist 1.192 + (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane 1.193 + (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane 1.194 + (world (nodify 1.195 + [obj-a obj-b]) 1.196 + (merge standard-debug-controls 1.197 + {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) 1.198 + "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) 1.199 + "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) 1.200 + "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) 1.201 + "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) 1.202 + "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) 1.203 + 1.204 + (fn [world] 1.205 + (enable-debug world) 1.206 + (set-gravity world Vector3f/ZERO) 1.207 + ) 1.208 + 1.209 + (fn [world _] 1.210 + 1.211 + (if @move-up? 1.212 + (.applyForce control-a 1.213 + (Vector3f. force 0 0) 1.214 + (Vector3f. 0 0 0))) 1.215 + (if @move-down? 1.216 + (.applyForce control-a 1.217 + (Vector3f. (- force) 0 0) 1.218 + (Vector3f. 0 0 0))) 1.219 + (if @move-left? 1.220 + (.applyForce control-a 1.221 + (Vector3f. 0 force 0) 1.222 + (Vector3f. 0 0 0))) 1.223 + (if @move-right? 1.224 + (.applyForce control-a 1.225 + (Vector3f. 0 (- force) 0) 1.226 + (Vector3f. 0 0 0))) 1.227 + 1.228 + (if @roll-left? 1.229 + (.applyForce control-a 1.230 + (Vector3f. 0 0 force) 1.231 + (Vector3f. 0 0 0))) 1.232 + (if @roll-right? 1.233 + (.applyForce control-a 1.234 + (Vector3f. 0 0 (- force)) 1.235 + (Vector3f. 0 0 0))) 1.236 + 1.237 + (if (zero? (rem (swap! timer inc) 100)) 1.238 + (.attachChild 1.239 + (.getRootNode world) 1.240 + (sphere 0.05 :color ColorRGBA/Yellow 1.241 + :physical? false :position 1.242 + (.getWorldTranslation obj-a))))) 1.243 + ) 1.244 + )) 1.245 + 1.246 +(defn test-joint [joint] 1.247 + (let [[origin top bottom floor] (world-setup joint) 1.248 + control (.getControl top RigidBodyControl) 1.249 + move-up? (atom false) 1.250 + move-down? (atom false) 1.251 + move-left? (atom false) 1.252 + move-right? (atom false) 1.253 + roll-left? (atom false) 1.254 + roll-right? (atom false) 1.255 + timer (atom 0)] 1.256 + 1.257 + (world 1.258 + (nodify [top bottom floor origin]) 1.259 + (merge standard-debug-controls 1.260 + {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) 1.261 + "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) 1.262 + "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) 1.263 + "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) 1.264 + "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) 1.265 + "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) 1.266 + 1.267 + (fn [world] 1.268 + (light-up-everything world) 1.269 + (enable-debug world) 1.270 + (set-gravity world (Vector3f. 0 0 0)) 1.271 + ) 1.272 + 1.273 + (fn [world _] 1.274 + (if (zero? (rem (swap! timer inc) 100)) 1.275 + (do 1.276 + ;; (println-repl @timer) 1.277 + (.attachChild (.getRootNode world) 1.278 + (sphere 0.05 :color ColorRGBA/Yellow 1.279 + :position (.getWorldTranslation top) 1.280 + :physical? false)) 1.281 + (.attachChild (.getRootNode world) 1.282 + (sphere 0.05 :color ColorRGBA/LightGray 1.283 + :position (.getWorldTranslation bottom) 1.284 + :physical? false)))) 1.285 + 1.286 + (if @move-up? 1.287 + (.applyTorque control 1.288 + (.mult (.getPhysicsRotation control) 1.289 + (Vector3f. 0 0 10)))) 1.290 + (if @move-down? 1.291 + (.applyTorque control 1.292 + (.mult (.getPhysicsRotation control) 1.293 + (Vector3f. 0 0 -10)))) 1.294 + (if @move-left? 1.295 + (.applyTorque control 1.296 + (.mult (.getPhysicsRotation control) 1.297 + (Vector3f. 0 10 0)))) 1.298 + (if @move-right? 1.299 + (.applyTorque control 1.300 + (.mult (.getPhysicsRotation control) 1.301 + (Vector3f. 0 -10 0)))) 1.302 + (if @roll-left? 1.303 + (.applyTorque control 1.304 + (.mult (.getPhysicsRotation control) 1.305 + (Vector3f. -1 0 0)))) 1.306 + (if @roll-right? 1.307 + (.applyTorque control 1.308 + (.mult (.getPhysicsRotation control) 1.309 + (Vector3f. 1 0 0)))))))) 1.310 + 1.311 +(defn follow-test 1.312 + "Show a camera that stays in the same relative position to a blue cube." 1.313 + [] 1.314 + (let [camera-pos (Vector3f. 0 30 0) 1.315 + rock (box 1 1 1 :color ColorRGBA/Blue 1.316 + :position (Vector3f. 0 10 0) 1.317 + :mass 30 1.318 + ) 1.319 + rot (.getWorldRotation rock) 1.320 + 1.321 + table (box 3 1 10 :color ColorRGBA/Gray :mass 0 1.322 + :position (Vector3f. 0 -3 0))] 1.323 + 1.324 + (world 1.325 + (nodify [rock table]) 1.326 + standard-debug-controls 1.327 + (fn [world] 1.328 + (let 1.329 + [cam (doto (.clone (.getCamera world)) 1.330 + (.setLocation camera-pos) 1.331 + (.lookAt Vector3f/ZERO 1.332 + Vector3f/UNIT_X))] 1.333 + (bind-sense rock cam) 1.334 + 1.335 + (.setTimer world (RatchetTimer. 60)) 1.336 + (add-camera! world cam (comp (view-image) BufferedImage!)) 1.337 + (add-camera! world (.getCamera world) no-op)) 1.338 + ) 1.339 + (fn [_ _] (println-repl rot))))) 1.340 +#+end_src 1.341 + 1.342 + 1.343 +* COMMENT generate source 1.344 +#+begin_src clojure :tangle ../src/cortex/integration.clj 1.345 +<<integration>> 1.346 +#+end_src 1.347 + 1.348 + 1.349 + 1.350 +