Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (use Double)
- (use IO)
- (load "Vector.carp")
- (use Vector2)
- (defmodule AABB2
- (deftype Box
- [center V2
- hwidth Double
- hheight Double])
- (defn get-center
- [b]
- @(Box.center b))
- (defn get-hwidth
- [b]
- @(Box.hwidth b))
- (defn get-hheight
- [b]
- @(Box.hheight b))
- (defn set-center
- [b v]
- (Box.set-center b v))
- (defn set-hwidth
- [b v]
- (Box.set-hwidth b v))
- (defn set-hheight
- [b v]
- (Box.set-hheight b v))
- (defn get-x
- [b]
- @(V2.x (Box.center b))) ;; PROBLEM OCCURS WHEN USING (get-x instead seems to think im trying to us a recursive function call
- (defn get-y
- [b]
- @(V2.y (Box.center b)))
- (defn x-min
- [b]
- (- (get-x (Box.center b)) @(Box.hwidth b)))
- (defn y-min
- [b]
- (- (get-y (Box.center b)) @(Box.hheight b)))
- (defn x-max
- [b]
- (+ (get-x (Box.center b)) @(Box.hwidth b)))
- (defn y-max
- [b]
- (+ (get-y (Box.center b)) @(Box.hheight b)))
- (defn to-string
- [b]
- (string-join @"(Box "
- (V2.str (Box.center b)) @", "
- (str @(Box.hwidth b)) @", "
- (str @(Box.hheight b)) @")"))
- (defn init
- [center hwidth hheight]
- (Box.init center hwidth hheight))
- (defn unit
- []
- (Box.init (V2.init 0.0 0.0) 0.5 0.5))
- (defn area
- [b]
- (* (* @(Box.hwidth b) 2.0) (* @(Box.hheight b) 2.0)))
- (defn corners
- [b]
- (let [center (Box.center b)
- hwidth @(Box.hwidth b)
- hheight @(Box.hheight b)
- tl (V2.init (- (get-x center) hwidth) (+ (get-y center) hheight))
- tr (V2.init (+ (get-x center) hwidth) (+ (get-y center) hheight))
- bl (V2.init (- (get-x center) hwidth) (- (get-y center) hheight))
- br (V2.init (+ (get-x center) hwidth) (- (get-y center) hheight))]
- [tl tr bl br]))
- )
- (defmodule Circle2
- (deftype Circle
- [center V2
- radius Double])
- (defn get-center
- [c]
- @(Circle.center c))
- (defn get-radius
- [c]
- @(Circle.radius c))
- (defn set-center
- [c v]
- (Circle.set-center c v))
- (defn set-radius
- [c v]
- (Circle.set-radius c v))
- (defn get-x
- [b]
- @(Vector2.V2.x (Circle.center b))) ;; PROBLEM OCCURS WHEN USING (get-x instead thinks im trying to do a recursive function call
- (defn get-y
- [b]
- @(Vector2.V2.y (Circle.center b)))
- (defn init
- [center radius]
- (Circle.init center radius))
- (defn unit
- []
- (Circle.init (Vector2.V2.init 0.0 0.0) 0.5))
- (defn to-string
- [c]
- (string-join @"(Circle "
- (Vector2.V2.str (Circle.center c)) @", "
- (str @(Circle.radius c)) @")"))
- (defn area
- [c]
- (* pi (pow @(Circle.radius c) 2.0)))
- )
- (use AABB2)
- (use Circle2)
- (defmodule Collision
- (defn aabb-aabb-overlap?
- [b1 b2]
- (let [combined-hwidth (+ @(Box.hwidth b1) @(Box.hwidth b2)) ;; THINK b1 etc... are V2 but with the definition of (get-x above by should be assumed to be a Box?
- combined-hheight (+ @(Box.hheight b1) @(Box.hheight b2))]
- (and* (< (get-x b1) (+ (get-x b2)
- combined-hwidth))
- (< (get-x b2) (+ (get-x b1)
- combined-hwidth))
- (< (get-y b1) (+ (get-y b2)
- combined-hheight))
- (< (get-y b2) (+ (get-y b1)
- combined-hheight)))))
- (defn aabb-aabb-mtv
- [b1 b2]
- (let [x-dist (if (< (get-x b1) (get-x b2))
- (* -1.0 (abs (- (x-max b1) (x-min b2))))
- (abs (- (x-max b2) (x-min b1))))
- y-dist (if (< (get-y b1) (get-y b2))
- (* -1.0 (abs (- (y-max b1) (y-min b2))))
- (abs (- (y-max b2) (y-min b1))))]
- (if (< x-dist y-dist)
- (Vector2.V2.init x-dist 0.0)
- (Vector2.V2.init 0.0 y-dist))))
- (defn circle-circle-overlap?
- [c1 c2]
- (let [x-diff (- (get-x c1) (get-x c2))
- y-diff (- (get-y c1) (get-y c2))]))
- )
- (defn main
- []
- (let [b1 &(AABB2.init (Vector2.V2.init 0.0 0.0)
- 1.0
- 1.0)
- b2 &(AABB2.init (Vector2.V2.init 1.9 2.0)
- 1.0
- 1.0)]
- (do (println
- &(str &(Collision.aabb-aabb-mtv b1 b2)))
- 0)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement