Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gm2-collision

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gm2-collision - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

src/circleCircle.js

8

index.js
module.exports = {
Response : require('./src/Response'),
circleCircle : require('./src/circleCircle'),
circleRect : require('./src/circleRect'),
rectRect : require('./src/rectRect'),
closestPointCircle : require('./src/closestPointCircle'),
closestPointRect : require('./src/closestPointRect'),
closestPointCircle : require('./src/closestPointCircle'),
pointCircle : require('./src/pointCircle'),
pointRect : require('./src/pointRect')
pointRect : require('./src/pointRect'),
Response : require('./src/Response')

@@ -10,0 +12,0 @@ };

{
"name": "gm2-collision",
"version": "0.0.2",
"version": "0.0.3",
"description": "2D Geometry - Collision detection and response",

@@ -25,5 +25,5 @@ "main": "index.js",

"gm2-circle": "0.0.1",
"gm2-rect": "0.0.1",
"gm2-rect": "0.0.2",
"gm2-vector2": "0.0.1"
}
}

@@ -0,1 +1,2 @@

var Vector2 = require('gm2-vector2');
var closestPointRect = require('./closestPointRect');

@@ -5,2 +6,5 @@ var pointCircle = require('./pointCircle');

// temp vector
var _v1 = new Vector2();
/**

@@ -20,3 +24,3 @@ * Check collision between a Circle and a Rect

var near = closestPointRect(circle.position, rect);
var near = closestPointRect(circle.position, rect, _v1);

@@ -32,4 +36,6 @@ if (!pointCircle(near, circle)) {

//@todo near?
var distance = circle.position.distanceTo(near);
//@todo using too many vector here, use setters instead
response.point = near;

@@ -36,0 +42,0 @@ response.normal = circle.position._subtract(near).normalize();

var Vector2 = require('gm2-vector2');
var pointCircle = require('./pointCircle');
/**
* Determine the closest point on a circle to a reference point
* Determine the closest point in a circle to a reference point
* @param {Vector2} point a point in space
* @param {Circle} circle a circle
* @param {Vector2} vector
* @return {Vector2} the closest point on the circle to the point
* @return {Vector2} the closest point in the circle to the point
*/

@@ -14,4 +15,9 @@ module.exports = function( point, circle, vector ) {

vector.set(point.x, point.y);
vector.set(point);
// check if it's inside first
if(pointCircle(point, circle)) {
return vector;
}
vector.subtract(circle.position);

@@ -18,0 +24,0 @@

@@ -0,1 +1,3 @@

var Vector2 = require('gm2-vector2');
/**

@@ -7,29 +9,29 @@ * Determine the closest point on a rect to another point

*/
module.exports = function (point, rect) {
module.exports = function (point, rect, vector) {
// @todo use temp vec
var closest = point.copy();
if(!vector) { vector = new Vector2(); }
var top = rect.position.y;
var left = rect.position.x;
var right = left + rect.width;
var bottom = top + rect.height;
vector.set(point);
// clamp to inside?
var x = Math.min(point.x, Math.max(left, right));
var y = Math.min(point.y, Math.max(top, bottom));
if( point.x < rect.position.x ) {
var dl = Math.abs(x - left);
var dr = Math.abs(x - right);
var dt = Math.abs(y - top);
var db = Math.abs(y - bottom);
vector.x = rect.position.x;
var min = Math.min(dl, dr, dt, db);
} else if ( point.x > rect.position.x + rect.width ) {
if(min == dt) { closest.set(x, top); }
else if(min == db) { closest.set(x, bottom); }
else if(min == dl) { closest.set(left, y); }
else { closest.set(right, y); }
vector.x = rect.position.x + rect.width;
return closest;
}
if( point.y < rect.position.y ) {
vector.y = rect.position.y;
} else if ( point.y > rect.position.y + rect.height ) {
vector.y = rect.position.y + rect.height;
}
return vector;
};

@@ -20,4 +20,4 @@ var closestPointCircle = require('./closestPointCircle');

closestPointCircle(point, circle, response.point);
response.normal.set(response.point).normalize();
response.point.set(point);
response.normal.set(point).normalize();
response.depth = circle.radius - Math.sqrt((dx * dx) + (dy * dy));

@@ -24,0 +24,0 @@ }

@@ -10,5 +10,5 @@ var expect = require('chai').expect;

it('works inside the circle', function () {
it('works outside the circle', function () {
var point = new Vector2(5,5);
var point = new Vector2(-15,-15);
var circle = new Circle(10);

@@ -18,9 +18,9 @@

expect(closest.x).to.equal(7.071067811865475);
expect(closest.y).to.equal(7.071067811865475);
expect(closest.x).to.equal(-7.071067811865475);
expect(closest.y).to.equal(-7.071067811865475);
});
it('works outside the circle', function () {
it('works inside the circle', function () {
var point = new Vector2(-15,-15);
var point = new Vector2(5, 6);
var circle = new Circle(10);

@@ -30,6 +30,18 @@

expect(closest.x).to.equal(-7.071067811865475);
expect(closest.y).to.equal(-7.071067811865475);
expect(closest.x).to.equal(5);
expect(closest.y).to.equal(6);
});
it('uses passed in vector', function () {
var temp = new Vector2();
var point = new Vector2(5, 6);
var circle = new Circle(10);
Collision.closestPointCircle(point, circle, temp);
expect(temp.x).to.equal(5);
expect(temp.y).to.equal(6);
});
};

@@ -10,2 +10,13 @@ var expect = require('chai').expect;

it('works outside the rect', function () {
var point = new Vector2(15, 15);
var rect = new Rect(10, 10);
var closest = Collision.closestPointRect(point, rect);
expect(closest.x).to.equal(10);
expect(closest.y).to.equal(10);
});
it('works inside the rect', function () {

@@ -18,17 +29,18 @@

expect(closest.x).to.equal(10);
expect(closest.x).to.equal(6);
expect(closest.y).to.equal(5);
});
it('works outside the rect', function () {
it('uses passed in vector', function () {
var point = new Vector2(15, 15);
var temp = new Vector2();
var point = new Vector2(6, 5);
var rect = new Rect(10, 10);
var closest = Collision.closestPointRect(point, rect);
Collision.closestPointRect(point, rect, temp);
expect(closest.x).to.equal(10);
expect(closest.y).to.equal(10);
expect(temp.x).to.equal(6);
expect(temp.y).to.equal(5);
});
};

@@ -44,3 +44,3 @@ var expect = require('chai').expect;

var point = new Vector2(5,5);
var point = new Vector2(5,8);
var circle = new Circle(10);

@@ -53,9 +53,9 @@

expect(collided).to.be.true;
expect(response.point.x).to.equal(7.071067811865475);
expect(response.point.y).to.equal(7.071067811865475);
expect(response.normal.x).to.equal(0.7071067811865475);
expect(response.normal.y).to.equal(0.7071067811865475);
expect(response.depth).to.equal(2.9289321881345245);
expect(response.point.x).to.equal(5);
expect(response.point.y).to.equal(8);
expect(response.normal.x).to.equal(0.52999894000318);
expect(response.normal.y).to.equal(0.847998304005088);
expect(response.depth).to.equal(0.5660188679433968);
});
};
var describe = require('mocha').describe;
describe('circleCircle', require('./circleCircle'));
describe('circleRect', require('./circleRect'));
describe('rectRect', require('./rectRect'));
describe('closestPointCircle', require('./closestPointCircle'));

@@ -5,0 +7,0 @@ describe('closestPointRect', require('./closestPointRect'));

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc