Skip to content

IdahoEv/collidex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Collidex

Build Status Coverage Status Hex.pm

Collidex is an in-development 2D geometry collision detection library in pure Elixir. It supports circles, grid-aligned rectangles, and convex polygons. When comparing two shapes, it will prefer the fastest algorithm that can test both.

For testing arbitrary convex polygons, Collidex has two available algorithms. A fully accurate collision detector will use the Separating Axis Theorem to check if the polygons collide. An additional fast checker is available that will detect most collisions and separations. In most usages it has significantly better performance but at the cost of a few false positives in some cases of near misses between acute polygons.

Usage

Points

Cartesian points are 2-tuples of the form {x, y}. I picked this format for speed and for compatibility with certrel/graphmath.

Geometric primitives

Circles, Rects (grid-aligned rectangles), and Polygons are Elixir structs. Each module has a make() function for creating them in common formats.

Circles

Circles can be made from three arguments (center_x, center_y, radius), or a 3-tuple specifying the same values.

iex> Collidex.Geometry.Circle.make(0.0, 0.0, 1.0)
%Collidex.Geometry.Circle{center: {0.0, 0.0}, radius: 1.0}

iex> Collidex.Geometry.Circle.make(1.0, 1.0, 1.0)
%Collidex.Geometry.Circle{center: {1.0, 1.0}, radius: 1.0}

Rects

Rects are grid-aligned rectangles delineated by two opposite corners. Rect.make takes a pair of vectors:

iex(1)> Collidex.Geometry.Rect.make({0.0, 0.0}, {1.0, 1.0})
%Collidex.Geometry.Rect{a: {0.0, 0.0}, b: {1.0, 1.0}}

For convenience, you may omit the braces and just specify four numbers:

iex(2)> Collidex.Geometry.Rect.make(0.0, 0.0, 1.0, 1.0)
%Collidex.Geometry.Rect{a: {0.0, 0.0}, b: {1.0, 1.0}}

Polygons

Specify polygons as an array of vectors:

iex(3)> Collidex.Geometry.Polygon.make([{0.0, 0.0}, {1.0, 1.0}, {2.0, 0.0}, {1.0, -1.0}])
%Collidex.Geometry.Polygon{vertices: [{0.0, 0.0}, {1.0, 1.0}, {2.0, 0.0},
  {1.0, -1.0}]}

Detecting Collisions Between primitives

Pass any pair of geometric shapes, as represented by Collidex structs, to Collidex.Detector.collision?/3. A truthy return value indicates that they overlap on the plane. The third argument is an optional atom representing the detection method for Polygon-to-Polygon collisions and defaults to :accurate. For faster but less accurate comparisons, specify :fast. See details in the docs for Collidex.Detector.Polygons.

Detecting collisions between and within lists of primitives

Checking for collisions between lists of geometric shapes:

Collidex.ListDetector.find_collisions(list_1, list_2, method \\ :accurate)

Will return a list of 3-tuples of the form { shape_1, shape_2, _ } representing pairs of shapes that intersect on the plane.

For collisions between elements of a single list, use 'find_collisions_within/2':

Collidex.ListDetector.find_collisions_within(list, method \\ :accurate)

Installation

If available in Hex, the package can be installed as:

Add collidex to your list of dependencies in mix.exs:

```elixir
def deps do
  [{:collidex, "~> 0.1.1"}]
end
```

License

MIT License, see LICENSE file.

About

2-D shape collision detection library in Elixir

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published