Skip to content

Commit

Permalink
Added Generic Points Array to Polygon
Browse files Browse the repository at this point in the history
- [FEATURE] Added `genericPoints` Array to Polygon which is an Array of all of the points in the Polygon as just numbers instead of Vectors. This is useful for creating triangles from Polygons.
  • Loading branch information
robertcorponoi committed Sep 30, 2020
1 parent 0e50150 commit c694fd7
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.1.0 / 2020-09-30
- [FEATURE] Added `genericPoints` Array to Polygon which is an Array of all of the points in the Polygon as just numbers instead of Vectors. This is useful for creating triangles from Polygons.

## 1.0.1 / 2020-09-29
- [HOTFIX] Fixed the location of the typings.

## 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.
Expand Down
14 changes: 14 additions & 0 deletions build/geometry/polygon.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export default class Polygon {
* @property {Array<Vector>}
*/
private _points;
/**
* An Array of the points of this polygon as numbers instead of Vectors.
*
* @private
*
* @property {Array<number>}
*/
private _pointsGeneric;
/**
* The angle of this polygon.
*
Expand Down Expand Up @@ -91,6 +99,12 @@ export default class Polygon {
* @returns {Array<Vector>}
*/
get points(): Array<Vector>;
/**
* Returns the points of this polygon as numbers instead of Vectors.
*
* @returns {Array<number>}
*/
get pointsGeneric(): Array<number>;
/**
* Returns the calculated points of this polygon.
*
Expand Down
27 changes: 25 additions & 2 deletions build/geometry/polygon.js

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion collider2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ var Polygon = /*#__PURE__*/function () {
* @property {Array<Vector>}
*/

/**
* An Array of the points of this polygon as numbers instead of Vectors.
*
* @private
*
* @property {Array<number>}
*/

/**
* The angle of this polygon.
*
Expand Down Expand Up @@ -443,6 +451,8 @@ var Polygon = /*#__PURE__*/function () {

_defineProperty(this, "_points", []);

_defineProperty(this, "_pointsGeneric", []);

_defineProperty(this, "_angle", 0);

_defineProperty(this, "_offset", new Vector());
Expand Down Expand Up @@ -490,7 +500,9 @@ var Polygon = /*#__PURE__*/function () {
for (i = 0; i < points.length; i++) {
// Remove consecutive duplicate points
var p1 = points[i];
var p2 = i < points.length - 1 ? points[i + 1] : points[0];
var p2 = i < points.length - 1 ? points[i + 1] : points[0]; // Push the points to the generic points Array.

this._pointsGeneric.push(points[i].x, points[i].y);

if (p1 !== p2 && p1.x === p2.x && p1.y === p2.y) {
points.splice(i, 1);
Expand Down Expand Up @@ -745,6 +757,17 @@ var Polygon = /*#__PURE__*/function () {
get: function get() {
return this._points;
}
/**
* Returns the points of this polygon as numbers instead of Vectors.
*
* @returns {Array<number>}
*/

}, {
key: "pointsGeneric",
get: function get() {
return this._pointsGeneric;
}
/**
* Returns the calculated points of this polygon.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "collider2d",
"version": "1.0.0",
"version": "1.1.0",
"description": "A 2D collision checker for modern JavaScript games.",
"main": "build/index.js",
"module": "collider2d.js",
Expand Down
19 changes: 19 additions & 0 deletions src/geometry/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export default class Polygon {
*/
private _points: Array<Vector> = [];

/**
* An Array of the points of this polygon as numbers instead of Vectors.
*
* @private
*
* @property {Array<number>}
*/
private _pointsGeneric: Array<number> = []

/**
* The angle of this polygon.
*
Expand Down Expand Up @@ -129,6 +138,13 @@ export default class Polygon {
*/
get points(): Array<Vector> { return this._points; }

/**
* Returns the points of this polygon as numbers instead of Vectors.
*
* @returns {Array<number>}
*/
get pointsGeneric(): Array<number> { return this._pointsGeneric; }

/**
* Returns the calculated points of this polygon.
*
Expand Down Expand Up @@ -192,6 +208,9 @@ export default class Polygon {
const p1: Vector = points[i];
const p2: Vector = i < points.length - 1 ? points[i + 1] : points[0];

// Push the points to the generic points Array.
this._pointsGeneric.push(points[i].x, points[i].y);

if (p1 !== p2 && p1.x === p2.x && p1.y === p2.y) {
points.splice(i, 1);
i -= 1;
Expand Down
67 changes: 41 additions & 26 deletions test/game-collision.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const chai = require('chai');
const { Vector, Box, Circle, Polygon, Collider2d } = require('../index');
const { Vector, Box, Circle, Polygon, Collider2d } = require('../build/index.js');

const collider2d = new Collider2d();

Expand All @@ -24,33 +24,48 @@ describe('Vector Scaling', () => {
});

describe("Polygon Centroids", () => {
it("should calculate the correct value for a square", () => {
const polygon = new Polygon(
new Vector(0, 0), [
new Vector(0, 0),
new Vector(40, 0),
new Vector(40, 40),
new Vector(0, 40)
]);

const c = polygon.getCentroid();

chai.expect(c.x).to.equal(20);
chai.expect(c.y).to.equal(20);
describe("Generic Points", () => {
it("should return the generic points of the Polygon", () => {
const polygon = new Polygon(new Vector(100, 100), [
new Vector(0, 0),
new Vector(200, 0),
new Vector(100, 200),
new Vector(0, 200),
]);

chai.expect(polygon.pointsGeneric).to.deep.equal([0, 0, 200, 0, 100, 200, 0, 200]);
});
});

it("should calculate the correct value for a triangle", () => {
const polygon = new Polygon(
new Vector(0, 0), [
new Vector(0, 0),
new Vector(100, 0),
new Vector(50, 99)
]);

const c = polygon.getCentroid();

chai.expect(c.x).to.equal(50);
chai.expect(c.y).to.equal(33);
describe("Centroids", () => {
it("should calculate the correct value for a square", () => {
const polygon = new Polygon(
new Vector(0, 0), [
new Vector(0, 0),
new Vector(40, 0),
new Vector(40, 40),
new Vector(0, 40)
]);

const c = polygon.getCentroid();

chai.expect(c.x).to.equal(20);
chai.expect(c.y).to.equal(20);
});

it("should calculate the correct value for a triangle", () => {
const polygon = new Polygon(
new Vector(0, 0), [
new Vector(0, 0),
new Vector(100, 0),
new Vector(50, 99)
]);

const c = polygon.getCentroid();

chai.expect(c.x).to.equal(50);
chai.expect(c.y).to.equal(33);
});
});
});

Expand Down

0 comments on commit c694fd7

Please sign in to comment.