Skip to content

Commit

Permalink
Exported geometry and fixed circular dependency
Browse files Browse the repository at this point in the history
- [FEATURE] Made the Vector, Box, Circle, and Polygon classes into their own individual exports instead of having to be created through the Collider2d class.
- [FEATURE] Removed the reference to Box in Polygon to clear up a circular dependency.
- [DOCS] Updated documentation to reflect new API.
- [TEST] Updated tests to match new API.
- [MISC] Removed unnecessary types.
- [MISC] Started renaming filse to use underscores to follow JavaScript guidelines.
- [MISC] Updated dev-dependencies to their latest versions and fixed all security vulnerabilities.
- [MISC] Updated npm scripts to remove existing files before building/bundling to avoid issues with old code.
- [MISC] Renamed lib directory to build to be consistent with other projects.
  • Loading branch information
robertcorponoi committed Sep 29, 2020
1 parent 2235da7 commit 2d9e565
Show file tree
Hide file tree
Showing 39 changed files with 4,581 additions and 4,828 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 1.0.0 / 2020-09-28
- [FEATURE] Made the Vector, Box, Circle, and Polygon classes into their own individual exports instead of having to be created through the Collider2d class.
- [FEATURE] Removed the reference to Box in Polygon to clear up a circular dependency.
- [DOCS] Updated documentation to reflect new API.
- [TEST] Updated tests to match new API.
- [MISC] Removed unnecessary types.
- [MISC] Started renaming filse to use underscores to follow JavaScript guidelines.
- [MISC] Updated dev-dependencies to their latest versions and fixed all security vulnerabilities.
- [MISC] Updated npm scripts to remove existing files before building/bundling to avoid issues with old code.
- [MISC] Renamed lib directory to build to be consistent with other projects.

## 0.2.1 / 2020-04-16
- [MISC] Updated out-of-date dependencies to their latest versions which also fixed all possible fixed security vulnerabilities.

Expand Down
108 changes: 57 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,30 @@ If you're using collider2d in a browser environment, you can use the `collider2d

```js
// Non-webpack environment:
import Collider2D from '../node_modules/collider2d/collider2d.js';
import { Vector, Box, Circle, Polygon, Collider2d } from '../node_modules/collider2d/collider2d.js';

// Webpack/other bundler environment:
import Collider2D from 'collider2d';
import { Vector, Box, Circle, Polygon, Collider2d } from 'collider2d';
```

and in a Node environment, you can imply require the module:

**node**

```js
const Collider2D = require('collider2d');
const { Vector, Box, Circle, Polygon, Collider2d } = require('collider2d');
```

lastly you just have to make a new instance of Collider2D like so:
The `Vector`, `Box`, `Circle`, and `Polygon` classes are used to create the shapes to test and then the `Collider2d` class is used to test collisions using the created shapes. Just like with the geometry classes, you'll need to create a new instance of the class to use the collision tests. In the examples below you'll see:

```js
const c2d = new Collider2D();
const collision = collider2d.testPolygonPolygon();
```

This implies that an instance of the `Collider2d` is created beforehand like so:

```js
const collider2d = new Collider2d();
```

## **Geometry**
Expand All @@ -87,7 +93,7 @@ A vector is a point in 2D space that has a x and y position.
**example:**

```js
const vec1 = c2d.vector(10, 25);
const vec1 = new Vector(10, 25);
```

To see the full list of properties and methods for circles, check out the [vector documentation](docs/vector.md).
Expand All @@ -104,7 +110,7 @@ A circle consists of a center position and a radius.
**example:**

```js
const circle = c2d.circle(c2d.vector(5, 5), 10);
const circle = new Circle(new Vector(5, 5), 10);
```

