Remove multimethod use from the physics libraries

This commit is contained in:
oakes
2014-04-19 18:36:30 -04:00
parent 3bf79d72e4
commit ab1936c84b
3 changed files with 46 additions and 91 deletions

View File

@@ -1,7 +1,6 @@
(ns play-clj.g2d-physics (ns play-clj.g2d-physics
(:require [play-clj.core :as c] (:require [play-clj.core :as c]
[play-clj.math :as m] [play-clj.math :as m]
[play-clj.physics :as p]
[play-clj.utils :as u]) [play-clj.utils :as u])
(:import [com.badlogic.gdx.physics.box2d Body BodyDef ChainShape CircleShape (:import [com.badlogic.gdx.physics.box2d Body BodyDef ChainShape CircleShape
Contact ContactListener EdgeShape Fixture FixtureDef Joint JointDef Contact ContactListener EdgeShape Fixture FixtureDef Joint JointDef
@@ -53,8 +52,8 @@
`(let [^Body object# (u/get-obj ~entity :body)] `(let [^Body object# (u/get-obj ~entity :body)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defmethod p/add-body! (defn add-body!
World "Adds the `body` to the `screen` for physics simulations and returns it."
[screen b-def] [screen b-def]
(box-2d! screen :create-body b-def)) (box-2d! screen :create-body b-def))
@@ -70,38 +69,40 @@
[entity] [entity]
(.getRotation ^Transform (body! entity :get-transform))) (.getRotation ^Transform (body! entity :get-transform)))
(defmethod p/body-position! (defn body-position!
Body "Changes the position of the body in `entity`."
[entity x y angle] [entity x y angle]
(body! entity :set-transform x y angle)) (body! entity :set-transform x y angle))
(defmethod p/body-x! (defn body-x!
Body "Changes the `x` of the body in `entity`."
[entity x] [entity x]
(p/body-position! entity x (body-y entity) (body-angle entity))) (body-position! entity x (body-y entity) (body-angle entity)))
(defmethod p/body-y! (defn body-y!
Body "Changes the `y` of the body in `entity`."
[entity y] [entity y]
(p/body-position! entity (body-x entity) y (body-angle entity))) (body-position! entity (body-x entity) y (body-angle entity)))
(defmethod p/body-angle! (defn body-angle!
Body "Changes the `angle` of the body in `entity`."
[entity angle] [entity angle]
(p/body-position! entity (body-x entity) (body-y entity) angle)) (body-position! entity (body-x entity) (body-y entity) angle))
(defn ^:private find-body (defn ^:private find-body
[body entities] [body entities]
(some #(if (= body (u/get-obj % :body)) %) entities)) (some #(if (= body (u/get-obj % :body)) %) entities))
(defmethod p/first-entity (defn first-entity
World "Returns the first entity in a contact. May only be used in contact functions
such as :on-begin-contact."
[screen entities] [screen entities]
(let [^Contact contact (u/get-obj screen :contact)] (let [^Contact contact (u/get-obj screen :contact)]
(-> contact .getFixtureA .getBody (find-body entities)))) (-> contact .getFixtureA .getBody (find-body entities))))
(defmethod p/second-entity (defn second-entity
World "Returns the second entity in a contact. May only be used in contact functions
such as :on-begin-contact."
[screen entities] [screen entities]
(let [^Contact contact (u/get-obj screen :contact)] (let [^Contact contact (u/get-obj screen :contact)]
(-> contact .getFixtureB .getBody (find-body entities)))) (-> contact .getFixtureB .getBody (find-body entities))))
@@ -125,8 +126,8 @@
[object k & options] [object k & options]
`(u/call! ^Joint ~object ~k ~@options)) `(u/call! ^Joint ~object ~k ~@options))
(defmethod p/add-joint! (defn add-joint!
World "Adds the `joint` to the `screen` for physics simulations and returns it."
[screen j-def] [screen j-def]
(box-2d! screen :create-joint j-def)) (box-2d! screen :create-joint j-def))
@@ -241,8 +242,9 @@
(not (some #(= (.getBodyB joint) (:body %)) entities))) (not (some #(= (.getBodyB joint) (:body %)) entities)))
(.destroyJoint world joint)))))) (.destroyJoint world joint))))))
(defmethod p/step! (defn step!
World "Runs the physics simulations for a single frame and optionally returns the
`entities` with their positions updated."
[{:keys [^World world time-step velocity-iterations position-iterations] [{:keys [^World world time-step velocity-iterations position-iterations]
:or {time-step (/ 1 60) velocity-iterations 10 position-iterations 10} :or {time-step (/ 1 60) velocity-iterations 10 position-iterations 10}
:as screen} :as screen}

View File

@@ -1,7 +1,6 @@
(ns play-clj.g3d-physics (ns play-clj.g3d-physics
(:require [play-clj.core :as c] (:require [play-clj.core :as c]
[play-clj.math :as m] [play-clj.math :as m]
[play-clj.physics :as p]
[play-clj.utils :as u]) [play-clj.utils :as u])
(:import [com.badlogic.gdx.math Matrix4] (:import [com.badlogic.gdx.math Matrix4]
[com.badlogic.gdx.physics.bullet Bullet] [com.badlogic.gdx.physics.bullet Bullet]
@@ -125,8 +124,8 @@
[] []
(btSoftBodyWorldInfo.)) (btSoftBodyWorldInfo.))
(defmethod p/add-body! (defn add-body!
World3D "Adds the `body` to the `screen` for physics simulations and returns it."
[screen body] [screen body]
(cond (cond
(isa? (type (:object body)) btRigidBody) (isa? (type (:object body)) btRigidBody)
@@ -150,41 +149,43 @@
(let [^btCollisionObject object (:object (u/get-obj entity :body))] (let [^btCollisionObject object (:object (u/get-obj entity :body))]
(-> object .getWorldTransform (. val) (aget Matrix4/M23)))) (-> object .getWorldTransform (. val) (aget Matrix4/M23))))
(defmethod p/body-position! (defn body-position!
Body3D "Changes the position of the body in `entity`."
[entity x y z] [entity x y z]
(let [^btCollisionObject object (:object (u/get-obj entity :body))] (let [^btCollisionObject object (:object (u/get-obj entity :body))]
(.setWorldTransform object (.setWorldTransform object
(doto (m/matrix-4*) (doto (m/matrix-4*)
(m/matrix-4! :set-translation x y z))))) (m/matrix-4! :set-translation x y z)))))
(defmethod p/body-x! (defn body-x!
Body3D "Changes the `x` of the body in `entity`."
[entity x] [entity x]
(p/body-position! entity x (body-y entity) (body-z entity))) (body-position! entity x (body-y entity) (body-z entity)))
(defmethod p/body-y! (defn body-y!
Body3D "Changes the `y` of the body in `entity`."
[entity y] [entity y]
(p/body-position! entity (body-x entity) y (body-z entity))) (body-position! entity (body-x entity) y (body-z entity)))
(defmethod p/body-z! (defn body-z!
Body3D "Changes the `z` of the body in `entity`."
[entity z] [entity z]
(p/body-position! entity (body-x entity) (body-y entity) z)) (body-position! entity (body-x entity) (body-y entity) z))
(defn ^:private find-body (defn ^:private find-body
[body entities] [body entities]
(some #(if (= body (:object (u/get-obj % :body))) %) entities)) (some #(if (= body (:object (u/get-obj % :body))) %) entities))
(defmethod p/first-entity (defn first-entity
World3D "Returns the first entity in a contact. May only be used in contact functions
such as :on-begin-contact."
[screen entities] [screen entities]
(-> (u/get-obj screen :first-body) (-> (u/get-obj screen :first-body)
(find-body entities))) (find-body entities)))
(defmethod p/second-entity (defn second-entity
World3D "Returns the second entity in a contact. May only be used in contact functions
such as :on-begin-contact."
[screen entities] [screen entities]
(-> (u/get-obj screen :second-body) (-> (u/get-obj screen :second-body)
(find-body entities))) (find-body entities)))
@@ -305,8 +306,9 @@
(bullet-3d! screen :remove-soft-body body)) (bullet-3d! screen :remove-soft-body body))
(.dispose body))))) (.dispose body)))))
(defmethod p/step! (defn step!
World3D "Runs the physics simulations for a single frame and optionally returns the
`entities` with their positions updated."
[{:keys [delta-time max-sub-steps time-step] [{:keys [delta-time max-sub-steps time-step]
:or {max-sub-steps 5 time-step (/ 1 60)} :or {max-sub-steps 5 time-step (/ 1 60)}
:as screen} :as screen}

View File

@@ -1,49 +0,0 @@
(ns play-clj.physics
(:require [play-clj.utils :as u]))
(defmulti step!
"Runs the physics simulations for a single frame and optionally returns the
`entities` with their positions updated."
(fn [screen & [entities]] (-> screen (u/get-obj :world) class)))
(defmulti add-body!
"Adds the `body` to the `screen` for physics simulations and returns it. For
2D physics, `body` should be a `body-def`, whereas for 3D physics it should be a
`basic-body` or a `rigid-body`."
(fn [screen body] (-> screen (u/get-obj :world) class)))
(defmulti add-joint!
"Adds the `joint` to the `screen` for physics simulations and returns it. For
2D physics, `joint` should be a `joint-def`."
(fn [screen joint] (-> screen (u/get-obj :world) class)))
(defmulti body-position!
"Changes the position of the body in `entity`. For 2D physics, the arguments
should be x, y, and angle, whereas for 3D physics they should be x, y, and z."
(fn [entity a1 a2 a3] (-> entity (u/get-obj :body) class)))
(defmulti body-x!
"Changes the `x` of the body in `entity`."
(fn [entity x] (-> entity (u/get-obj :body) class)))
(defmulti body-y!
"Changes the `y` of the body in `entity`."
(fn [entity y] (-> entity (u/get-obj :body) class)))
(defmulti body-z!
"Changes the `z` of the body in `entity`. Only works with 3D physics."
(fn [entity z] (-> entity (u/get-obj :body) class)))
(defmulti body-angle!
"Changes the `angle` of the body in `entity`. Only works with 2D physics."
(fn [entity angle] (-> entity (u/get-obj :body) class)))
(defmulti first-entity
"Returns the first entity in a contact. May only be used in contact functions,
such as :on-begin-contact."
(fn [screen entities] (-> screen (u/get-obj :world) class)))
(defmulti second-entity
"Returns the second entity in a contact. May only be used in contact functions,
such as :on-begin-contact."
(fn [screen entities] (-> screen (u/get-obj :world) class)))