Advertisement
Guest User

Untitled

a guest
May 26th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (use Double)
  2. (use IO)
  3. (load "Vector.carp")
  4. (use Vector2)
  5.  
  6. (defmodule AABB2
  7.  
  8.   (deftype Box
  9.       [center V2
  10.        hwidth Double
  11.        hheight Double])
  12.  
  13.   (defn get-center
  14.     [b]
  15.     @(Box.center b))
  16.  
  17.   (defn get-hwidth
  18.     [b]
  19.     @(Box.hwidth b))
  20.  
  21.   (defn get-hheight
  22.     [b]
  23.     @(Box.hheight b))
  24.  
  25.   (defn set-center
  26.     [b v]
  27.     (Box.set-center b v))
  28.  
  29.   (defn set-hwidth
  30.     [b v]
  31.     (Box.set-hwidth b v))
  32.  
  33.   (defn set-hheight
  34.     [b v]
  35.     (Box.set-hheight b v))
  36.  
  37.   (defn get-x
  38.     [b]
  39.     @(V2.x (Box.center b))) ;; PROBLEM OCCURS WHEN USING (get-x instead seems to think im trying to us a recursive function call
  40.  
  41.   (defn get-y
  42.     [b]
  43.     @(V2.y (Box.center b)))
  44.  
  45.   (defn x-min
  46.     [b]
  47.     (- (get-x (Box.center b)) @(Box.hwidth b)))
  48.  
  49.   (defn y-min
  50.     [b]
  51.     (- (get-y (Box.center b)) @(Box.hheight b)))
  52.  
  53.   (defn x-max
  54.     [b]
  55.     (+ (get-x (Box.center b)) @(Box.hwidth b)))
  56.  
  57.   (defn y-max
  58.     [b]
  59.     (+ (get-y (Box.center b)) @(Box.hheight b)))
  60.  
  61.   (defn to-string
  62.     [b]
  63.     (string-join @"(Box "
  64.                  (V2.str (Box.center b)) @", "
  65.                  (str @(Box.hwidth b)) @", "
  66.                  (str @(Box.hheight b)) @")"))
  67.  
  68.   (defn init
  69.     [center hwidth hheight]
  70.     (Box.init center hwidth hheight))
  71.  
  72.   (defn unit
  73.     []
  74.     (Box.init (V2.init 0.0 0.0) 0.5 0.5))
  75.  
  76.   (defn area
  77.     [b]
  78.     (* (* @(Box.hwidth b) 2.0) (* @(Box.hheight b) 2.0)))
  79.  
  80.   (defn corners
  81.     [b]
  82.     (let [center (Box.center b)
  83.           hwidth @(Box.hwidth b)
  84.           hheight @(Box.hheight b)
  85.           tl (V2.init (- (get-x center) hwidth) (+ (get-y center) hheight))
  86.           tr (V2.init (+ (get-x center) hwidth) (+ (get-y center) hheight))
  87.           bl (V2.init (- (get-x center) hwidth) (- (get-y center) hheight))
  88.           br (V2.init (+ (get-x center) hwidth) (- (get-y center) hheight))]
  89.       [tl tr bl br]))
  90.   )
  91.  
  92. (defmodule Circle2
  93.  
  94.   (deftype Circle
  95.       [center V2
  96.        radius Double])
  97.  
  98.   (defn get-center
  99.     [c]
  100.     @(Circle.center c))
  101.  
  102.   (defn get-radius
  103.     [c]
  104.     @(Circle.radius c))
  105.  
  106.   (defn set-center
  107.     [c v]
  108.     (Circle.set-center c v))
  109.  
  110.   (defn set-radius
  111.     [c v]
  112.     (Circle.set-radius c v))
  113.  
  114.   (defn get-x
  115.     [b]
  116.     @(Vector2.V2.x (Circle.center b))) ;; PROBLEM OCCURS WHEN USING (get-x instead thinks im trying to do a recursive function call
  117.  
  118.   (defn get-y
  119.     [b]
  120.     @(Vector2.V2.y (Circle.center b)))
  121.  
  122.   (defn init
  123.     [center radius]
  124.     (Circle.init center radius))
  125.  
  126.   (defn unit
  127.     []
  128.     (Circle.init (Vector2.V2.init 0.0 0.0) 0.5))
  129.  
  130.   (defn to-string
  131.     [c]
  132.     (string-join @"(Circle "
  133.                  (Vector2.V2.str (Circle.center c)) @", "
  134.                  (str @(Circle.radius c)) @")"))
  135.  
  136.   (defn area
  137.     [c]
  138.     (* pi (pow @(Circle.radius c) 2.0)))
  139.   )
  140.  
  141. (use AABB2)
  142. (use Circle2)
  143.  
  144. (defmodule Collision
  145.  
  146.   (defn aabb-aabb-overlap?
  147.     [b1 b2]
  148.     (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?
  149.           combined-hheight (+ @(Box.hheight b1) @(Box.hheight b2))]
  150.       (and* (< (get-x b1) (+ (get-x b2)
  151.                              combined-hwidth))
  152.             (< (get-x b2) (+ (get-x b1)
  153.                              combined-hwidth))
  154.             (< (get-y b1) (+ (get-y b2)
  155.                              combined-hheight))
  156.             (< (get-y b2) (+ (get-y b1)
  157.                              combined-hheight)))))
  158.  
  159.   (defn aabb-aabb-mtv
  160.     [b1 b2]
  161.     (let [x-dist (if (< (get-x b1) (get-x b2))
  162.                    (* -1.0 (abs (- (x-max b1) (x-min b2))))
  163.                    (abs (- (x-max b2) (x-min b1))))
  164.           y-dist (if (< (get-y b1) (get-y b2))
  165.                    (* -1.0 (abs (- (y-max b1) (y-min b2))))
  166.                    (abs (- (y-max b2) (y-min b1))))]
  167.       (if (< x-dist y-dist)
  168.         (Vector2.V2.init x-dist 0.0)
  169.         (Vector2.V2.init 0.0 y-dist))))
  170.  
  171.   (defn circle-circle-overlap?
  172.     [c1 c2]
  173.     (let [x-diff (- (get-x c1) (get-x c2))
  174.           y-diff (- (get-y c1) (get-y c2))]))
  175.   )
  176.  
  177. (defn main
  178.   []
  179.   (let [b1 &(AABB2.init (Vector2.V2.init 0.0 0.0)
  180.                         1.0
  181.                         1.0)
  182.         b2 &(AABB2.init (Vector2.V2.init 1.9 2.0)
  183.                         1.0
  184.                         1.0)]
  185.     (do (println
  186.          &(str &(Collision.aabb-aabb-mtv b1 b2)))
  187.         0)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement