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; package play_clj.g3d_physics;
import com.badlogic.gdx.physics.bullet.collision.btCollisionObject;
import clojure.lang.IFn; 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; IFn started, ended;
public ContactListener(IFn started, IFn ended) { public ContactListener3D(IFn started, IFn ended) {
this.started = started; this.started = started;
this.ended = ended; this.ended = ended;
} }

View File

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

View File

@@ -1,6 +1,7 @@
(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
@@ -52,7 +53,7 @@
`(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 c/add-body! (defmethod p/add-body!
World World
[screen b-def] [screen b-def]
(box-2d! screen :create-body b-def)) (box-2d! screen :create-body b-def))
@@ -69,37 +70,37 @@
[entity] [entity]
(.getRotation ^Transform (body! entity :get-transform))) (.getRotation ^Transform (body! entity :get-transform)))
(defmethod c/body-position! (defmethod p/body-position!
Body Body
[entity x y angle] [entity x y angle]
(body! entity :set-transform x y angle)) (body! entity :set-transform x y angle))
(defmethod c/body-x! (defmethod p/body-x!
Body Body
[entity x] [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 Body
[entity y] [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 Body
[entity angle] [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 (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 c/first-entity (defmethod p/first-entity
World World
[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 c/second-entity (defmethod p/second-entity
World World
[screen entities] [screen entities]
(let [^Contact contact (u/get-obj screen :contact)] (let [^Contact contact (u/get-obj screen :contact)]
@@ -124,7 +125,7 @@
[object k & options] [object k & options]
`(u/call! ^Joint ~object ~k ~@options)) `(u/call! ^Joint ~object ~k ~@options))
(defmethod c/add-joint! (defmethod p/add-joint!
World World
[screen j-def] [screen j-def]
(box-2d! screen :create-joint j-def)) (box-2d! screen :create-joint j-def))
@@ -240,7 +241,7 @@
(not (some #(= (.getBodyB joint) (:body %)) entities))) (not (some #(= (.getBodyB joint) (:body %)) entities)))
(.destroyJoint world joint)))))) (.destroyJoint world joint))))))
(defmethod c/step! (defmethod p/step!
World World
[{: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}

View File

@@ -1,6 +1,7 @@
(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]
@@ -15,7 +16,7 @@
[com.badlogic.gdx.physics.bullet.softbody btSoftBody [com.badlogic.gdx.physics.bullet.softbody btSoftBody
btSoftBodyRigidBodyCollisionConfiguration btSoftBodyWorldInfo btSoftBodyRigidBodyCollisionConfiguration btSoftBodyWorldInfo
btSoftRigidDynamicsWorld] btSoftRigidDynamicsWorld]
[play_clj.g3d_physics ContactListener])) [play_clj.g3d_physics ContactListener3D]))
(def ^:private init (delay (Bullet/init))) (def ^:private init (delay (Bullet/init)))
@@ -124,7 +125,7 @@
[] []
(btSoftBodyWorldInfo.)) (btSoftBodyWorldInfo.))
(defmethod c/add-body! (defmethod p/add-body!
World3D World3D
[screen body] [screen body]
(cond (cond
@@ -149,7 +150,7 @@
(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 c/body-position! (defmethod p/body-position!
Body3D Body3D
[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))]
@@ -157,32 +158,32 @@
(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 c/body-x! (defmethod p/body-x!
Body3D Body3D
[entity x] [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 Body3D
[entity y] [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 Body3D
[entity z] [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 (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 c/first-entity (defmethod p/first-entity
World3D World3D
[screen entities] [screen entities]
(-> (u/get-obj screen :first-body) (-> (u/get-obj screen :first-body)
(find-body entities))) (find-body entities)))
(defmethod c/second-entity (defmethod p/second-entity
World3D World3D
[screen entities] [screen entities]
(-> (u/get-obj screen :second-body) (-> (u/get-obj screen :second-body)
@@ -267,7 +268,7 @@
[screen [screen
{:keys [on-begin-contact on-end-contact]} {:keys [on-begin-contact on-end-contact]}
execute-fn!] execute-fn!]
(ContactListener. (ContactListener3D.
(fn [a b] (fn [a b]
(execute-fn! on-begin-contact :first-body a :second-body b)) (execute-fn! on-begin-contact :first-body a :second-body b))
(fn [a b] (fn [a b]
@@ -304,7 +305,7 @@
(bullet-3d! screen :remove-soft-body body)) (bullet-3d! screen :remove-soft-body body))
(.dispose body))))) (.dispose body)))))
(defmethod c/step! (defmethod p/step!
World3D World3D
[{: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)}

View File

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