Mercurial > cortex
view org/test-creature.org @ 144:48f9cba082eb
proprioception is position-dependent, investigating...
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 02 Feb 2012 03:17:11 -0700 |
parents | d0f4b473ab4f |
children | 7a49b81ca1bf |
line wrap: on
line source
1 #+title: First attempt at a creature!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description:5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org11 * Brainstorming different sensors and effectors.13 Every sense that we have should have an effector that changes what14 that sense (or others who have that sense) experiences.16 ** Classic Senses17 | Sense | Effector |18 |------------------------------+---------------------------------|19 | Vision | Variable Coloration |20 | Hearing | Speech |21 | Proprioception | Movement |22 | Smell/Taste (Chemoreception) | Pheremones |23 | Touch | Movement / Controllable Texture |24 | Acceleration | Movement |25 | Balance (sense gravity) | Movement |26 | | |28 - New Senses/Effectors29 - Levitation30 - Telekenesis31 - control of gravity within a certain radius32 - speed up/slow time33 - object creation/destruction35 - Symbol Sense36 Where objects in the world can be queried for description /37 symbols.39 - Symbol Marking40 The ability to mark objects in the world with your own descriptions41 and symbols.43 - Vision44 Distinguish the polarization of light45 Color46 Movement48 * project ideas49 - HACKER for writing muscle-control programs : Presented with50 low-level muscle control/ sense API, generate higher level programs51 for accomplishing various stated goals. Example goals might be52 "extend all your fingers" or "move your hand into the area with53 blue light" or "decrease the angle of this joint". It would be54 like Sussman's HACKER, except it would operate with much more data55 in a more realistic world. Start off with "calestanthics" to56 develop subrouitines over the motor control API. This would be the57 "spinal chord" of a more intelligent creature. The low level58 programming code might be a turning machine that could develop59 programs to iterate over a "tape" where each entry in the tape60 could control recruitment of the fibers in a muscle.61 - Make a virtual computer in the virtual world which with which the62 creature interacts using its fingers to press keys on a virtual63 keyboard. The creature can access the internet, watch videos, take64 over the world, anything it wants.65 - Make virtual insturments like pianos, drumbs, etc that it learns to66 play.67 - make a joint that figures out what type of joint it is (range of68 motion)74 * goals76 ** have to get done before winston77 - [ ] write an explination for why greyscale bitmaps for senses is78 appropiate -- 1/2 day79 - [ ] muscle control -- day80 - [ ] proprioception sensor map in the style of the other senses -- day81 - [ ] refactor integration code to distribute to each of the senses82 -- day83 - [ ] create video showing all the senses for Winston -- 2 days84 - [ ] write summary of project for Winston \85 - [ ] project proposals for Winston \86 - [ ] additional senses to be implemented for Winston | -- 2 days87 - [ ] send Winston package /89 ** would be cool to get done before winston90 - [X] enable greyscale bitmaps for touch -- 2 hours91 - [X] use sawfish to auto-tile sense windows -- 6 hours92 - [X] sawfish keybinding to automatically delete all sense windows93 - [ ] directly change the UV-pixels to show sensor activation -- 294 days95 - [ ] proof of concept C sense manipulation -- 2 days96 - [ ] proof of concept GPU sense manipulation -- week97 - [ ] fourier view of sound -- 2 or 3 days98 - [ ] dancing music generator -- 1 day, depends on fourier100 ** don't have to get done before winston101 - [ ] write tests for integration -- 3 days102 - [ ] usertime/gametime clock HUD display -- day103 - [ ] find papers for each of the senses justifying my own104 representation -- week105 - [ ] show sensor maps in HUD display? -- 4 days106 - [ ] show sensor maps in AWT display? -- 2 days109 * Intro110 So far, I've made the following senses --111 - Vision112 - Hearing113 - Touch114 - Proprioception116 And one effector:117 - Movement119 However, the code so far has only enabled these senses, but has not120 actually implemented them. For example, there is still a lot of work121 to be done for vision. I need to be able to create an /eyeball/ in122 simulation that can be moved around and see the world from different123 angles. I also need to determine weather to use log-polar or cartesian124 for the visual input, and I need to determine how/wether to125 disceritise the visual input.127 I also want to be able to visualize both the sensors and the128 effectors in pretty pictures. This semi-retarted creature will be my129 first attempt at bringing everything together.131 * The creature's body133 Still going to do an eve-like body in blender, but due to problems134 importing the joints, etc into jMonkeyEngine3, I'm going to do all135 the connecting here in clojure code, using the names of the individual136 components and trial and error. Later, I'll maybe make some sort of137 creature-building modifications to blender that support whatever138 discreitized senses I'm going to make.140 #+name: body-1141 #+begin_src clojure142 (ns cortex.silly143 "let's play!"144 {:author "Robert McIntyre"})146 ;; TODO remove this!147 (require 'cortex.import)148 (cortex.import/mega-import-jme3)149 (use '(cortex world util body hearing touch vision))151 (rlm.rlm-commands/help)152 (import java.awt.image.BufferedImage)153 (import javax.swing.JPanel)154 (import javax.swing.SwingUtilities)155 (import java.awt.Dimension)156 (import javax.swing.JFrame)157 (import java.awt.Dimension)158 (import com.aurellem.capture.RatchetTimer)159 (declare joint-create)160 (use 'clojure.contrib.def)162 (defn points->image163 "Take a sparse collection of points and visuliaze it as a164 BufferedImage."166 ;; TODO maybe parallelize this since it's easy168 [points]169 (if (empty? points)170 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)171 (let [xs (vec (map first points))172 ys (vec (map second points))173 x0 (apply min xs)174 y0 (apply min ys)175 width (- (apply max xs) x0)176 height (- (apply max ys) y0)177 image (BufferedImage. (inc width) (inc height)178 BufferedImage/TYPE_INT_RGB)]179 (dorun180 (for [x (range (.getWidth image))181 y (range (.getHeight image))]182 (.setRGB image x y 0xFF0000)))183 (dorun184 (for [index (range (count points))]185 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))187 image)))189 (defn average [coll]190 (/ (reduce + coll) (count coll)))192 (defn collapse-1d193 "One dimensional analogue of collapse"194 [center line]195 (let [length (count line)196 num-above (count (filter (partial < center) line))197 num-below (- length num-above)]198 (range (- center num-below)199 (+ center num-above))))201 (defn collapse202 "Take a set of pairs of integers and collapse them into a203 contigous bitmap."204 [points]205 (if (empty? points) []206 (let207 [num-points (count points)208 center (vector209 (int (average (map first points)))210 (int (average (map first points))))211 flattened212 (reduce213 concat214 (map215 (fn [column]216 (map vector217 (map first column)218 (collapse-1d (second center)219 (map second column))))220 (partition-by first (sort-by first points))))221 squeezed222 (reduce223 concat224 (map225 (fn [row]226 (map vector227 (collapse-1d (first center)228 (map first row))229 (map second row)))230 (partition-by second (sort-by second flattened))))231 relocate232 (let [min-x (apply min (map first squeezed))233 min-y (apply min (map second squeezed))]234 (map (fn [[x y]]235 [(- x min-x)236 (- y min-y)])237 squeezed))]238 relocate)))240 (defn load-bullet []241 (let [sim (world (Node.) {} no-op no-op)]242 (doto sim243 (.enqueue244 (fn []245 (.stop sim)))246 (.start))))248 (defn load-blender-model249 "Load a .blend file using an asset folder relative path."250 [^String model]251 (.loadModel252 (doto (asset-manager)253 (.registerLoader BlenderModelLoader (into-array String ["blend"])))254 model))256 (defn meta-data [blender-node key]257 (if-let [data (.getUserData blender-node "properties")]258 (.findValue data key)259 nil))261 (defn blender-to-jme262 "Convert from Blender coordinates to JME coordinates"263 [#^Vector3f in]264 (Vector3f. (.getX in)265 (.getZ in)266 (- (.getY in))))272 (defn world-to-local273 "Convert the world coordinates into coordinates relative to the274 object (i.e. local coordinates), taking into account the rotation275 of object."276 [#^Spatial object world-coordinate]277 (let [out (Vector3f.)]278 (.worldToLocal object world-coordinate out) out))280 (defn local-to-world281 "Convert the local coordinates into coordinates into world relative282 coordinates"283 [#^Spatial object local-coordinate]284 (let [world-coordinate (Vector3f.)]285 (.localToWorld object local-coordinate world-coordinate)286 world-coordinate))288 (defmulti joint-dispatch289 "Translate blender pseudo-joints into real JME joints."290 (fn [constraints & _]291 (:type constraints)))293 (defmethod joint-dispatch :point294 [constraints control-a control-b pivot-a pivot-b rotation]295 (println-repl "creating POINT2POINT joint")296 ;; bullet's point2point joints are BROKEN, so we must use the297 ;; generic 6DOF joint instead of an actual Point2Point joint!299 ;; should be able to do this:300 (comment301 (Point2PointJoint.302 control-a303 control-b304 pivot-a305 pivot-b))307 ;; but instead we must do this:308 (println-repl "substuting 6DOF joint for POINT2POINT joint!")309 (doto310 (SixDofJoint.311 control-a312 control-b313 pivot-a314 pivot-b315 false)316 (.setLinearLowerLimit Vector3f/ZERO)317 (.setLinearUpperLimit Vector3f/ZERO)318 ;;(.setAngularLowerLimit (Vector3f. 1 1 1))319 ;;(.setAngularUpperLimit (Vector3f. 0 0 0))321 ))324 (defmethod joint-dispatch :hinge325 [constraints control-a control-b pivot-a pivot-b rotation]326 (println-repl "creating HINGE joint")327 (let [axis328 (if-let329 [axis (:axis constraints)]330 axis331 Vector3f/UNIT_X)332 [limit-1 limit-2] (:limit constraints)333 hinge-axis334 (.mult335 rotation336 (blender-to-jme axis))]337 (doto338 (HingeJoint.339 control-a340 control-b341 pivot-a342 pivot-b343 hinge-axis344 hinge-axis)345 (.setLimit limit-1 limit-2))))347 (defmethod joint-dispatch :cone348 [constraints control-a control-b pivot-a pivot-b rotation]349 (let [limit-xz (:limit-xz constraints)350 limit-xy (:limit-xy constraints)351 twist (:twist constraints)]353 (println-repl "creating CONE joint")354 (println-repl rotation)355 (println-repl356 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))357 (println-repl358 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))359 (println-repl360 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))361 (doto362 (ConeJoint.363 control-a364 control-b365 pivot-a366 pivot-b367 rotation368 rotation)369 (.setLimit (float limit-xz)370 (float limit-xy)371 (float twist)))))373 (defn connect374 "here are some examples:375 {:type :point}376 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}377 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)379 {:type :cone :limit-xz 0]380 :limit-xy 0]381 :twist 0]} (use XZY rotation mode in blender!)"382 [#^Node obj-a #^Node obj-b #^Node joint]383 (let [control-a (.getControl obj-a RigidBodyControl)384 control-b (.getControl obj-b RigidBodyControl)385 joint-center (.getWorldTranslation joint)386 joint-rotation (.toRotationMatrix (.getWorldRotation joint))387 pivot-a (world-to-local obj-a joint-center)388 pivot-b (world-to-local obj-b joint-center)]390 (if-let [constraints391 (map-vals392 eval393 (read-string394 (meta-data joint "joint")))]395 ;; A side-effect of creating a joint registers396 ;; it with both physics objects which in turn397 ;; will register the joint with the physics system398 ;; when the simulation is started.399 (do400 (println-repl "creating joint between"401 (.getName obj-a) "and" (.getName obj-b))402 (joint-dispatch constraints403 control-a control-b404 pivot-a pivot-b405 joint-rotation))406 (println-repl "could not find joint meta-data!"))))411 (defn assemble-creature [#^Node pieces joints]412 (dorun413 (map414 (fn [geom]415 (let [physics-control416 (RigidBodyControl.417 (HullCollisionShape.418 (.getMesh geom))419 (if-let [mass (meta-data geom "mass")]420 (do421 (println-repl422 "setting" (.getName geom) "mass to" (float mass))423 (float mass))424 (float 1)))]426 (.addControl geom physics-control)))427 (filter #(isa? (class %) Geometry )428 (node-seq pieces))))429 (dorun430 (map431 (fn [joint]432 (let [[obj-a obj-b] (joint-targets pieces joint)]433 (connect obj-a obj-b joint)))434 joints))435 pieces)437 (declare blender-creature)439 (def hand "Models/creature1/one.blend")441 (def worm "Models/creature1/try-again.blend")443 (def touch "Models/creature1/touch.blend")445 (defn worm-model [] (load-blender-model worm))447 (defn x-ray [#^ColorRGBA color]448 (doto (Material. (asset-manager)449 "Common/MatDefs/Misc/Unshaded.j3md")450 (.setColor "Color" color)451 (-> (.getAdditionalRenderState)452 (.setDepthTest false))))454 (defn colorful []455 (.getChild (worm-model) "worm-21"))457 (import jme3tools.converters.ImageToAwt)459 (import ij.ImagePlus)461 ;; Every Mesh has many triangles, each with its own index.462 ;; Every vertex has its own index as well.464 (defn tactile-sensor-image465 "Return the touch-sensor distribution image in BufferedImage format,466 or nil if it does not exist."467 [#^Geometry obj]468 (if-let [image-path (meta-data obj "touch")]469 (ImageToAwt/convert470 (.getImage471 (.loadTexture472 (asset-manager)473 image-path))474 false false 0)))476 (import ij.process.ImageProcessor)477 (import java.awt.image.BufferedImage)479 (def white -1)481 (defn filter-pixels482 "List the coordinates of all pixels matching pred, within the bounds483 provided. Bounds -> [x0 y0 width height]"484 {:author "Dylan Holmes"}485 ([pred #^BufferedImage image]486 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))487 ([pred #^BufferedImage image [x0 y0 width height]]488 ((fn accumulate [x y matches]489 (cond490 (>= y (+ height y0)) matches491 (>= x (+ width x0)) (recur 0 (inc y) matches)492 (pred (.getRGB image x y))493 (recur (inc x) y (conj matches [x y]))494 :else (recur (inc x) y matches)))495 x0 y0 [])))497 (defn white-coordinates498 "Coordinates of all the white pixels in a subset of the image."499 ([#^BufferedImage image bounds]500 (filter-pixels #(= % white) image bounds))501 ([#^BufferedImage image]502 (filter-pixels #(= % white) image)))504 (defn triangle505 "Get the triangle specified by triangle-index from the mesh within506 bounds."507 [#^Mesh mesh triangle-index]508 (let [scratch (Triangle.)]509 (.getTriangle mesh triangle-index scratch)510 scratch))512 (defn triangle-vertex-indices513 "Get the triangle vertex indices of a given triangle from a given514 mesh."515 [#^Mesh mesh triangle-index]516 (let [indices (int-array 3)]517 (.getTriangle mesh triangle-index indices)518 (vec indices)))520 (defn vertex-UV-coord521 "Get the uv-coordinates of the vertex named by vertex-index"522 [#^Mesh mesh vertex-index]523 (let [UV-buffer524 (.getData525 (.getBuffer526 mesh527 VertexBuffer$Type/TexCoord))]528 [(.get UV-buffer (* vertex-index 2))529 (.get UV-buffer (+ 1 (* vertex-index 2)))]))531 (defn triangle-UV-coord532 "Get the uv-cooridnates of the triangle's verticies."533 [#^Mesh mesh width height triangle-index]534 (map (fn [[u v]] (vector (* width u) (* height v)))535 (map (partial vertex-UV-coord mesh)536 (triangle-vertex-indices mesh triangle-index))))538 (defn same-side?539 "Given the points p1 and p2 and the reference point ref, is point p540 on the same side of the line that goes through p1 and p2 as ref is?"541 [p1 p2 ref p]542 (<=543 0544 (.dot545 (.cross (.subtract p2 p1) (.subtract p p1))546 (.cross (.subtract p2 p1) (.subtract ref p1)))))548 (defn triangle-seq [#^Triangle tri]549 [(.get1 tri) (.get2 tri) (.get3 tri)])551 (defn vector3f-seq [#^Vector3f v]552 [(.getX v) (.getY v) (.getZ v)])554 (defn inside-triangle?555 "Is the point inside the triangle?"556 {:author "Dylan Holmes"}557 [#^Triangle tri #^Vector3f p]558 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]559 (and560 (same-side? vert-1 vert-2 vert-3 p)561 (same-side? vert-2 vert-3 vert-1 p)562 (same-side? vert-3 vert-1 vert-2 p))))564 (defn triangle->matrix4f565 "Converts the triangle into a 4x4 matrix: The first three columns566 contain the vertices of the triangle; the last contains the unit567 normal of the triangle. The bottom row is filled with 1s."568 [#^Triangle t]569 (let [mat (Matrix4f.)570 [vert-1 vert-2 vert-3]571 ((comp vec map) #(.get t %) (range 3))572 unit-normal (do (.calculateNormal t)(.getNormal t))573 vertices [vert-1 vert-2 vert-3 unit-normal]]574 (dorun575 (for [row (range 4) col (range 3)]576 (do577 (.set mat col row (.get (vertices row)col))578 (.set mat 3 row 1))))579 mat))581 (defn triangle-transformation582 "Returns the affine transformation that converts each vertex in the583 first triangle into the corresponding vertex in the second584 triangle."585 [#^Triangle tri-1 #^Triangle tri-2]586 (.mult587 (triangle->matrix4f tri-2)588 (.invert (triangle->matrix4f tri-1))))590 (defn point->vector2f [[u v]]591 (Vector2f. u v))593 (defn vector2f->vector3f [v]594 (Vector3f. (.getX v) (.getY v) 0))596 (defn map-triangle [f #^Triangle tri]597 (Triangle.598 (f 0 (.get1 tri))599 (f 1 (.get2 tri))600 (f 2 (.get3 tri))))602 (defn points->triangle603 "Convert a list of points into a triangle."604 [points]605 (apply #(Triangle. %1 %2 %3)606 (map (fn [point]607 (let [point (vec point)]608 (Vector3f. (get point 0 0)609 (get point 1 0)610 (get point 2 0))))611 (take 3 points))))613 (defn convex-bounds614 ;;dylan615 "Returns the smallest square containing the given616 vertices, as a vector of integers [left top width height]."617 ;; "Dimensions of the smallest integer bounding square of the list of618 ;; 2D verticies in the form: [x y width height]."619 [uv-verts]620 (let [xs (map first uv-verts)621 ys (map second uv-verts)622 x0 (Math/floor (apply min xs))623 y0 (Math/floor (apply min ys))624 x1 (Math/ceil (apply max xs))625 y1 (Math/ceil (apply max ys))]626 [x0 y0 (- x1 x0) (- y1 y0)]))628 (defn sensors-in-triangle629 ;;dylan630 "Locate the touch sensors in the triangle, returning a map of their UV and geometry-relative coordinates."631 ;;"Find the locations of the touch sensors within a triangle in both632 ;; UV and gemoetry relative coordinates."633 [image mesh tri-index]634 (let [width (.getWidth image)635 height (.getHeight image)636 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)637 bounds (convex-bounds UV-vertex-coords)639 cutout-triangle (points->triangle UV-vertex-coords)640 UV-sensor-coords641 (filter (comp (partial inside-triangle? cutout-triangle)642 (fn [[u v]] (Vector3f. u v 0)))643 (white-coordinates image bounds))644 UV->geometry (triangle-transformation645 cutout-triangle646 (triangle mesh tri-index))647 geometry-sensor-coords648 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))649 UV-sensor-coords)]650 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))652 (defn-memo locate-feelers653 "Search the geometry's tactile UV image for touch sensors, returning654 their positions in geometry-relative coordinates."655 [#^Geometry geo]656 (let [mesh (.getMesh geo)657 num-triangles (.getTriangleCount mesh)]658 (if-let [image (tactile-sensor-image geo)]659 (map660 (partial sensors-in-triangle image mesh)661 (range num-triangles))662 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))664 (use 'clojure.contrib.def)666 (defn-memo touch-topology [#^Gemoetry geo]667 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))669 (defn-memo feeler-coordinates [#^Geometry geo]670 (vec (map :geometry (locate-feelers geo))))672 (defn enable-touch [#^Geometry geo]673 (let [feeler-coords (feeler-coordinates geo)674 tris (triangles geo)675 limit 0.1676 ;;results (CollisionResults.)677 ]678 (if (empty? (touch-topology geo))679 nil680 (fn [node]681 (let [sensor-origins682 (map683 #(map (partial local-to-world geo) %)684 feeler-coords)685 triangle-normals686 (map (partial get-ray-direction geo)687 tris)688 rays689 (flatten690 (map (fn [origins norm]691 (map #(doto (Ray. % norm)692 (.setLimit limit)) origins))693 sensor-origins triangle-normals))]694 (vector695 (touch-topology geo)696 (vec697 (for [ray rays]698 (do699 (let [results (CollisionResults.)]700 (.collideWith node ray results)701 (let [touch-objects702 (filter #(not (= geo (.getGeometry %)))703 results)]704 (- 255705 (if (empty? touch-objects) 255706 (rem707 (int708 (* 255 (/ (.getDistance709 (first touch-objects)) limit)))710 256))))))))))))))713 (defn touch [#^Node pieces]714 (filter (comp not nil?)715 (map enable-touch716 (filter #(isa? (class %) Geometry)717 (node-seq pieces)))))720 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s721 ;; http://en.wikipedia.org/wiki/Retina723 (defn test-eye []724 (.getChild725 (.getChild (worm-model) "eyes")726 "eye"))729 (defn retina-sensor-image730 "Return a map of pixel selection functions to BufferedImages731 describing the distribution of light-sensitive components on this732 geometry's surface. Each function creates an integer from the rgb733 values found in the pixel. :red, :green, :blue, :gray are already734 defined as extracting the red green blue and average components735 respectively."736 [#^Spatial eye]737 (if-let [eye-map (meta-data eye "eye")]738 (map-vals739 #(ImageToAwt/convert740 (.getImage (.loadTexture (asset-manager) %))741 false false 0)742 (eval (read-string eye-map)))))744 (defn eye-dimensions745 "returns the width and height specified in the metadata of the eye"746 [#^Spatial eye]747 (let [dimensions748 (map #(vector (.getWidth %) (.getHeight %))749 (vals (retina-sensor-image eye)))]750 [(apply max (map first dimensions))751 (apply max (map second dimensions))]))753 (defn creature-eyes754 ;;dylan755 "Return the children of the creature's \"eyes\" node."756 ;;"The eye nodes which are children of the \"eyes\" node in the757 ;;creature."758 [#^Node creature]759 (if-let [eye-node (.getChild creature "eyes")]760 (seq (.getChildren eye-node))761 (do (println-repl "could not find eyes node") [])))763 ;; Here's how vision will work.765 ;; Make the continuation in scene-processor take FrameBuffer,766 ;; byte-buffer, BufferedImage already sized to the correct767 ;; dimensions. the continuation will decide wether to "mix" them768 ;; into the BufferedImage, lazily ignore them, or mix them halfway769 ;; and call c/graphics card routines.771 ;; (vision creature) will take an optional :skip argument which will772 ;; inform the continuations in scene processor to skip the given773 ;; number of cycles; 0 means that no cycles will be skipped.775 ;; (vision creature) will return [init-functions sensor-functions].776 ;; The init-functions are each single-arg functions that take the777 ;; world and register the cameras and must each be called before the778 ;; corresponding sensor-functions. Each init-function returns the779 ;; viewport for that eye which can be manipulated, saved, etc. Each780 ;; sensor-function is a thunk and will return data in the same781 ;; format as the tactile-sensor functions; the structure is782 ;; [topology, sensor-data]. Internally, these sensor-functions783 ;; maintain a reference to sensor-data which is periodically updated784 ;; by the continuation function established by its init-function.785 ;; They can be queried every cycle, but their information may not786 ;; necessairly be different every cycle.788 ;; Each eye in the creature in blender will work the same way as789 ;; joints -- a zero dimensional object with no geometry whose local790 ;; coordinate system determines the orientation of the resulting791 ;; eye. All eyes will have a parent named "eyes" just as all joints792 ;; have a parent named "joints". The resulting camera will be a793 ;; ChaseCamera or a CameraNode bound to the geo that is closest to794 ;; the eye marker. The eye marker will contain the metadata for the795 ;; eye, and will be moved by it's bound geometry. The dimensions of796 ;; the eye's camera are equal to the dimensions of the eye's "UV"797 ;; map.800 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;802 ;; Ears work the same way as vision.804 ;; (hearing creature) will return [init-functions805 ;; sensor-functions]. The init functions each take the world and806 ;; register a SoundProcessor that does foureier transforms on the807 ;; incommong sound data, making it available to each sensor function.809 (defn creature-ears810 "Return the children of the creature's \"ears\" node."811 ;;dylan812 ;;"The ear nodes which are children of the \"ears\" node in the813 ;;creature."814 [#^Node creature]815 (if-let [ear-node (.getChild creature "ears")]816 (seq (.getChildren ear-node))817 (do (println-repl "could not find ears node") [])))819 (defn closest-node820 "Return the object in creature which is closest to the given node."821 ;;dylan"The closest object in creature to the given node."822 [#^Node creature #^Node eye]823 (loop [radius (float 0.01)]824 (let [results (CollisionResults.)]825 (.collideWith826 creature827 (BoundingBox. (.getWorldTranslation eye)828 radius radius radius)829 results)830 (if-let [target (first results)]831 (.getGeometry target)832 (recur (float (* 2 radius)))))))834 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,835 ;;anchor-qualia, augment-organ, with-organ836 (defn bind-sense837 "Bind the sense to the Spatial such that it will maintain its838 current position relative to the Spatial no matter how the spatial839 moves. 'sense can be either a Camera or Listener object."840 [#^Spatial obj sense]841 (let [sense-offset (.subtract (.getLocation sense)842 (.getWorldTranslation obj))843 initial-sense-rotation (Quaternion. (.getRotation sense))844 base-anti-rotation (.inverse (.getWorldRotation obj))]845 (.addControl846 obj847 (proxy [AbstractControl] []848 (controlUpdate [tpf]849 (let [total-rotation850 (.mult base-anti-rotation (.getWorldRotation obj))]851 (.setLocation sense852 (.add853 (.mult total-rotation sense-offset)854 (.getWorldTranslation obj)))855 (.setRotation sense856 (.mult total-rotation initial-sense-rotation))))857 (controlRender [_ _])))))860 (defn update-listener-velocity861 "Update the listener's velocity every update loop."862 [#^Spatial obj #^Listener lis]863 (let [old-position (atom (.getLocation lis))]864 (.addControl865 obj866 (proxy [AbstractControl] []867 (controlUpdate [tpf]868 (let [new-position (.getLocation lis)]869 (.setVelocity870 lis871 (.mult (.subtract new-position @old-position)872 (float (/ tpf))))873 (reset! old-position new-position)))874 (controlRender [_ _])))))876 (import com.aurellem.capture.audio.AudioSendRenderer)878 (defn attach-ear879 [#^Application world #^Node creature #^Spatial ear continuation]880 (let [target (closest-node creature ear)881 lis (Listener.)882 audio-renderer (.getAudioRenderer world)883 sp (sound-processor continuation)]884 (.setLocation lis (.getWorldTranslation ear))885 (.setRotation lis (.getWorldRotation ear))886 (bind-sense target lis)887 (update-listener-velocity target lis)888 (.addListener audio-renderer lis)889 (.registerSoundProcessor audio-renderer lis sp)))891 (defn enable-hearing892 [#^Node creature #^Spatial ear]893 (let [hearing-data (atom [])]894 [(fn [world]895 (attach-ear world creature ear896 (fn [data]897 (reset! hearing-data (vec data)))))898 [(fn []899 (let [data @hearing-data900 topology901 (vec (map #(vector % 0) (range 0 (count data))))902 scaled-data903 (vec904 (map905 #(rem (int (* 255 (/ (+ 1 %) 2))) 256)906 data))]907 [topology scaled-data]))908 ]]))910 (defn hearing911 [#^Node creature]912 (reduce913 (fn [[init-a senses-a]914 [init-b senses-b]]915 [(conj init-a init-b)916 (into senses-a senses-b)])917 [[][]]918 (for [ear (creature-ears creature)]919 (enable-hearing creature ear))))921 (defn attach-eye922 "Attach a Camera to the appropiate area and return the Camera."923 [#^Node creature #^Spatial eye]924 (let [target (closest-node creature eye)925 [cam-width cam-height] (eye-dimensions eye)926 cam (Camera. cam-width cam-height)]927 (.setLocation cam (.getWorldTranslation eye))928 (.setRotation cam (.getWorldRotation eye))929 (.setFrustumPerspective930 cam 45 (/ (.getWidth cam) (.getHeight cam))931 1 1000)932 (bind-sense target cam)933 cam))935 (def presets936 {:all 0xFFFFFF937 :red 0xFF0000938 :blue 0x0000FF939 :green 0x00FF00})941 (defn enable-vision942 "return [init-function sensor-functions] for a particular eye"943 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]944 (let [retinal-map (retina-sensor-image eye)945 camera (attach-eye creature eye)946 vision-image947 (atom948 (BufferedImage. (.getWidth camera)949 (.getHeight camera)950 BufferedImage/TYPE_BYTE_BINARY))]951 [(fn [world]952 (add-eye953 world camera954 (let [counter (atom 0)]955 (fn [r fb bb bi]956 (if (zero? (rem (swap! counter inc) (inc skip)))957 (reset! vision-image (BufferedImage! r fb bb bi)))))))958 (vec959 (map960 (fn [[key image]]961 (let [whites (white-coordinates image)962 topology (vec (collapse whites))963 mask (presets key)]964 (fn []965 (vector966 topology967 (vec968 (for [[x y] whites]969 (bit-and970 mask (.getRGB @vision-image x y))))))))971 retinal-map))]))973 (defn vision974 [#^Node creature & {skip :skip :or {skip 0}}]975 (reduce976 (fn [[init-a senses-a]977 [init-b senses-b]]978 [(conj init-a init-b)979 (into senses-a senses-b)])980 [[][]]981 (for [eye (creature-eyes creature)]982 (enable-vision creature eye))))988 ;; lower level --- nodes989 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair991 ;; higher level -- organs992 ;;994 ;; higher level --- sense/effector995 ;; these are the functions that provide world i/o, chinese-room style1000 (defn blender-creature1001 "Return a creature with all joints in place."1002 [blender-path]1003 (let [model (load-blender-model blender-path)1004 joints (creature-joints model)]1005 (assemble-creature model joints)))1007 (defn gray-scale [num]1008 (+ num1009 (bit-shift-left num 8)1010 (bit-shift-left num 16)))1012 (defn debug-touch-window1013 "creates function that offers a debug view of sensor data"1014 []1015 (let [vi (view-image)]1016 (fn1017 [[coords sensor-data]]1018 (let [image (points->image coords)]1019 (dorun1020 (for [i (range (count coords))]1021 (.setRGB image ((coords i) 0) ((coords i) 1)1022 (gray-scale (sensor-data i)))))1025 (vi image)))))1027 (defn debug-vision-window1028 "creates function that offers a debug view of sensor data"1029 []1030 (let [vi (view-image)]1031 (fn1032 [[coords sensor-data]]1033 (let [image (points->image coords)]1034 (dorun1035 (for [i (range (count coords))]1036 (.setRGB image ((coords i) 0) ((coords i) 1)1037 (sensor-data i))))1038 (vi image)))))1040 (defn debug-hearing-window1041 "view audio data"1042 [height]1043 (let [vi (view-image)]1044 (fn [[coords sensor-data]]1045 (let [image (BufferedImage. (count coords) height1046 BufferedImage/TYPE_INT_RGB)]1047 (dorun1048 (for [x (range (count coords))]1049 (dorun1050 (for [y (range height)]1051 (let [raw-sensor (sensor-data x)]1052 (.setRGB image x y (gray-scale raw-sensor)))))))1054 (vi image)))))1058 ;;(defn test-touch [world creature]1063 ;; here's how motor-control/ proprioception will work: Each muscle is1064 ;; defined by a 1-D array of numbers (the "motor pool") each of which1065 ;; represent muscle fibers. A muscle also has a scalar :strength1066 ;; factor which determines how strong the muscle as a whole is.1067 ;; The effector function for a muscle takes a number < (count1068 ;; motor-pool) and that number is said to "activate" all the muscle1069 ;; fibers whose index is lower than the number. Each fiber will apply1070 ;; force in proportion to its value in the array. Lower values cause1071 ;; less force. The lower values can be put at the "beginning" of the1072 ;; 1-D array to simulate the layout of actual human muscles, which are1073 ;; capable of more percise movements when exerting less force.1075 ;; I don't know how to encode proprioception, so for now, just return1076 ;; a function for each joint that returns a triplet of floats which1077 ;; represent relative roll, pitch, and yaw. Write display code for1078 ;; this though.1080 (defn muscle-fibre-values1081 "Take the first row of the image and return the low-order bytes."1082 [#^BufferedImage image]1083 (let [width (.getWidth image)]1084 (for [x (range width)]1085 (bit-and1086 0xFF1087 (.getRGB image x 0)))))1090 (defn draw-sprite [image sprite x y color ]1091 (dorun1092 (for [[u v] sprite]1093 (.setRGB image (+ u x) (+ v y) color))))1095 (defn view-angle1096 "create a debug view of an angle"1097 [color]1098 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)1099 previous (atom [25 25])1100 sprite [[0 0] [0 1]1101 [0 -1] [-1 0] [1 0]]]1102 (fn [angle]1103 (let [angle (float angle)]1104 (let [position1105 [(+ 25 (int (* 20 (Math/cos angle))))1106 (+ 25 (int (* 20(Math/sin angle))))]]1107 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)1108 (draw-sprite image sprite (position 0) (position 1) color)1109 (reset! previous position))1110 image))))1112 (defn proprioception-debug-window1113 []1114 (let [yaw (view-angle 0xFF0000)1115 roll (view-angle 0x00FF00)1116 pitch (view-angle 0xFFFFFF)1117 v-yaw (view-image)1118 v-roll (view-image)1119 v-pitch (view-image)1120 ]1121 (fn [prop-data]1122 (dorun1123 (map1124 (fn [[y r p]]1125 (v-yaw (yaw y))1126 (v-roll (roll r))1127 (v-pitch (pitch p)))1128 prop-data)))))1136 (defn test-creature [thing]1137 (let [x-axis1138 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)1139 y-axis1140 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)1141 z-axis1142 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)1143 creature (blender-creature thing)1144 touch-nerves (touch creature)1145 touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)1146 [init-vision-fns vision-data] (vision creature)1147 vision-debug (map (fn [_] (debug-vision-window)) vision-data)1148 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)1149 [init-hearing-fns hearing-senses] (hearing creature)1150 hearing-windows (map (fn [_] (debug-hearing-window 50))1151 hearing-senses)1152 bell (AudioNode. (asset-manager)1153 "Sounds/pure.wav" false)1154 prop (proprioception creature)1155 prop-debug (proprioception-debug-window)1156 ;; dream1158 ]1161 (apply1162 world1163 (with-movement1164 (.getChild creature "worm-21")1165 ["key-r" "key-t"1166 "key-f" "key-g"1167 "key-v" "key-b"]1168 [10 10 10 10 1 1]1169 [(nodify [creature1170 (box 10 2 10 :position (Vector3f. 0 -9 0)1171 :color ColorRGBA/Gray :mass 0)1172 x-axis y-axis z-axis1173 me1174 ])1175 (merge standard-debug-controls1176 {"key-return"1177 (fn [_ value]1178 (if value1179 (do1180 (println-repl "play-sound")1181 (.play bell))))})1182 (fn [world]1183 (light-up-everything world)1184 (enable-debug world)1185 (dorun (map #(% world) init-vision-fns))1186 (dorun (map #(% world) init-hearing-fns))1188 (add-eye world1189 (attach-eye creature (test-eye))1190 (comp (view-image) BufferedImage!))1192 (add-eye world (.getCamera world) no-op)1193 (set-gravity world (Vector3f. 0 0 0))1194 ;;(com.aurellem.capture.Capture/captureVideo1195 ;; world (file-str "/home/r/proj/ai-videos/hand"))1196 ;;(.setTimer world (RatchetTimer. 60))1197 (speed-up world)1198 ;;(set-gravity world (Vector3f. 0 0 0))1199 )1200 (fn [world tpf]1201 ;;(dorun1202 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))1204 (prop-debug (prop))1206 (dorun1207 (map #(%1 (%2 (.getRootNode world)))1208 touch-debug-windows touch-nerves))1210 (dorun1211 (map #(%1 (%2))1212 vision-debug vision-data))1213 (dorun1214 (map #(%1 (%2)) hearing-windows hearing-senses))1217 ;;(println-repl (vision-data))1218 (.setLocalTranslation me (.getLocation (.getCamera world)))1221 )]1222 ;;(let [timer (atom 0)]1223 ;; (fn [_ _]1224 ;; (swap! timer inc)1225 ;; (if (= (rem @timer 60) 0)1226 ;; (println-repl (float (/ @timer 60))))))1227 ))))1237 ;;; experiments in collisions1241 (defn collision-test []1242 (let [b-radius 11243 b-position (Vector3f. 0 0 0)1244 obj-b (box 1 1 1 :color ColorRGBA/Blue1245 :position b-position1246 :mass 0)1247 node (nodify [obj-b])1248 bounds-b1249 (doto (Picture.)1250 (.setHeight 50)1251 (.setWidth 50)1252 (.setImage (asset-manager)1253 "Models/creature1/hand.png"1254 false1255 ))1257 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))1259 collisions1260 (let [cr (CollisionResults.)]1261 (.collideWith node bounds-b cr)1262 (println (map #(.getContactPoint %) cr))1263 cr)1265 ;;collision-points1266 ;;(map #(sphere 0.1 :position (.getContactPoint %))1267 ;; collisions)1269 ;;node (nodify (conj collision-points obj-b))1271 sim1272 (world node1273 {"key-space"1274 (fn [_ value]1275 (if value1276 (let [cr (CollisionResults.)]1277 (.collideWith node bounds-b cr)1278 (println-repl (map #(.getContactPoint %) cr))1279 cr)))}1280 no-op1281 no-op)1283 ]1284 sim1286 ))1289 ;; the camera will stay in its initial position/rotation with relation1290 ;; to the spatial.1293 (defn follow-test1294 "show a camera that stays in the same relative position to a blue cube."1295 []1296 (let [camera-pos (Vector3f. 0 30 0)1297 rock (box 1 1 1 :color ColorRGBA/Blue1298 :position (Vector3f. 0 10 0)1299 :mass 301300 )1301 rot (.getWorldRotation rock)1303 table (box 3 1 10 :color ColorRGBA/Gray :mass 01304 :position (Vector3f. 0 -3 0))]1306 (world1307 (nodify [rock table])1308 standard-debug-controls1309 (fn [world]1310 (let1311 [cam (doto (.clone (.getCamera world))1312 (.setLocation camera-pos)1313 (.lookAt Vector3f/ZERO1314 Vector3f/UNIT_X))]1315 (bind-sense rock cam)1317 (.setTimer world (RatchetTimer. 60))1318 (add-eye world cam (comp (view-image) BufferedImage!))1319 (add-eye world (.getCamera world) no-op))1320 )1321 (fn [_ _] (println-repl rot)))))1325 #+end_src1327 #+results: body-11328 : #'cortex.silly/follow-test1331 * COMMENT purgatory1332 #+begin_src clojure1333 (defn bullet-trans []1334 (let [obj-a (sphere 0.5 :color ColorRGBA/Red1335 :position (Vector3f. -10 5 0))1336 obj-b (sphere 0.5 :color ColorRGBA/Blue1337 :position (Vector3f. -10 -5 0)1338 :mass 0)1339 control-a (.getControl obj-a RigidBodyControl)1340 control-b (.getControl obj-b RigidBodyControl)1341 swivel1342 (.toRotationMatrix1343 (doto (Quaternion.)1344 (.fromAngleAxis (/ Math/PI 2)1345 Vector3f/UNIT_X)))]1346 (doto1347 (ConeJoint.1348 control-a control-b1349 (Vector3f. 0 5 0)1350 (Vector3f. 0 -5 0)1351 swivel swivel)1352 (.setLimit (* 0.6 (/ Math/PI 4))1353 (/ Math/PI 4)1354 (* Math/PI 0.8)))1355 (world (nodify1356 [obj-a obj-b])1357 standard-debug-controls1358 enable-debug1359 no-op)))1362 (defn bullet-trans* []1363 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red1364 :position (Vector3f. 5 0 0)1365 :mass 90)1366 obj-b (sphere 0.5 :color ColorRGBA/Blue1367 :position (Vector3f. -5 0 0)1368 :mass 0)1369 control-a (.getControl obj-a RigidBodyControl)1370 control-b (.getControl obj-b RigidBodyControl)1371 move-up? (atom nil)1372 move-down? (atom nil)1373 move-left? (atom nil)1374 move-right? (atom nil)1375 roll-left? (atom nil)1376 roll-right? (atom nil)1377 force 1001378 swivel1379 (.toRotationMatrix1380 (doto (Quaternion.)1381 (.fromAngleAxis (/ Math/PI 2)1382 Vector3f/UNIT_X)))1383 x-move1384 (doto (Matrix3f.)1385 (.fromStartEndVectors Vector3f/UNIT_X1386 (.normalize (Vector3f. 1 1 0))))1388 timer (atom 0)]1389 (doto1390 (ConeJoint.1391 control-a control-b1392 (Vector3f. -8 0 0)1393 (Vector3f. 2 0 0)1394 ;;swivel swivel1395 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY1396 x-move Matrix3f/IDENTITY1397 )1398 (.setCollisionBetweenLinkedBodys false)1399 (.setLimit (* 1 (/ Math/PI 4)) ;; twist1400 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane1401 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane1402 (world (nodify1403 [obj-a obj-b])1404 (merge standard-debug-controls1405 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))1406 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))1407 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))1408 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))1409 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))1410 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})1412 (fn [world]1413 (enable-debug world)1414 (set-gravity world Vector3f/ZERO)1415 )1417 (fn [world _]1419 (if @move-up?1420 (.applyForce control-a1421 (Vector3f. force 0 0)1422 (Vector3f. 0 0 0)))1423 (if @move-down?1424 (.applyForce control-a1425 (Vector3f. (- force) 0 0)1426 (Vector3f. 0 0 0)))1427 (if @move-left?1428 (.applyForce control-a1429 (Vector3f. 0 force 0)1430 (Vector3f. 0 0 0)))1431 (if @move-right?1432 (.applyForce control-a1433 (Vector3f. 0 (- force) 0)1434 (Vector3f. 0 0 0)))1436 (if @roll-left?1437 (.applyForce control-a1438 (Vector3f. 0 0 force)1439 (Vector3f. 0 0 0)))1440 (if @roll-right?1441 (.applyForce control-a1442 (Vector3f. 0 0 (- force))1443 (Vector3f. 0 0 0)))1445 (if (zero? (rem (swap! timer inc) 100))1446 (.attachChild1447 (.getRootNode world)1448 (sphere 0.05 :color ColorRGBA/Yellow1449 :physical? false :position1450 (.getWorldTranslation obj-a)))))1451 )1452 ))1454 (defn transform-trianglesdsd1455 "Transform that converts each vertex in the first triangle1456 into the corresponding vertex in the second triangle."1457 [#^Triangle tri-1 #^Triangle tri-2]1458 (let [in [(.get1 tri-1)1459 (.get2 tri-1)1460 (.get3 tri-1)]1461 out [(.get1 tri-2)1462 (.get2 tri-2)1463 (.get3 tri-2)]]1464 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))1465 in* [(.mult translate (in 0))1466 (.mult translate (in 1))1467 (.mult translate (in 2))]1468 final-translation1469 (doto (Matrix4f.)1470 (.setTranslation (out 1)))1472 rotate-11473 (doto (Matrix3f.)1474 (.fromStartEndVectors1475 (.normalize1476 (.subtract1477 (in* 1) (in* 0)))1478 (.normalize1479 (.subtract1480 (out 1) (out 0)))))1481 in** [(.mult rotate-1 (in* 0))1482 (.mult rotate-1 (in* 1))1483 (.mult rotate-1 (in* 2))]1484 scale-factor-11485 (.mult1486 (.normalize1487 (.subtract1488 (out 1)1489 (out 0)))1490 (/ (.length1491 (.subtract (out 1)1492 (out 0)))1493 (.length1494 (.subtract (in** 1)1495 (in** 0)))))1496 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))1497 in*** [(.mult scale-1 (in** 0))1498 (.mult scale-1 (in** 1))1499 (.mult scale-1 (in** 2))]1505 ]1507 (dorun (map println in))1508 (println)1509 (dorun (map println in*))1510 (println)1511 (dorun (map println in**))1512 (println)1513 (dorun (map println in***))1514 (println)1516 ))))1519 (defn world-setup [joint]1520 (let [joint-position (Vector3f. 0 0 0)1521 joint-rotation1522 (.toRotationMatrix1523 (.mult1524 (doto (Quaternion.)1525 (.fromAngleAxis1526 (* 1 (/ Math/PI 4))1527 (Vector3f. -1 0 0)))1528 (doto (Quaternion.)1529 (.fromAngleAxis1530 (* 1 (/ Math/PI 2))1531 (Vector3f. 0 0 1)))))1532 top-position (.mult joint-rotation (Vector3f. 8 0 0))1534 origin (doto1535 (sphere 0.1 :physical? false :color ColorRGBA/Cyan1536 :position top-position))1537 top (doto1538 (sphere 0.1 :physical? false :color ColorRGBA/Yellow1539 :position top-position)1541 (.addControl1542 (RigidBodyControl.1543 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))1544 bottom (doto1545 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray1546 :position (Vector3f. 0 0 0))1547 (.addControl1548 (RigidBodyControl.1549 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))1550 table (box 10 2 10 :position (Vector3f. 0 -20 0)1551 :color ColorRGBA/Gray :mass 0)1552 a (.getControl top RigidBodyControl)1553 b (.getControl bottom RigidBodyControl)]1555 (cond1556 (= joint :cone)1558 (doto (ConeJoint.1559 a b1560 (world-to-local top joint-position)1561 (world-to-local bottom joint-position)1562 joint-rotation1563 joint-rotation1564 )1567 (.setLimit (* (/ 10) Math/PI)1568 (* (/ 4) Math/PI)1569 0)))1570 [origin top bottom table]))1572 (defn test-joint [joint]1573 (let [[origin top bottom floor] (world-setup joint)1574 control (.getControl top RigidBodyControl)1575 move-up? (atom false)1576 move-down? (atom false)1577 move-left? (atom false)1578 move-right? (atom false)1579 roll-left? (atom false)1580 roll-right? (atom false)1581 timer (atom 0)]1583 (world1584 (nodify [top bottom floor origin])1585 (merge standard-debug-controls1586 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))1587 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))1588 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))1589 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))1590 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))1591 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})1593 (fn [world]1594 (light-up-everything world)1595 (enable-debug world)1596 (set-gravity world (Vector3f. 0 0 0))1597 )1599 (fn [world _]1600 (if (zero? (rem (swap! timer inc) 100))1601 (do1602 ;; (println-repl @timer)1603 (.attachChild (.getRootNode world)1604 (sphere 0.05 :color ColorRGBA/Yellow1605 :position (.getWorldTranslation top)1606 :physical? false))1607 (.attachChild (.getRootNode world)1608 (sphere 0.05 :color ColorRGBA/LightGray1609 :position (.getWorldTranslation bottom)1610 :physical? false))))1612 (if @move-up?1613 (.applyTorque control1614 (.mult (.getPhysicsRotation control)1615 (Vector3f. 0 0 10))))1616 (if @move-down?1617 (.applyTorque control1618 (.mult (.getPhysicsRotation control)1619 (Vector3f. 0 0 -10))))1620 (if @move-left?1621 (.applyTorque control1622 (.mult (.getPhysicsRotation control)1623 (Vector3f. 0 10 0))))1624 (if @move-right?1625 (.applyTorque control1626 (.mult (.getPhysicsRotation control)1627 (Vector3f. 0 -10 0))))1628 (if @roll-left?1629 (.applyTorque control1630 (.mult (.getPhysicsRotation control)1631 (Vector3f. -1 0 0))))1632 (if @roll-right?1633 (.applyTorque control1634 (.mult (.getPhysicsRotation control)1635 (Vector3f. 1 0 0))))))))1639 (defprotocol Frame1640 (frame [this]))1642 (extend-type BufferedImage1643 Frame1644 (frame [image]1645 (merge1646 (apply1647 hash-map1648 (interleave1649 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]1650 (vector x y)))1651 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]1652 (let [data (.getRGB image x y)]1653 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)1654 :g (bit-shift-right (bit-and 0x00ff00 data) 8)1655 :b (bit-and 0x0000ff data)))))))1656 {:width (.getWidth image) :height (.getHeight image)})))1659 (extend-type ImagePlus1660 Frame1661 (frame [image+]1662 (frame (.getBufferedImage image+))))1665 #+end_src1668 * COMMENT generate source1669 #+begin_src clojure :tangle ../src/cortex/silly.clj1670 <<body-1>>1671 #+end_src