Automatically add box2d contact listener

This commit is contained in:
oakes
2014-01-17 14:17:47 -05:00
parent 7d9ceaa752
commit 6f9c8a4ebd
4 changed files with 53 additions and 36 deletions

View File

@@ -1,22 +1,34 @@
(ns play-clj.g2d-physics
(:require [play-clj.math :as m]
[play-clj.utils :as u])
(:import [com.badlogic.gdx.physics.box2d World]))
(:import [com.badlogic.gdx.physics.box2d ContactListener World]))
(defn box2d*
(defn box-2d*
([]
(box2d* 0 0 true))
(box-2d* 0 0 true))
([gravity-x gravity-y]
(box2d* gravity-x gravity-y true))
(box-2d* gravity-x gravity-y true))
([gravity-x gravity-y sleep?]
(World. (m/vector-2 gravity-x gravity-y) sleep?)))
(defmacro box2d
(defmacro box-2d
[gravity-x gravity-y & options]
`(let [object# (box2d* ~gravity-x ~gravity-y)]
`(let [object# (box-2d* ~gravity-x ~gravity-y)]
(u/calls! ^World object# ~@options)
object#))
(defmacro box2d!
(defmacro box-2d!
[{:keys [^World world]} k & options]
`(u/call! ^World ~world ~k ~@options))
(defn contact-listener
[{:keys [on-begin-contact on-end-contact on-post-solve on-pre-solve]} execute-fn!]
(reify ContactListener
(beginContact [this c]
(execute-fn! on-begin-contact :contact c))
(endContact [this c]
(execute-fn! on-end-contact :contact c))
(postSolve [this c i]
(execute-fn! on-post-solve :contact c :impulse i))
(preSolve [this c m]
(execute-fn! on-pre-solve :contact c :old-manifold m))))