Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Created February 15, 2021 12:55
Show Gist options
  • Save DarkWiiPlayer/253480c4e981153e3e8d59944dd15dc9 to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/253480c4e981153e3e8d59944dd15dc9 to your computer and use it in GitHub Desktop.
Thoughs on how to implement 2D collision handling

Polygon-Polygon collision

let A and B be polygons

let a and b be vectors representing the movement of said polygons within a game tick

let c be the movement of A relative to B a-b

calculate the transformation matrix Ta that transforms c into the vector (1, 0)

let A' and B' be T applied to A and B

for every point p in A' for every edge e = (e1, e2) in B' calculate the intersection of the segments (p, p+c) and e { subtract p from e and calculate whether e crosses the X-axis}

for every point p in B' for every edge e = (e1, e2) in A' calculate the intersection of the segments (p, p+c) and e { subtract p from e and calculate whether e crosses the X-axis}

return the shortest X coordinate among all intersections as the collision or no collision if there were no intersections

Ideally, both A' and B' should fit entirely in the cache when represented as arrays of coordinates as integers.

Quad-Tree Collision Grid?

Benefit: adapts to number of object and to clustering

Problem: Subdividing requires recalculation by the object

Possible solution: object registers movement box

Problem: Shrinking now needs to take into consideration movement box sizes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment