Put common physics code into separate namespace

This commit is contained in:
oakes
2014-04-19 17:16:33 -04:00
parent fac14ab006
commit 3bf79d72e4
5 changed files with 32 additions and 30 deletions

View File

@@ -1,12 +1,12 @@
package play_clj.g3d_physics;
import com.badlogic.gdx.physics.bullet.collision.btCollisionObject;
import clojure.lang.IFn;
import com.badlogic.gdx.physics.bullet.collision.*;
public class ContactListener extends com.badlogic.gdx.physics.bullet.collision.ContactListener {
public class ContactListener3D extends ContactListener {
IFn started, ended;
public ContactListener(IFn started, IFn ended) {
public ContactListener3D(IFn started, IFn ended) {
this.started = started;
this.ended = ended;
}

View File

@@ -36,7 +36,6 @@
(load "core_cameras")
(load "core_graphics")
(load "core_listeners")
(load "core_physics")
(load "core_utils")
(defn ^:private reset-changed!

View File

@@ -1,6 +1,7 @@
(ns play-clj.g2d-physics
(:require [play-clj.core :as c]
[play-clj.math :as m]
[play-clj.physics :as p]
[play-clj.utils :as u])
(:import [com.badlogic.gdx.physics.box2d Body BodyDef ChainShape CircleShape
Contact ContactListener EdgeShape Fixture FixtureDef Joint JointDef
@@ -52,7 +53,7 @@
`(let [^Body object# (u/get-obj ~entity :body)]
(u/call! object# ~k ~@options)))
(defmethod c/add-body!
(defmethod p/add-body!
World
[screen b-def]
(box-2d! screen :create-body b-def))
@@ -69,37 +70,37 @@
[entity]
(.getRotation ^Transform (body! entity :get-transform)))
(defmethod c/body-position!
(defmethod p/body-position!
Body
[entity x y angle]
(body! entity :set-transform x y angle))
(defmethod c/body-x!
(defmethod p/body-x!
Body
[entity x]
(c/body-position! entity x (body-y entity) (body-angle entity)))
(p/body-position! entity x (body-y entity) (body-angle entity)))
(defmethod c/body-y!
(defmethod p/body-y!
Body
[entity y]
(c/body-position! entity (body-x entity) y (body-angle entity)))
(p/body-position! entity (body-x entity) y (body-angle entity)))
(defmethod c/body-angle!
(defmethod p/body-angle!
Body
[entity angle]
(c/body-position! entity (body-x entity) (body-y entity) angle))
(p/body-position! entity (body-x entity) (body-y entity) angle))
(defn ^:private find-body
[body entities]
(some #(if (= body (u/get-obj % :body)) %) entities))
(defmethod c/first-entity
(defmethod p/first-entity
World
[screen entities]
(let [^Contact contact (u/get-obj screen :contact)]
(-> contact .getFixtureA .getBody (find-body entities))))
(defmethod c/second-entity
(defmethod p/second-entity
World
[screen entities]
(let [^Contact contact (u/get-obj screen :contact)]
@@ -124,7 +125,7 @@
[object k & options]
`(u/call! ^Joint ~object ~k ~@options))
(defmethod c/add-joint!
(defmethod p/add-joint!
World
[screen j-def]
(box-2d! screen :create-joint j-def))
@@ -240,7 +241,7 @@
(not (some #(= (.getBodyB joint) (:body %)) entities)))
(.destroyJoint world joint))))))
(defmethod c/step!
(defmethod p/step!
World
[{:keys [^World world time-step velocity-iterations position-iterations]
:or {time-step (/ 1 60) velocity-iterations 10 position-iterations 10}

View File

@@ -1,6 +1,7 @@
(ns play-clj.g3d-physics
(:require [play-clj.core :as c]
[play-clj.math :as m]
[play-clj.physics :as p]
[play-clj.utils :as u])
(:import [com.badlogic.gdx.math Matrix4]
[com.badlogic.gdx.physics.bullet Bullet]
@@ -15,7 +16,7 @@
[com.badlogic.gdx.physics.bullet.softbody btSoftBody
btSoftBodyRigidBodyCollisionConfiguration btSoftBodyWorldInfo
btSoftRigidDynamicsWorld]
[play_clj.g3d_physics ContactListener]))
[play_clj.g3d_physics ContactListener3D]))
(def ^:private init (delay (Bullet/init)))
@@ -124,7 +125,7 @@
[]
(btSoftBodyWorldInfo.))
(defmethod c/add-body!
(defmethod p/add-body!
World3D
[screen body]
(cond
@@ -149,7 +150,7 @@
(let [^btCollisionObject object (:object (u/get-obj entity :body))]
(-> object .getWorldTransform (. val) (aget Matrix4/M23))))
(defmethod c/body-position!
(defmethod p/body-position!
Body3D
[entity x y z]
(let [^btCollisionObject object (:object (u/get-obj entity :body))]
@@ -157,32 +158,32 @@
(doto (m/matrix-4*)
(m/matrix-4! :set-translation x y z)))))
(defmethod c/body-x!
(defmethod p/body-x!
Body3D
[entity x]
(c/body-position! entity x (body-y entity) (body-z entity)))
(p/body-position! entity x (body-y entity) (body-z entity)))
(defmethod c/body-y!
(defmethod p/body-y!
Body3D
[entity y]
(c/body-position! entity (body-x entity) y (body-z entity)))
(p/body-position! entity (body-x entity) y (body-z entity)))
(defmethod c/body-z!
(defmethod p/body-z!
Body3D
[entity z]
(c/body-position! entity (body-x entity) (body-y entity) z))
(p/body-position! entity (body-x entity) (body-y entity) z))
(defn ^:private find-body
[body entities]
(some #(if (= body (:object (u/get-obj % :body))) %) entities))
(defmethod c/first-entity
(defmethod p/first-entity
World3D
[screen entities]
(-> (u/get-obj screen :first-body)
(find-body entities)))
(defmethod c/second-entity
(defmethod p/second-entity
World3D
[screen entities]
(-> (u/get-obj screen :second-body)
@@ -267,7 +268,7 @@
[screen
{:keys [on-begin-contact on-end-contact]}
execute-fn!]
(ContactListener.
(ContactListener3D.
(fn [a b]
(execute-fn! on-begin-contact :first-body a :second-body b))
(fn [a b]
@@ -304,7 +305,7 @@
(bullet-3d! screen :remove-soft-body body))
(.dispose body)))))
(defmethod c/step!
(defmethod p/step!
World3D
[{:keys [delta-time max-sub-steps time-step]
:or {max-sub-steps 5 time-step (/ 1 60)}

View File

@@ -1,4 +1,5 @@
(in-ns 'play-clj.core)
(ns play-clj.physics
(:require [play-clj.utils :as u]))
(defmulti step!
"Runs the physics simulations for a single frame and optionally returns the