Remove multimethod use from the physics libraries
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
(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
|
||||
@@ -53,8 +52,8 @@
|
||||
`(let [^Body object# (u/get-obj ~entity :body)]
|
||||
(u/call! object# ~k ~@options)))
|
||||
|
||||
(defmethod p/add-body!
|
||||
World
|
||||
(defn add-body!
|
||||
"Adds the `body` to the `screen` for physics simulations and returns it."
|
||||
[screen b-def]
|
||||
(box-2d! screen :create-body b-def))
|
||||
|
||||
@@ -70,38 +69,40 @@
|
||||
[entity]
|
||||
(.getRotation ^Transform (body! entity :get-transform)))
|
||||
|
||||
(defmethod p/body-position!
|
||||
Body
|
||||
(defn body-position!
|
||||
"Changes the position of the body in `entity`."
|
||||
[entity x y angle]
|
||||
(body! entity :set-transform x y angle))
|
||||
|
||||
(defmethod p/body-x!
|
||||
Body
|
||||
(defn body-x!
|
||||
"Changes the `x` of the body in `entity`."
|
||||
[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!
|
||||
Body
|
||||
(defn body-y!
|
||||
"Changes the `y` of the body in `entity`."
|
||||
[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!
|
||||
Body
|
||||
(defn body-angle!
|
||||
"Changes the `angle` of the body in `entity`."
|
||||
[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
|
||||
[body entities]
|
||||
(some #(if (= body (u/get-obj % :body)) %) entities))
|
||||
|
||||
(defmethod p/first-entity
|
||||
World
|
||||
(defn first-entity
|
||||
"Returns the first entity in a contact. May only be used in contact functions
|
||||
such as :on-begin-contact."
|
||||
[screen entities]
|
||||
(let [^Contact contact (u/get-obj screen :contact)]
|
||||
(-> contact .getFixtureA .getBody (find-body entities))))
|
||||
|
||||
(defmethod p/second-entity
|
||||
World
|
||||
(defn second-entity
|
||||
"Returns the second entity in a contact. May only be used in contact functions
|
||||
such as :on-begin-contact."
|
||||
[screen entities]
|
||||
(let [^Contact contact (u/get-obj screen :contact)]
|
||||
(-> contact .getFixtureB .getBody (find-body entities))))
|
||||
@@ -125,8 +126,8 @@
|
||||
[object k & options]
|
||||
`(u/call! ^Joint ~object ~k ~@options))
|
||||
|
||||
(defmethod p/add-joint!
|
||||
World
|
||||
(defn add-joint!
|
||||
"Adds the `joint` to the `screen` for physics simulations and returns it."
|
||||
[screen j-def]
|
||||
(box-2d! screen :create-joint j-def))
|
||||
|
||||
@@ -241,8 +242,9 @@
|
||||
(not (some #(= (.getBodyB joint) (:body %)) entities)))
|
||||
(.destroyJoint world joint))))))
|
||||
|
||||
(defmethod p/step!
|
||||
World
|
||||
(defn step!
|
||||
"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]
|
||||
:or {time-step (/ 1 60) velocity-iterations 10 position-iterations 10}
|
||||
:as screen}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
(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]
|
||||
@@ -125,8 +124,8 @@
|
||||
[]
|
||||
(btSoftBodyWorldInfo.))
|
||||
|
||||
(defmethod p/add-body!
|
||||
World3D
|
||||
(defn add-body!
|
||||
"Adds the `body` to the `screen` for physics simulations and returns it."
|
||||
[screen body]
|
||||
(cond
|
||||
(isa? (type (:object body)) btRigidBody)
|
||||
@@ -150,41 +149,43 @@
|
||||
(let [^btCollisionObject object (:object (u/get-obj entity :body))]
|
||||
(-> object .getWorldTransform (. val) (aget Matrix4/M23))))
|
||||
|
||||
(defmethod p/body-position!
|
||||
Body3D
|
||||
(defn body-position!
|
||||
"Changes the position of the body in `entity`."
|
||||
[entity x y z]
|
||||
(let [^btCollisionObject object (:object (u/get-obj entity :body))]
|
||||
(.setWorldTransform object
|
||||
(doto (m/matrix-4*)
|
||||
(m/matrix-4! :set-translation x y z)))))
|
||||
|
||||
(defmethod p/body-x!
|
||||
Body3D
|
||||
(defn body-x!
|
||||
"Changes the `x` of the body in `entity`."
|
||||
[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!
|
||||
Body3D
|
||||
(defn body-y!
|
||||
"Changes the `y` of the body in `entity`."
|
||||
[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!
|
||||
Body3D
|
||||
(defn body-z!
|
||||
"Changes the `z` of the body in `entity`."
|
||||
[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
|
||||
[body entities]
|
||||
(some #(if (= body (:object (u/get-obj % :body))) %) entities))
|
||||
|
||||
(defmethod p/first-entity
|
||||
World3D
|
||||
(defn first-entity
|
||||
"Returns the first entity in a contact. May only be used in contact functions
|
||||
such as :on-begin-contact."
|
||||
[screen entities]
|
||||
(-> (u/get-obj screen :first-body)
|
||||
(find-body entities)))
|
||||
|
||||
(defmethod p/second-entity
|
||||
World3D
|
||||
(defn second-entity
|
||||
"Returns the second entity in a contact. May only be used in contact functions
|
||||
such as :on-begin-contact."
|
||||
[screen entities]
|
||||
(-> (u/get-obj screen :second-body)
|
||||
(find-body entities)))
|
||||
@@ -305,8 +306,9 @@
|
||||
(bullet-3d! screen :remove-soft-body body))
|
||||
(.dispose body)))))
|
||||
|
||||
(defmethod p/step!
|
||||
World3D
|
||||
(defn step!
|
||||
"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]
|
||||
:or {max-sub-steps 5 time-step (/ 1 60)}
|
||||
:as screen}
|
||||
|
||||
@@ -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)))
|
||||
Reference in New Issue
Block a user