A library for generating and working with polygons, particularly for detecting and resolving collisions between them.
Implements the separating axis theorem for polygon collision detection in 2D space.
Using regular polygons with an arbitrary number of sides and of an artbitrary size, can detect collisions and calculate the minimum translation vector to resolve collision.
Detailed documentation available at hexdocs
- Generate polygons
Polygon.from_vertices/1
- Create a polygon from a list of vertices, expected to be in counter-clockwise order. Polygons can be convex or concave. Currently, if a polygon is concave then a convex hull is used in collision checks.Polygon.gen_regular_polygon/4
- Create a regular[fn:1] polygon from a number of sides, radius, rotation angle, and midpoint.
- Find out information about the polygon
Polygon.convex?/1
- Determine whether a polygon is convexPolygon.centroid/1
- Find a polygon’s midpoint
- Manipulate a polygon
Polygon.translate/2
- Translate a polygon in Cartesian spacePolygon.rotate/3
- Rotate a polygon by a measure in radians around a point.Polygon.rotate_degrees/3
- As above but with degrees.
- Determine and handle collisions
Collidable.collision?/2
- Check for collision between two polygonsCollidable.resolution/2
- Get the minimum translation vector and magnitude to move two polygons out of collision.Collidable.resolve_collision/2
- Given two polygons, move them out of collision and returns the translated polygons.
- Work with polygon edges
Edge.from_vertex_pair/1
- Construct an edge from a pair of verticesEdge.calculate_length/1
- Find the length of an edgeEdge.calculate_angle/2
- Calculate the angle made by two edges meeting at a vertex.
- Work with vertices
Vertex.from_tuple/1
- Construct a vertex from a tuple of numbersVertex.to_tuple/1
- Revert a vertex to a tupleVertex.graham_scan/1
- Construct a convex hull that bounds a list of vertices describing a polygonVertex.round_vertices/1
- Round the components of a vertex
- [ ] Additional means of collision detection
- [ ] Detect impending collisions
- [ ] More accurately detect collisions when polygons are concave (via triangulation)
- [ ] 3D collision detection
- [ ] 3D objects via polygon meshes
- [ ] Optimization
Add collision
to your list of dependencies in mix.exs
:
def deps do
[{:collision, "~> 0.3.1"}]
end
[fn:1] A polygon with n equal sides and n equal angles.