134 lines
4.6 KiB
JavaScript
134 lines
4.6 KiB
JavaScript
/*
|
|
* Copyright (c) 2016-2017 Ali Shakiba http://shakiba.me/planck.js
|
|
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
|
*
|
|
* This software is provided 'as-is', without any express or implied
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
* arising from the use of this software.
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
* including commercial applications, and to alter it and redistribute it
|
|
* freely, subject to the following restrictions:
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
* claim that you wrote the original software. If you use this software
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
* appreciated but is not required.
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
* misrepresented as being the original software.
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
*/
|
|
|
|
DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
|
|
ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
|
|
|
var common = require('../util/common');
|
|
var Math = require('../common/Math');
|
|
var Transform = require('../common/Transform');
|
|
var Rot = require('../common/Rot');
|
|
var Vec2 = require('../common/Vec2');
|
|
var AABB = require('../collision/AABB');
|
|
var Settings = require('../Settings');
|
|
var Manifold = require('../Manifold');
|
|
var Contact = require('../Contact');
|
|
var Shape = require('../Shape');
|
|
var CircleShape = require('./CircleShape');
|
|
var PolygonShape = require('./PolygonShape');
|
|
|
|
Contact.addType(PolygonShape.TYPE, CircleShape.TYPE, PolygonCircleContact);
|
|
|
|
function PolygonCircleContact(manifold, xfA, fixtureA, indexA, xfB, fixtureB, indexB) {
|
|
ASSERT && common.assert(fixtureA.getType() == PolygonShape.TYPE);
|
|
ASSERT && common.assert(fixtureB.getType() == CircleShape.TYPE);
|
|
CollidePolygonCircle(manifold, fixtureA.getShape(), xfA, fixtureB.getShape(),
|
|
xfB);
|
|
}
|
|
|
|
function CollidePolygonCircle(manifold, polygonA, xfA, circleB, xfB) {
|
|
manifold.pointCount = 0;
|
|
|
|
// Compute circle position in the frame of the polygon.
|
|
var c = Transform.mul(xfB, circleB.m_p);
|
|
var cLocal = Transform.mulT(xfA, c);
|
|
|
|
// Find the min separating edge.
|
|
var normalIndex = 0;
|
|
var separation = -Infinity;
|
|
var radius = polygonA.m_radius + circleB.m_radius;
|
|
var vertexCount = polygonA.m_count;
|
|
var vertices = polygonA.m_vertices;
|
|
var normals = polygonA.m_normals;
|
|
|
|
for (var i = 0; i < vertexCount; ++i) {
|
|
var s = Vec2.dot(normals[i], Vec2.sub(cLocal, vertices[i]));
|
|
|
|
if (s > radius) {
|
|
// Early out.
|
|
return;
|
|
}
|
|
|
|
if (s > separation) {
|
|
separation = s;
|
|
normalIndex = i;
|
|
}
|
|
}
|
|
|
|
// Vertices that subtend the incident face.
|
|
var vertIndex1 = normalIndex;
|
|
var vertIndex2 = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0;
|
|
var v1 = vertices[vertIndex1];
|
|
var v2 = vertices[vertIndex2];
|
|
|
|
// If the center is inside the polygon ...
|
|
if (separation < Math.EPSILON) {
|
|
manifold.pointCount = 1;
|
|
manifold.type = Manifold.e_faceA;
|
|
manifold.localNormal.set(normals[normalIndex]);
|
|
manifold.localPoint.wSet(0.5, v1, 0.5, v2);
|
|
manifold.points[0].localPoint = circleB.m_p;
|
|
manifold.points[0].id.key = 0;
|
|
return;
|
|
}
|
|
|
|
// Compute barycentric coordinates
|
|
var u1 = Vec2.dot(Vec2.sub(cLocal, v1), Vec2.sub(v2, v1));
|
|
var u2 = Vec2.dot(Vec2.sub(cLocal, v2), Vec2.sub(v1, v2));
|
|
if (u1 <= 0.0) {
|
|
if (Vec2.distanceSquared(cLocal, v1) > radius * radius) {
|
|
return;
|
|
}
|
|
|
|
manifold.pointCount = 1;
|
|
manifold.type = Manifold.e_faceA;
|
|
manifold.localNormal.wSet(1, cLocal, -1, v1);
|
|
manifold.localNormal.normalize();
|
|
manifold.localPoint = v1;
|
|
manifold.points[0].localPoint.set(circleB.m_p);
|
|
manifold.points[0].id.key = 0;
|
|
} else if (u2 <= 0.0) {
|
|
if (Vec2.distanceSquared(cLocal, v2) > radius * radius) {
|
|
return;
|
|
}
|
|
|
|
manifold.pointCount = 1;
|
|
manifold.type = Manifold.e_faceA;
|
|
manifold.localNormal.wSet(1, cLocal, -1, v2);
|
|
manifold.localNormal.normalize();
|
|
manifold.localPoint.set(v2);
|
|
manifold.points[0].localPoint.set(circleB.m_p);
|
|
manifold.points[0].id.key = 0;
|
|
} else {
|
|
var faceCenter = Vec2.mid(v1, v2);
|
|
var separation = Vec2.dot(cLocal, normals[vertIndex1])
|
|
- Vec2.dot(faceCenter, normals[vertIndex1]);
|
|
if (separation > radius) {
|
|
return;
|
|
}
|
|
|
|
manifold.pointCount = 1;
|
|
manifold.type = Manifold.e_faceA;
|
|
manifold.localNormal.set(normals[vertIndex1]);
|
|
manifold.localPoint.set(faceCenter);
|
|
manifold.points[0].localPoint.set(circleB.m_p);
|
|
manifold.points[0].id.key = 0;
|
|
}
|
|
}
|