Neverending Perpetual Motion

# npm

Have ideas to improve npm?Join in the discussion! »

## separating-axis-test

1.1.0 • Public • Published

# separating-axis-test

Test for the intersection of 2 convex polytopes in 2d or 3d.

If an intersection exists, return the minimum translation vector.

(also called the separating axis theorem or the hyperplane separation theorem)

# example

The examples below use axis-aligned boxes, but oriented boxes or any other convex polytope will work too.

The examples below show nested coordinates but flat arrays work too.

## 2d example

2d example with two axis-aligned boxes:

# separating axes

To calculate the separating axes:

In 2d you can loop over each line segment and calculate the edge normal:

In 3d you can loop over each face and compute the face normal:

If separating axes are flipped versions of each other you can omit one to spare some calculations. For example if you have both `[1,0]` and `[-1,0]`, you can drop one of them.

# api

``````var sat2d = require('separating-axis-test/2d')
var sat3d = require('separating-axis-test/3d')
var {sat2d,sat3d} = require('separating-axis-test')
``````

## sat2d(out, A, B)

Calculate the intersection of convex polygons `A` and `B` in 2d, storing the minimum translation vector in `out` if the shapes intersect.

Each polygon `A` and `B` must have these fields:

• `positions` - an array of coordinates which describe the convex hull
• `separatingAxes` - an array of (normalized) axis normals to test

Coordinates and normals may be specified as nested (`[[x0,y0],[x1,y1],...]`) or flat (`[x0,y0,x1,y1,...]`).

If there is no intersection, this function returns null. Otherwise, it returns the minimum translation vector `out`.

## sat3d(out, A, B, epsilon=0.00001)

Calculate the intersection of convex polytopes `A` and `B` in 3d, storing the minimum translation vector in `out` if the shapes intersect.

Each polytope `A` and `B` must have these fields:

• `positions` - an array of coordinates which describe the convex hull
• `separatingAxes` - an array of (normalized) axis normals to test

Coordinates and normals may be specified as nested (`[[x0,y0,z0],[x1,y1,z1],...]`) or flat (`[x0,y0,z0,x1,y1,z1,...]`).

If there is no intersection, this function returns null. Otherwise, it returns the minimum translation vector `out`.

# install

``````npm install separating-axis-test
``````

bsd

## Keywords

### Install

`npm i separating-axis-test`

6

1.1.0

bsd

27 kB

16