diff --git a/src-java/play_clj/g3d_physics/ContactListener.java b/src-java/play_clj/g3d_physics/ContactListener3D.java similarity index 60% rename from src-java/play_clj/g3d_physics/ContactListener.java rename to src-java/play_clj/g3d_physics/ContactListener3D.java index 40877f2..b51a96c 100644 --- a/src-java/play_clj/g3d_physics/ContactListener.java +++ b/src-java/play_clj/g3d_physics/ContactListener3D.java @@ -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; } diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index 5272151..1021a01 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -36,7 +36,6 @@ (load "core_cameras") (load "core_graphics") (load "core_listeners") -(load "core_physics") (load "core_utils") (defn ^:private reset-changed! diff --git a/src/play_clj/g2d_physics.clj b/src/play_clj/g2d_physics.clj index b71ed3d..c0e9cbd 100644 --- a/src/play_clj/g2d_physics.clj +++ b/src/play_clj/g2d_physics.clj @@ -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} diff --git a/src/play_clj/g3d_physics.clj b/src/play_clj/g3d_physics.clj index e086d65..cdcd26e 100644 --- a/src/play_clj/g3d_physics.clj +++ b/src/play_clj/g3d_physics.clj @@ -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)} diff --git a/src/play_clj/core_physics.clj b/src/play_clj/physics.clj similarity index 96% rename from src/play_clj/core_physics.clj rename to src/play_clj/physics.clj index d00ee9d..9347505 100644 --- a/src/play_clj/core_physics.clj +++ b/src/play_clj/physics.clj @@ -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