a Physics Engine in Janet Part 2: Intersections
It's time for more physics! Today I'm going to go through 2 shapes, Circles and Rectangles (with rotation!), and the 3 types of intersections that entails.
- Circle vs Circle
- Rectangle vs Rectangle
- Circle vs Rectangle
Note that 99% of the logic here is based on this amazing physics tutorial on youtube. Thank you "Two-Bit Coding" for making an actual good tutorial with working code and logic I can understand 😅.
Shapes
blah blah blah
(defn shape/circle [radius]
{:type :circle
:radius radius
:area (* math/pi radius radius)
:get-vertices (fn [self] nil)
:draw (fn [self {:pos pos} col]
(tic80/circ ;(map math/round pos)
(self :radius)
col)
(tic80/circb ;(map math/round pos)
(self :radius)
7))})
(defn shape/rectangle [width height]
{:type :rectangle
:width width
:height height
:area (* width height)
:get-vertices (fn [self]
(let [left (/ (* -1 width) 2)
right (+ (/ (* -1 width) 2) width)
top (/ (* -1 height) 2)
bottom (+ (/ (* -1 height) 2) height)]
[(vec/init left top)
(vec/init right top)
(vec/init right bottom)
(vec/init left bottom)]))
:draw (fn [self body col]
(let [[a b c d] (:get-transformed-vertices body)]
(tic80/tri ;a ;b ;c col)
(tic80/tri ;a ;c ;d col)
(tic80/line ;a ;b 7)
(tic80/line ;b ;c 7)
(tic80/line ;c ;d 7)
(tic80/line ;d ;a 7)))})
Circle vs Circle
Rectangle vs Rectangle: the Seperate Axis Theorem
Rectangle vs Circle
Just like last time, heres the complete code!