annotate org/test-creature.org @ 87:af1bb43661f9

working on multimethod joint creation
author Robert McIntyre <rlm@mit.edu>
date Sat, 07 Jan 2012 05:06:58 -0700
parents 00ab1f10266f
children 3e929630a25f
rev   line source
rlm@73 1 #+title: First attempt at a creature!
rlm@73 2 #+author: Robert McIntyre
rlm@73 3 #+email: rlm@mit.edu
rlm@73 4 #+description:
rlm@73 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@73 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@73 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@73 8
rlm@73 9 * Intro
rlm@73 10 So far, I've made the following senses --
rlm@73 11 - Vision
rlm@73 12 - Hearing
rlm@73 13 - Touch
rlm@73 14 - Proprioception
rlm@73 15
rlm@73 16 And one effector:
rlm@73 17 - Movement
rlm@73 18
rlm@73 19 However, the code so far has only enabled these senses, but has not
rlm@73 20 actually implemented them. For example, there is still a lot of work
rlm@73 21 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 22 simulation that can be moved around and see the world from different
rlm@73 23 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 24 for the visual input, and I need to determine how/wether to
rlm@73 25 disceritise the visual input.
rlm@73 26
rlm@73 27 I also want to be able to visualize both the sensors and the
rlm@73 28 effectors in pretty pictures. This semi-retarted creature will by my
rlm@73 29 first attempt at bringing everything together.
rlm@73 30
rlm@73 31 * The creature's body
rlm@73 32
rlm@73 33 Still going to do an eve-like body in blender, but due to problems
rlm@73 34 importing the joints, etc into jMonkeyEngine3, I',m going to do all
rlm@73 35 the connecting here in clojure code, using the names of the individual
rlm@73 36 components and trial and error. Later, I'll maybe make some sort of
rlm@73 37 creature-building modifications to blender that support whatever
rlm@73 38 discreitized senses I'm going to make.
rlm@73 39
rlm@73 40 #+name: body-1
rlm@73 41 #+begin_src clojure
rlm@73 42 (ns cortex.silly
rlm@73 43 "let's play!"
rlm@73 44 {:author "Robert McIntyre"})
rlm@73 45
rlm@73 46 ;; TODO remove this!
rlm@73 47 (require 'cortex.import)
rlm@73 48 (cortex.import/mega-import-jme3)
rlm@73 49 (use '(cortex world util body hearing touch vision))
rlm@73 50
rlm@73 51 (rlm.rlm-commands/help)
rlm@73 52
rlm@87 53 (declare joint-create)
rlm@83 54
rlm@83 55 (defn load-bullet []
rlm@84 56 (let [sim (world (Node.) {} no-op no-op)]
rlm@84 57 (.enqueue
rlm@84 58 sim
rlm@84 59 (fn []
rlm@84 60 (.stop sim)))
rlm@84 61 (.start sim)))
rlm@83 62
rlm@73 63 (defn load-blender-model
rlm@73 64 "Load a .blend file using an asset folder relative path."
rlm@73 65 [^String model]
rlm@73 66 (.loadModel
rlm@73 67 (doto (asset-manager)
rlm@73 68 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 69 model))
rlm@73 70
rlm@74 71 (defn meta-data [blender-node key]
rlm@74 72 (if-let [data (.getUserData blender-node "properties")]
rlm@74 73 (.findValue data key)
rlm@74 74 nil))
rlm@73 75
rlm@78 76 (defn blender-to-jme
rlm@78 77 "Convert from Blender coordinates to JME coordinates"
rlm@78 78 [#^Vector3f in]
rlm@78 79 (Vector3f. (.getX in)
rlm@78 80 (.getZ in)
rlm@78 81 (- (.getY in))))
rlm@74 82
rlm@79 83 (defn jme-to-blender
rlm@79 84 "Convert from JME coordinates to Blender coordinates"
rlm@79 85 [#^Vector3f in]
rlm@79 86 (Vector3f. (.getX in)
rlm@79 87 (- (.getZ in))
rlm@79 88 (.getY in)))
rlm@79 89
rlm@78 90 (defn joint-targets
rlm@78 91 "Return the two closest two objects to the joint object, ordered
rlm@78 92 from bottom to top according to the joint's rotation."
rlm@78 93 [#^Node parts #^Node joint]
rlm@78 94 ;;(println (meta-data joint "joint"))
rlm@78 95 (.getWorldRotation joint)
rlm@78 96 (loop [radius (float 0.01)]
rlm@78 97 (let [results (CollisionResults.)]
rlm@78 98 (.collideWith
rlm@78 99 parts
rlm@78 100 (BoundingBox. (.getWorldTranslation joint)
rlm@78 101 radius radius radius)
rlm@78 102 results)
rlm@78 103 (let [targets
rlm@78 104 (distinct
rlm@78 105 (map #(.getGeometry %) results))]
rlm@78 106 (if (>= (count targets) 2)
rlm@78 107 (sort-by
rlm@79 108 #(let [v
rlm@79 109 (jme-to-blender
rlm@79 110 (.mult
rlm@79 111 (.inverse (.getWorldRotation joint))
rlm@79 112 (.subtract (.getWorldTranslation %)
rlm@79 113 (.getWorldTranslation joint))))]
rlm@79 114 (println-repl (.getName %) ":" v)
rlm@79 115 (.dot (Vector3f. 1 1 1)
rlm@79 116 v))
rlm@78 117 (take 2 targets))
rlm@78 118 (recur (float (* radius 2))))))))
rlm@74 119
rlm@87 120 (defn world-to-local
rlm@87 121 "Convert the world coordinates into coordinates relative to the
rlm@87 122 object (i.e. local coordinates), taking into account the rotation
rlm@87 123 of object."
rlm@87 124 [#^Spatial object world-coordinate]
rlm@87 125 (let [out (Vector3f.)]
rlm@87 126 (.worldToLocal object world-coordinate out) out))
rlm@87 127
rlm@87 128 (defmulti zz
rlm@87 129 (fn [a b _ _ _ _ _]
rlm@87 130
rlm@87 131 (:type a)))
rlm@87 132 (defmethod zz :p [a b] (println "hi"))
rlm@87 133
rlm@87 134
rlm@87 135 (defmulti joint-dispatch
rlm@87 136 "Translate blender pseudo-joints into real JME joints."
rlm@87 137 (fn [constraints _ _ _ _ _]
rlm@87 138 (:type constraints)))
rlm@87 139
rlm@87 140 (defmethod joint-dispatch :point
rlm@87 141 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 142 (println-repl "creating POINT2POINT joint")
rlm@87 143 (Point2PointJoint.
rlm@87 144 control-a
rlm@87 145 control-b
rlm@87 146 pivot-a
rlm@87 147 pivot-b))
rlm@87 148
rlm@87 149 (defmethod joint-dispatch :hinge
rlm@87 150 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 151 (println-repl "creating HINGE joint")
rlm@87 152 (let [axis
rlm@87 153 (if-let
rlm@87 154 [axis (:axis constraints)]
rlm@87 155 axis
rlm@87 156 Vector3f/UNIT_X)
rlm@87 157 [limit-1 limit-2] (:limit constraints)
rlm@87 158 hinge-axis
rlm@87 159 (.mult
rlm@87 160 rotation
rlm@87 161 (blender-to-jme axis))]
rlm@87 162 (doto
rlm@87 163 (HingeJoint.
rlm@87 164 control-a
rlm@87 165 control-b
rlm@87 166 pivot-a
rlm@87 167 pivot-b
rlm@87 168 hinge-axis
rlm@87 169 hinge-axis)
rlm@87 170 (.setLimit limit-1 limit-2))))
rlm@87 171
rlm@87 172
rlm@87 173 (defmethod joint-dispatch :cone
rlm@87 174 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 175 (let [limit-xz (:limit-xz constraints)
rlm@87 176 limit-xy (:limit-xy constraints)
rlm@87 177 twist (:twist constraints)]
rlm@87 178
rlm@87 179
rlm@87 180 (println-repl "creating CONE joint")
rlm@87 181 (println-repl rotation)
rlm@87 182 (println-repl
rlm@87 183 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 184 (println-repl
rlm@87 185 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 186 (println-repl
rlm@87 187 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 188 (doto
rlm@87 189 (ConeJoint.
rlm@87 190 control-a
rlm@87 191 control-b
rlm@87 192 pivot-a
rlm@87 193 pivot-b
rlm@87 194 rotation
rlm@87 195 rotation)
rlm@87 196 (.setLimit (float limit-xz)
rlm@87 197 (float limit-xy)
rlm@87 198 (float twist)))))
rlm@87 199
rlm@87 200
rlm@87 201
rlm@87 202 (defn connect*
rlm@87 203 "here are some examples:
rlm@87 204 {:type :point}
rlm@87 205 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 206 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 207
rlm@87 208 {:type :cone :limit-xz 0]
rlm@87 209 :limit-xy 0]
rlm@87 210 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 211 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 212 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 213 control-b (.getControl obj-b RigidBodyControl)
rlm@87 214 joint-center (.getWorldTranslation joint)
rlm@87 215 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 216 pivot-a (world-to-local obj-a joint-center)
rlm@87 217 pivot-b (world-to-local obj-b joint-center)]
rlm@87 218 ;; A side-effect of creating a joint registers
rlm@87 219 ;; it with both physics objects which in turn
rlm@87 220 ;; will register the joint with the physics system
rlm@87 221 ;; when the simulation is started.
rlm@87 222 (if-let [constraints
rlm@87 223 (map-vals
rlm@87 224 eval
rlm@87 225 (read-string
rlm@87 226 (meta-data joint "joint")))]
rlm@87 227 (do
rlm@87 228 (println-repl "creating joint between"
rlm@87 229 (.getName obj-a) "and" (.getName obj-b))
rlm@87 230 (joint-dispatch constraints
rlm@87 231 control-a control-b
rlm@87 232 pivot-a pivot-b
rlm@87 233 joint-rotation))
rlm@87 234
rlm@87 235 (println-repl "could not find joint meta-data!"))))
rlm@87 236
rlm@87 237
rlm@87 238
rlm@87 239
rlm@78 240 (defn connect
rlm@78 241 "here are some examples:
rlm@78 242 {:type :point}
rlm@78 243 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@78 244 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@74 245
rlm@78 246 {:type :cone :limit-xz 0]
rlm@79 247 :limit-xy 0]
rlm@79 248 :twist 0]} (use XZY rotation mode in blender!)"
rlm@81 249 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 250 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 251 control-b (.getControl obj-b RigidBodyControl)
rlm@81 252 joint-center (.getWorldTranslation joint)
rlm@87 253 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 254 pivot-a (world-to-local obj-a joint-center)
rlm@87 255 pivot-b (world-to-local obj-b joint-center)]
rlm@81 256 ;; A side-effect of creating a joint registers
rlm@81 257 ;; it with both physics objects which in turn
rlm@81 258 ;; will register the joint with the physics system
rlm@81 259 ;; when the simulation is started.
rlm@81 260 (if-let [constraints
rlm@81 261 (map-vals
rlm@81 262 eval
rlm@81 263 (read-string
rlm@81 264 (meta-data joint "joint")))]
rlm@74 265
rlm@81 266 (let [joint-type (:type constraints)]
rlm@81 267 (println-repl "creating joint between"
rlm@81 268 (.getName obj-a) "and" (.getName obj-b))
rlm@81 269 (cond (= :point joint-type)
rlm@81 270 (do
rlm@81 271 (println-repl "creating POINT joint")
rlm@81 272 (Point2PointJoint.
rlm@81 273 control-a
rlm@81 274 control-b
rlm@81 275 pivot-a
rlm@81 276 pivot-b))
rlm@81 277 (= :hinge joint-type)
rlm@81 278 (do
rlm@81 279 (println-repl "creating HINGE joint")
rlm@81 280 (let [axis (if-let
rlm@81 281 [axis (:axis constraints)]
rlm@81 282 axis
rlm@81 283 Vector3f/UNIT_X)
rlm@81 284 [limit-1 limit-2] (:limit constraints)
rlm@81 285 hinge-axis
rlm@81 286 (.mult
rlm@81 287 (.getWorldRotation joint)
rlm@81 288 (blender-to-jme axis))]
rlm@81 289 (doto
rlm@81 290 (HingeJoint.
rlm@81 291 control-a
rlm@81 292 control-b
rlm@81 293 pivot-a
rlm@81 294 pivot-b
rlm@81 295 hinge-axis
rlm@81 296 hinge-axis)
rlm@81 297 (.setLimit limit-1 limit-2))))
rlm@81 298 (= :cone joint-type)
rlm@81 299 (do
rlm@81 300 (let [limit-xz (:limit-xz constraints)
rlm@81 301 limit-xy (:limit-xy constraints)
rlm@87 302 twist (:twist constraints)]
rlm@87 303
rlm@81 304
rlm@81 305 (println-repl "creating CONE joint")
rlm@87 306 (println-repl joint-rotation)
rlm@85 307 (println-repl
rlm@87 308 "UNIT_X --> " (.mult joint-rotation (Vector3f. 1 0 0)))
rlm@85 309 (println-repl
rlm@87 310 "UNIT_Y --> " (.mult joint-rotation (Vector3f. 0 1 0)))
rlm@85 311 (println-repl
rlm@87 312 "UNIT_Z --> " (.mult joint-rotation (Vector3f. 0 0 1)))
rlm@85 313 (doto
rlm@85 314 (ConeJoint.
rlm@85 315 control-a
rlm@85 316 control-b
rlm@85 317 pivot-a
rlm@85 318 pivot-b
rlm@87 319 joint-rotation
rlm@87 320 joint-rotation
rlm@85 321 )
rlm@85 322 (.setLimit (float limit-xz)
rlm@85 323 (float limit-xy)
rlm@85 324 (float twist)))))
rlm@81 325 true
rlm@81 326 (println-repl
rlm@81 327 "joint-type" joint-type "not recognized")))
rlm@81 328
rlm@81 329 (println-repl "could not find joint meta-data!"))))
rlm@74 330
rlm@83 331
rlm@78 332 (defn assemble-creature [#^Node pieces joints]
rlm@78 333 (dorun
rlm@78 334 (map
rlm@78 335 (fn [geom]
rlm@78 336 (let [physics-control
rlm@78 337 (RigidBodyControl.
rlm@78 338 (HullCollisionShape.
rlm@78 339 (.getMesh geom))
rlm@78 340 (if-let [mass (meta-data geom "mass")]
rlm@78 341 (do
rlm@78 342 (println-repl
rlm@78 343 "setting" (.getName geom) "mass to" (float mass))
rlm@78 344 (float mass))
rlm@78 345 (float 1)))]
rlm@78 346
rlm@78 347 (.addControl geom physics-control)))
rlm@78 348 (filter #(isa? (class %) Geometry )
rlm@78 349 (node-seq pieces))))
rlm@77 350
rlm@78 351 (dorun
rlm@78 352 (map
rlm@78 353 (fn [joint]
rlm@78 354 (let [[obj-a obj-b]
rlm@78 355 (joint-targets pieces joint)]
rlm@87 356 (connect* obj-a obj-b joint)))
rlm@78 357 joints))
rlm@78 358 pieces)
rlm@74 359
rlm@78 360 (defn blender-creature [blender-path]
rlm@78 361 (let [model (load-blender-model blender-path)
rlm@78 362 joints
rlm@78 363 (if-let [joint-node (.getChild model "joints")]
rlm@78 364 (seq (.getChildren joint-node))
rlm@78 365 (do (println-repl "could not find joints node")
rlm@78 366 []))]
rlm@78 367 (assemble-creature model joints)))
rlm@74 368
rlm@78 369 (def hand "Models/creature1/one.blend")
rlm@74 370
rlm@78 371 (def worm "Models/creature1/try-again.blend")
rlm@78 372
rlm@80 373 (defn x-ray [#^ColorRGBA color]
rlm@80 374 (doto (Material. (asset-manager)
rlm@80 375 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 376 (.setColor "Color" color)
rlm@80 377 (-> (.getAdditionalRenderState)
rlm@80 378 (.setDepthTest false))))
rlm@80 379
rlm@78 380 (defn test-creature [thing]
rlm@80 381 (let [x-axis
rlm@80 382 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@80 383 y-axis
rlm@80 384 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@80 385 z-axis
rlm@80 386 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
rlm@78 387 (world
rlm@78 388 (nodify [(blender-creature thing)
rlm@81 389 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@80 390 :color ColorRGBA/Gray :mass 0)
rlm@80 391 x-axis y-axis z-axis
rlm@80 392 ])
rlm@78 393 standard-debug-controls
rlm@78 394 (comp light-up-everything enable-debug
rlm@78 395 (fn [world]
rlm@78 396 (.setTimer world (NanoTimer.))
rlm@87 397 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@78 398 (speed-up world)
rlm@78 399 world
rlm@78 400 ))
rlm@80 401 no-op)))
rlm@78 402
rlm@78 403 (defn world-setup [joint]
rlm@83 404 (let [
rlm@83 405
rlm@84 406 joint-position (Vector3f. 0 0 0)
rlm@83 407 joint-rotation
rlm@83 408 (.toRotationMatrix
rlm@83 409 (.mult
rlm@83 410 (doto (Quaternion.)
rlm@83 411 (.fromAngleAxis
rlm@83 412 (* 1 (/ Math/PI 4))
rlm@85 413 (Vector3f. -1 0 0)))
rlm@85 414 (doto (Quaternion.)
rlm@85 415 (.fromAngleAxis
rlm@85 416 (* 1 (/ Math/PI 2))
rlm@85 417 (Vector3f. 0 0 1)))))
rlm@84 418 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@83 419
rlm@83 420 origin (doto
rlm@83 421 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@84 422 :position top-position))
rlm@83 423 top (doto
rlm@78 424 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@84 425 :position top-position)
rlm@83 426
rlm@78 427 (.addControl
rlm@78 428 (RigidBodyControl.
rlm@83 429 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@78 430 bottom (doto
rlm@78 431 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@83 432 :position (Vector3f. 0 0 0))
rlm@83 433 (.addControl
rlm@78 434 (RigidBodyControl.
rlm@78 435 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@83 436 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@78 437 :color ColorRGBA/Gray :mass 0)
rlm@78 438 a (.getControl top RigidBodyControl)
rlm@78 439 b (.getControl bottom RigidBodyControl)]
rlm@83 440
rlm@78 441 (cond
rlm@83 442 (= joint :cone)
rlm@83 443
rlm@83 444 (doto (ConeJoint.
rlm@83 445 a b
rlm@87 446 (world-to-local top joint-position)
rlm@87 447 (world-to-local bottom joint-position)
rlm@83 448 joint-rotation
rlm@83 449 joint-rotation
rlm@83 450 )
rlm@78 451
rlm@83 452
rlm@83 453 (.setLimit (* (/ 10) Math/PI)
rlm@83 454 (* (/ 4) Math/PI)
rlm@83 455 0)))
rlm@83 456 [origin top bottom table]))
rlm@78 457
rlm@78 458
rlm@78 459
rlm@78 460 (defn test-joint [joint]
rlm@83 461 (let [[origin top bottom floor] (world-setup joint)
rlm@78 462 control (.getControl top RigidBodyControl)
rlm@78 463 move-up? (atom false)
rlm@78 464 move-down? (atom false)
rlm@78 465 move-left? (atom false)
rlm@78 466 move-right? (atom false)
rlm@78 467 roll-left? (atom false)
rlm@78 468 roll-right? (atom false)
rlm@78 469 timer (atom 0)]
rlm@78 470
rlm@78 471 (world
rlm@83 472 (nodify [top bottom floor origin])
rlm@78 473 (merge standard-debug-controls
rlm@78 474 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@78 475 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@78 476 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@78 477 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@78 478 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@78 479 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@78 480
rlm@78 481 (fn [world]
rlm@78 482 (light-up-everything world)
rlm@78 483 (enable-debug world)
rlm@78 484 (set-gravity world (Vector3f. 0 0 0))
rlm@78 485 )
rlm@78 486
rlm@78 487 (fn [world _]
rlm@78 488 (if (zero? (rem (swap! timer inc) 100))
rlm@78 489 (do
rlm@78 490 ;; (println-repl @timer)
rlm@78 491 (.attachChild (.getRootNode world)
rlm@78 492 (sphere 0.05 :color ColorRGBA/Yellow
rlm@78 493 :position (.getWorldTranslation top)
rlm@83 494 :physical? false))
rlm@83 495 (.attachChild (.getRootNode world)
rlm@83 496 (sphere 0.05 :color ColorRGBA/LightGray
rlm@83 497 :position (.getWorldTranslation bottom)
rlm@83 498 :physical? false))))
rlm@83 499
rlm@78 500 (if @move-up?
rlm@78 501 (.applyTorque control
rlm@78 502 (.mult (.getPhysicsRotation control)
rlm@78 503 (Vector3f. 0 0 10))))
rlm@78 504 (if @move-down?
rlm@78 505 (.applyTorque control
rlm@78 506 (.mult (.getPhysicsRotation control)
rlm@78 507 (Vector3f. 0 0 -10))))
rlm@78 508 (if @move-left?
rlm@78 509 (.applyTorque control
rlm@78 510 (.mult (.getPhysicsRotation control)
rlm@78 511 (Vector3f. 0 10 0))))
rlm@78 512 (if @move-right?
rlm@78 513 (.applyTorque control
rlm@78 514 (.mult (.getPhysicsRotation control)
rlm@78 515 (Vector3f. 0 -10 0))))
rlm@78 516 (if @roll-left?
rlm@78 517 (.applyTorque control
rlm@78 518 (.mult (.getPhysicsRotation control)
rlm@78 519 (Vector3f. -1 0 0))))
rlm@78 520 (if @roll-right?
rlm@78 521 (.applyTorque control
rlm@78 522 (.mult (.getPhysicsRotation control)
rlm@78 523 (Vector3f. 1 0 0))))))))
rlm@83 524
rlm@83 525
rlm@83 526
rlm@87 527 #+end_src
rlm@83 528
rlm@87 529 #+results: body-1
rlm@87 530 : #'cortex.silly/test-joint
rlm@78 531
rlm@78 532
rlm@78 533 * COMMENT purgatory
rlm@78 534 #+begin_src clojure
rlm@77 535 (defn bullet-trans []
rlm@77 536 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 537 :position (Vector3f. -10 5 0))
rlm@77 538 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 539 :position (Vector3f. -10 -5 0)
rlm@77 540 :mass 0)
rlm@77 541 control-a (.getControl obj-a RigidBodyControl)
rlm@77 542 control-b (.getControl obj-b RigidBodyControl)
rlm@77 543 swivel
rlm@77 544 (.toRotationMatrix
rlm@77 545 (doto (Quaternion.)
rlm@77 546 (.fromAngleAxis (/ Math/PI 2)
rlm@77 547 Vector3f/UNIT_X)))]
rlm@77 548 (doto
rlm@77 549 (ConeJoint.
rlm@77 550 control-a control-b
rlm@77 551 (Vector3f. 0 5 0)
rlm@77 552 (Vector3f. 0 -5 0)
rlm@77 553 swivel swivel)
rlm@77 554 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 555 (/ Math/PI 4)
rlm@77 556 (* Math/PI 0.8)))
rlm@77 557 (world (nodify
rlm@77 558 [obj-a obj-b])
rlm@77 559 standard-debug-controls
rlm@77 560 enable-debug
rlm@77 561 no-op)))
rlm@74 562
rlm@74 563
rlm@77 564 (defn bullet-trans* []
rlm@77 565 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 566 :position (Vector3f. 5 0 0)
rlm@77 567 :mass 90)
rlm@77 568 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 569 :position (Vector3f. -5 0 0)
rlm@77 570 :mass 0)
rlm@77 571 control-a (.getControl obj-a RigidBodyControl)
rlm@77 572 control-b (.getControl obj-b RigidBodyControl)
rlm@77 573 move-up? (atom nil)
rlm@77 574 move-down? (atom nil)
rlm@77 575 move-left? (atom nil)
rlm@77 576 move-right? (atom nil)
rlm@77 577 roll-left? (atom nil)
rlm@77 578 roll-right? (atom nil)
rlm@77 579 force 100
rlm@77 580 swivel
rlm@77 581 (.toRotationMatrix
rlm@77 582 (doto (Quaternion.)
rlm@77 583 (.fromAngleAxis (/ Math/PI 2)
rlm@77 584 Vector3f/UNIT_X)))
rlm@77 585 x-move
rlm@77 586 (doto (Matrix3f.)
rlm@77 587 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 588 (.normalize (Vector3f. 1 1 0))))
rlm@77 589
rlm@77 590 timer (atom 0)]
rlm@77 591 (doto
rlm@77 592 (ConeJoint.
rlm@77 593 control-a control-b
rlm@77 594 (Vector3f. -8 0 0)
rlm@77 595 (Vector3f. 2 0 0)
rlm@77 596 ;;swivel swivel
rlm@77 597 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 598 x-move Matrix3f/IDENTITY
rlm@77 599 )
rlm@77 600 (.setCollisionBetweenLinkedBodys false)
rlm@77 601 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 602 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 603 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 604 (world (nodify
rlm@77 605 [obj-a obj-b])
rlm@77 606 (merge standard-debug-controls
rlm@77 607 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 608 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 609 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 610 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 611 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 612 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 613
rlm@77 614 (fn [world]
rlm@77 615 (enable-debug world)
rlm@77 616 (set-gravity world Vector3f/ZERO)
rlm@77 617 )
rlm@77 618
rlm@77 619 (fn [world _]
rlm@77 620
rlm@77 621 (if @move-up?
rlm@77 622 (.applyForce control-a
rlm@77 623 (Vector3f. force 0 0)
rlm@77 624 (Vector3f. 0 0 0)))
rlm@77 625 (if @move-down?
rlm@77 626 (.applyForce control-a
rlm@77 627 (Vector3f. (- force) 0 0)
rlm@77 628 (Vector3f. 0 0 0)))
rlm@77 629 (if @move-left?
rlm@77 630 (.applyForce control-a
rlm@77 631 (Vector3f. 0 force 0)
rlm@77 632 (Vector3f. 0 0 0)))
rlm@77 633 (if @move-right?
rlm@77 634 (.applyForce control-a
rlm@77 635 (Vector3f. 0 (- force) 0)
rlm@77 636 (Vector3f. 0 0 0)))
rlm@77 637
rlm@77 638 (if @roll-left?
rlm@77 639 (.applyForce control-a
rlm@77 640 (Vector3f. 0 0 force)
rlm@77 641 (Vector3f. 0 0 0)))
rlm@77 642 (if @roll-right?
rlm@77 643 (.applyForce control-a
rlm@77 644 (Vector3f. 0 0 (- force))
rlm@77 645 (Vector3f. 0 0 0)))
rlm@77 646
rlm@77 647 (if (zero? (rem (swap! timer inc) 100))
rlm@77 648 (.attachChild
rlm@77 649 (.getRootNode world)
rlm@77 650 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 651 :physical? false :position
rlm@77 652 (.getWorldTranslation obj-a)))))
rlm@77 653 )
rlm@77 654 ))
rlm@77 655
rlm@77 656
rlm@77 657
rlm@73 658 #+end_src
rlm@73 659
rlm@73 660
rlm@73 661 * COMMENT generate source
rlm@73 662 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@73 663 <<body-1>>
rlm@73 664 #+end_src
rlm@73 665