comparison org/test-creature.org @ 116:947bef5d6670

working on eye-following camera
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Jan 2012 01:07:46 -0700
parents 247860e25536
children 94c005f7f9dd
comparison
equal deleted inserted replaced
115:247860e25536 116:947bef5d6670
178 178
179 (defn joint-targets 179 (defn joint-targets
180 "Return the two closest two objects to the joint object, ordered 180 "Return the two closest two objects to the joint object, ordered
181 from bottom to top according to the joint's rotation." 181 from bottom to top according to the joint's rotation."
182 [#^Node parts #^Node joint] 182 [#^Node parts #^Node joint]
183 ;;(println (meta-data joint "joint"))
184 (.getWorldRotation joint)
185 (loop [radius (float 0.01)] 183 (loop [radius (float 0.01)]
186 (let [results (CollisionResults.)] 184 (let [results (CollisionResults.)]
187 (.collideWith 185 (.collideWith
188 parts 186 parts
189 (BoundingBox. (.getWorldTranslation joint) 187 (BoundingBox. (.getWorldTranslation joint)
345 (joint-targets pieces joint)] 343 (joint-targets pieces joint)]
346 (connect obj-a obj-b joint))) 344 (connect obj-a obj-b joint)))
347 joints)) 345 joints))
348 pieces) 346 pieces)
349 347
350 (defn blender-creature [blender-path] 348 (declare blender-creature)
351 (let [model (load-blender-model blender-path)
352 joints
353 (if-let [joint-node (.getChild model "joints")]
354 (seq (.getChildren joint-node))
355 (do (println-repl "could not find joints node")
356 []))]
357 (assemble-creature model joints)))
358 349
359 (def hand "Models/creature1/one.blend") 350 (def hand "Models/creature1/one.blend")
360 351
361 (def worm "Models/creature1/try-again.blend") 352 (def worm "Models/creature1/try-again.blend")
362 353
648 (.getImage (.loadTexture (asset-manager) %)) 639 (.getImage (.loadTexture (asset-manager) %))
649 false false 0) 640 false false 0)
650 (read-string 641 (read-string
651 eye-map)))) 642 eye-map))))
652 643
653 644 (defn creature-eyes
654 (defn enable-vision 645 "The eye nodes which are children of the \"eyes\" node in the
655 646 creature."
656 ;; need to create a camera based on uv image, 647 [#^Node creature]
657 ;; update this camera every frame based on the position of this 648 (if-let [eye-node (.getChild creature "eyes")]
658 ;; geometry. (maybe can get cam to follow the object) 649 (seq (.getChildren eye-node))
659 650 (do (println-repl "could not find eyes node") [])))
660 ;; use a stack for the continuation to grab the image. 651
661
662
663 [#^Geometry eye]
664
665 652
666 ;; Here's how vision will work. 653 ;; Here's how vision will work.
667 654
668 ;; Make the continuation in scene-processor take FrameBuffer, 655 ;; Make the continuation in scene-processor take FrameBuffer,
669 ;; byte-buffer, BufferedImage already sized to the correct 656 ;; byte-buffer, BufferedImage already sized to the correct
696 ;; ChaseCamera or a CameraNode bound to the geo that is closest to 683 ;; ChaseCamera or a CameraNode bound to the geo that is closest to
697 ;; the eye marker. The eye marker will contain the metadata for the 684 ;; the eye marker. The eye marker will contain the metadata for the
698 ;; eye, and will be moved by it's bound geometry. The dimensions of 685 ;; eye, and will be moved by it's bound geometry. The dimensions of
699 ;; the eye's camera are equal to the dimensions of the eye's "UV" 686 ;; the eye's camera are equal to the dimensions of the eye's "UV"
700 ;; map. 687 ;; map.
688
689 (defn eye-target
690 "The closest object in creature to eye."
691 [#^Node creature #^Node eye]
692 (loop [radius (float 0.01)]
693 (let [results (CollisionResults.)]
694 (.collideWith
695 creature
696 (BoundingBox. (.getWorldTranslation eye)
697 radius radius radius)
698 results)
699 (if-let [target (first results)]
700 (.getGeometry target)
701 (recur (float (* 2 radius)))))))
702
703 (defn attach-eyes
704 "For each eye in the creature, attach a CameraNode to the appropiate
705 area and return the Camera."
706 [#^Node creature]
707 (for [eye (creature-eyes creature)]
708 (let [target (eye-target creature eye)]
709 (CameraNode
710 )
701 711
712 (defn vision
713
714 ;; need to create a camera based on uv image,
715 ;; update this camera every frame based on the position of this
716 ;; geometry. (maybe can get cam to follow the object)
717
718 ;; use a stack for the continuation to grab the image.
719
720
721 [#^Geometry eye]
722
723
724
702 725
703 ) 726 )
727
728
729 (defn blender-creature
730 "Return a creature with all joints in place."
731 [blender-path]
732 (let [model (load-blender-model blender-path)
733 joints
734 (if-let [joint-node (.getChild model "joints")]
735 (seq (.getChildren joint-node))
736 (do (println-repl "could not find joints node") []))]
737 (assemble-creature model joints)))
738
739
740
741
742
704 743
705 (defn debug-window 744 (defn debug-window
706 "creates function that offers a debug view of sensor data" 745 "creates function that offers a debug view of sensor data"
707 [] 746 []
708 (let [vi (view-image)] 747 (let [vi (view-image)]
821 ] 860 ]
822 sim 861 sim
823 862
824 )) 863 ))
825 864
865
866 ;; the camera will stay in its initial position/rotation with relation
867 ;; to the spatial.
868
869 (defn bind-camera [#^Spatial obj #^Camera cam]
870 (let [cam-offset (.subtract (.getLocation cam)
871 (.getWorldTranslation obj))
872 initial-cam-rotation (.getRotation cam)
873 base-anti-rotation (.inverse (.getWorldRotation obj))]
874 (.addControl
875 obj
876 (proxy [AbstractControl] []
877 (controlUpdate [tpf]
878 (let [total-rotation
879 (.mult base-anti-rotation (.getWorldRotation obj))]
880
881 (.setLocation cam
882 (.add
883 (.mult total-rotation cam-offset)
884 (.getWorldTranslation obj)))
885 (.setRotation cam
886 initial-cam-rotation)
887 ;;(.mult total-rotation initial-cam-rotation)
888
889 ))
890
891 (controlRender [_ _])))))
826 892
827 893
828 894
895 (defn follow-test []
896 (let [camera-pos (Vector3f. 0 30 0)
897 rock (box 1 1 1 :color ColorRGBA/Blue
898 :position (Vector3f. 0 10 0)
899 :mass 30
900 )
901
902 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
903 :position (Vector3f. 0 -3 0))]
904
905 (world
906 (nodify [rock table])
907 standard-debug-controls
908 (fn [world]
909 (let
910 [cam (doto (.clone (.getCamera world))
911 (.setLocation camera-pos)
912 (.lookAt Vector3f/ZERO
913 Vector3f/UNIT_X))]
914 (bind-camera rock cam)
915
916 (.setTimer world (RatchetTimer. 60))
917 (add-eye world cam (comp (view-image) BufferedImage!))
918 (add-eye world (.getCamera world) no-op))
919 )
920 no-op)))
921
829 #+end_src 922 #+end_src
830 923
831 #+results: body-1 924 #+results: body-1
832 : #'cortex.silly/test-creature 925 : #'cortex.silly/test-creature
833 926