To see the full list of properties and methods for circles, check out the [circles documentation](docs/circle.md).
Expand All @@ -121,11 +127,11 @@ A polygon consists of a convex shape with any number of points (specified in a c
**example:**

```js
const polygon = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
const polygon = new Polygon(new Vector(0, 0), [
new Vector(0, 0),
new Vector(40, 0),
new Vector(40, 40),
new Vector(0, 40)
]);
```

Expand All @@ -144,7 +150,7 @@ A box represents an axis-aligned bounding box with a width and a height.
**example:**

```js
const box = c2d.box(c2d.vector(5, 10), 100, 250);
const box = new Box(new Vector(5, 10), 100, 250);
```

To see the full list of properties and methods for boxes, check out the [box documentation](docs/box.md).
Expand Down Expand Up @@ -185,9 +191,9 @@ Returns true if the point is in the circle or false otherwise.
**example:**

```js
const circle = c2d.circle(c2d.vector(100, 100), 20);
const circle = new Circle(new Vector(100, 100), 20);

const collision = c2d.pointInCircle(c2d.vector(110, 110), circle); // true
const collision = collider2d.pointInCircle(new Vector(110, 110), circle); // true
```

### **pointInPolygon**
Expand All @@ -204,13 +210,13 @@ Returns true if the point is in the polygon or false otherwise.
**example:**

```js
const triangle = c2d.polygon(c2d.vector(30, 0), [
c2d.vector(0, 0),
c2d.vector(30, 0),
c2d.vector(0, 30)
const triangle = new Polygon(new Vector(30, 0), [
new Vector(0, 0),
new Vector(30, 0),
new Vector(0, 30)
]);

const collision = c2d.pointInPolygon(c2d.vector(35, 5), triangle); // true
const collision = collider2d.pointInPolygon(new Vector(35, 5), triangle); // true
```

## **Polygon Collisions**
Expand All @@ -230,24 +236,24 @@ Returns true if the circles collide or false otherwise. If details is set to tru
**example:**

```js
const polygon1 = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
const polygon1 = new Polygon(new Vector(0, 0), [
new Vector(0, 0),
new Vector(40, 0),
new Vector(40, 40),
new Vector(0, 40)
]);

const polygon2 = c2d.polygon(c2d.vector(30, 0), [
c2d.vector(0, 0),
c2d.vector(30, 0),
c2d.vector(0, 30)
const polygon2 = new Polygon(new Vector(30, 0), [
new Vector(0, 0),
new Vector(30, 0),
new Vector(0, 30)
]);

// No details
const collided = c2d.testPolygonPolygon(polygon1, polygon2); // true
const collided = collider2d.testPolygonPolygon(polygon1, polygon2); // true

// Details
const collidedDetails = c2d.testPolygonPolygon(polygon1, polygon2, true);
const collidedDetails = collider2d.testPolygonPolygon(polygon1, polygon2, true);
console.log(collidedDetails.overlap); // 10
```

Expand All @@ -266,20 +272,20 @@ Returns true if the circles collide or false otherwise. If details is set to tru
**example:**

```js
const circle = c2d.circle(c2d.vector(50, 50), 20);
const circle = new Circle(new Vector(50, 50), 20);

const polygon = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
const polygon = new Polygon(new Vector(0, 0), [
new Vector(0, 0),
new Vector(40, 0),
new Vector(40, 40),
new Vector(0, 40)
]);

// No details
const collided = c2d.testPolygonCircle(polygon, circle); // true
const collided = collider2d.testPolygonCircle(polygon, circle); // true

// Details
const collidedDetails = c2d.testPolygonCircle(polygon, circle, true);
const collidedDetails = collider2d.testPolygonCircle(polygon, circle, true);
console.log(collidedDetails.overlap.toFixed(2)); // 5.86
```

Expand All @@ -300,14 +306,14 @@ Returns true if the circles collide or false otherwise. If details is set to tru
**example:**

```js
const circle1 = c2d.circle(c2d.vector(0, 0), 20);
const circle2 = c2d.circle(c2d.vector(30, 0), 20);
const circle1 = new Circle(new Vector(0, 0), 20);
const circle2 = new Circle(new Vector(30, 0), 20);

// No details
const collided = c2d.testCircleCircle(circle1, circle2); // true
const collided = collider2d.testCircleCircle(circle1, circle2); // true

// Details
const collidedDetails = c2d.testCircleCircle(circle1, circle2, true);
const collidedDetails = collider2d.testCircleCircle(circle1, circle2, true);
console.log(collidedDetails.overlap); // 10
```

Expand All @@ -328,20 +334,20 @@ Returns true if the circles collide or false otherwise. If details is set to tru
**example:**

```js
const circle = c2d.circle(c2d.vector(50, 50), 20);
const circle = new Circle(new Vector(50, 50), 20);

const polygon = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
const polygon = new Polygon(new Vector(0, 0), [
new Vector(0, 0),
new Vector(40, 0),
new Vector(40, 40),
new Vector(0, 40)
]);

// No details
const collided = c2d.testCirclePolygon(circle, polygon); // true
const collided = collider2d.testCirclePolygon(circle, polygon); // true

// Details
const collidedDetails = c2d.testCirclePolygon(circle, polygon, true);
const collidedDetails = collider2d.testCirclePolygon(circle, polygon, true);
```

## **Tests**
Expand Down
46 changes: 4 additions & 42 deletions lib/index.d.ts → build/collider2d.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Box from './geometry/Box';
import Vector from './geometry/Vector';
import Circle from './geometry/Circle';
import Polygon from './geometry/Polygon';
import CollisionDetails from './details/CollisionDetails';
import Vector from './geometry/vector';
import Circle from './geometry/circle';
import Polygon from './geometry/polygon';
import CollisionDetails from './collision_details';
export default class Collider2D {
/**
* A pool of `Vector objects that are used in calculations to avoid allocating memory.
Expand Down Expand Up @@ -61,43 +60,6 @@ export default class Collider2D {
*/
private _RIGHT_VORONOI_REGION;
constructor();
/**
* Creates a new vector.
*
* @param {number} x The x position of the vector.
* @param {number} y The y position of the vector.
*
* @returns {Vector} Returns the newly created vector.
*/
vector(x: number, y: number): Vector;
/**
* Creates a new circle.
*
* @param {Vector} position A vector specifying the center position of the circle.
* @param {number} radius The radius of the circle.
*
* @returns {Circle} Returns the newly created circle.
*/
circle(position: Vector, radius: number): Circle;
/**
* Creates a new polygon.
*
* @param {Vector} position A vector representing the origin point of the polygon.
* @param {Array<Vector>} points An array of vectors that specifies the points of the polygon, in counter-clockwise order.
*
* @returns {Polygon} Returns the newly created plygon.
*/
polygon(position: Vector, points: Array<Vector>): Polygon;
/**
* Creates a new box.
*
* @param {Vector} position A vector representing the position of the box.
* @param {number} width The width of the box.
* @param {number} height The height of the box.
*
* @returns {Box} Returns the newly created box.
*/
box(position: Vector, width: number, height: number): Box;
/**
* Check if a point is inside a circle.
*
Expand Down
Loading

0 comments on commit 2d9e565

Please sign in to comment.