collision v0.3.1 Collision.Detection.SeparatingAxis

Implements the separating axis theorem for collision detection.

Checks for collision by projecting all of the edges of a pair of polygons against test axes that are the normals of their edges.

If there is any axis for which the projections aren’t overlapping, then the polygons are not colliding with one another. If all of the axes have overlapping projections, the polygons are colliding.

Summary

Functions

Check for collision between two polygons

Checks for collision between two polygons and, if colliding, calculates the minimum translation vector to move out of collision. The float in the return is the magnitude of overlap?

Types

polygon :: Collision.Polygon.Polygon.t

Functions

collision?(polygon_1, polygon_2)

Specs

collision?(polygon, polygon) :: boolean

Check for collision between two polygons.

Returns: true | false

Examples

iex> p1 = Polygon.gen_regular_polygon(4, 4, 0, {0, 0})
iex> p2 = Polygon.gen_regular_polygon(4, 6, 0, {2, 2})
iex> SeparatingAxis.collision?(p1, p2)
true

iex> p1 = Polygon.gen_regular_polygon(3, 1, 0, {-5, 8})
iex> p2 = Polygon.gen_regular_polygon(4, 6, 0, {2, 2})
iex> SeparatingAxis.collision?(p1, p2)
false
collision_mtv(polygon_1, polygon_2)

Specs

collision_mtv(polygon, polygon) :: {Collision.Vector.Vector2.t, number}

Checks for collision between two polygons and, if colliding, calculates the minimum translation vector to move out of collision. The float in the return is the magnitude of overlap?.

Returns: nil | {Vector2.t, float}

Examples

iex> p1 = Polygon.gen_regular_polygon(4, 4, 45, {0, 0})
iex> p2 = Polygon.gen_regular_polygon(4, 4, 45, {4, 0})
iex> {mtv, magnitude} = SeparatingAxis.collision_mtv(p1, p2)
iex> Vector.round_components(mtv, 2)
%Collision.Vector.Vector2{x: -1.0, y: 0.0}
iex> Float.round(magnitude, 2)
1.66

iex> p1 = Polygon.gen_regular_polygon(3, 1, 0, {-5, 8})
iex> p2 = Polygon.gen_regular_polygon(4, 6, 0, {2, 2})
iex> SeparatingAxis.collision_mtv(p1, p2)
nil