Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
minimumImpulsePair.isActive = true;
minimumImpulsePair.collision.parentA.totalContacts += minimumImpulsePair.activeContacts.length;
minimumImpulsePair.collision.parentB.totalContacts += minimumImpulsePair.activeContacts.length;
// Activate edge similar to or concave to the shortest edge
for (j = 0; j < edgePairs.length; j++) {
pair = edgePairs[j];
// Skip the minimum pair
if (pair === minimumImpulsePair) {
continue;
}
// If this edge's normal pushes away from the delta vector between
// itself and the other edge, we can assume concavity
let edgePositionDelta = Vector.sub(pair.collision.edge.position, minimumImpulsePair.collision.edge.position);
if (Vector.dot(edgePositionDelta, pair.collision.edge.normals[1]) <= 0) {
pair.isActive = true;
}
// Accumulate the contacts of any pairs concave to the shortest pair
if (pair.isActive) {
pair.collision.parentA.totalContacts += pair.activeContacts.length;
pair.collision.parentB.totalContacts += pair.activeContacts.length;
}
}
}
};
let edge0;
let edge1 = Vector.sub(m_v2, m_v1);
edge1 = Vector.normalise(edge1);
let edge2;
let m_normal0 = edge.normals[0];
let m_normal1 = edge.normals[1];
let m_normal2 = edge.normals[2];
let convex1 = false;
let convex2 = false;
// Is there a leading edge, is it convex?
if (hasVertex0) {
edge0 = Vector.sub(m_v1, m_v0);
edge0 = Vector.normalise(edge0);
convex1 = Vector.cross(edge0, edge1) >= 0.0;
}
// Is there a trailing edge, is it convex?
if (hasVertex3) {
edge2 = Vector.sub(m_v3, m_v2);
edge2 = Vector.normalise(edge2);
convex2 = Vector.cross(edge1, edge2) >= 0.0;
}
// Determine collision normal limits for front and back collisions
if (hasVertex0 && hasVertex3) {
if (convex1 && convex2) {
front.normal = m_normal1;
front.lowerLimit = m_normal0;
function buildEdgeNormalRanges(edge) {
let front = {};
let back = {};
let m_v0 = edge.vertices[0];
let m_v1 = edge.vertices[1];
let m_v2 = edge.vertices[2];
let m_v3 = edge.vertices[3];
let hasVertex0 = !!m_v0;
let hasVertex3 = !!m_v3;
let edge0;
let edge1 = Vector.sub(m_v2, m_v1);
edge1 = Vector.normalise(edge1);
let edge2;
let m_normal0 = edge.normals[0];
let m_normal1 = edge.normals[1];
let m_normal2 = edge.normals[2];
let convex1 = false;
let convex2 = false;
// Is there a leading edge, is it convex?
if (hasVertex0) {
edge0 = Vector.sub(m_v1, m_v0);
edge0 = Vector.normalise(edge0);
convex1 = Vector.cross(edge0, edge1) >= 0.0;
}
let m_normal1 = edge.normals[1];
let m_normal2 = edge.normals[2];
let convex1 = false;
let convex2 = false;
// Is there a leading edge, is it convex?
if (hasVertex0) {
edge0 = Vector.sub(m_v1, m_v0);
edge0 = Vector.normalise(edge0);
convex1 = Vector.cross(edge0, edge1) >= 0.0;
}
// Is there a trailing edge, is it convex?
if (hasVertex3) {
edge2 = Vector.sub(m_v3, m_v2);
edge2 = Vector.normalise(edge2);
convex2 = Vector.cross(edge1, edge2) >= 0.0;
}
// Determine collision normal limits for front and back collisions
if (hasVertex0 && hasVertex3) {
if (convex1 && convex2) {
front.normal = m_normal1;
front.lowerLimit = m_normal0;
front.upperLimit = m_normal2;
back.normal = Vector.neg(m_normal1);
back.lowerLimit = Vector.neg(m_normal1);
back.upperLimit = Vector.neg(m_normal1);
} else if (convex1) {
front.normal = m_normal1;
}
// Set some further properties on the collision object
collision.bodyA = bodyA.id < bodyB.id ? bodyA : bodyB;
collision.bodyB = bodyA.id < bodyB.id ? bodyB : bodyA;
collision.collided = true;
collision.depth = minOverlap.overlap;
collision.edge = minOverlap.edge;
collision.parentA = collision.bodyA.parent;
collision.parentB = collision.bodyB.parent;
bodyA = collision.bodyA;
bodyB = collision.bodyB;
// Ensure that the collision normal is facing away from bodyA
if (!minOverlap.flip && Vector.dot(minOverlap.axis, Vector.sub(bodyB.position, bodyA.position)) < 0) {
collision.normal = {
x: minOverlap.axis.x,
y: minOverlap.axis.y
};
} else {
collision.normal = {
x: -minOverlap.axis.x,
y: -minOverlap.axis.y
};
}
collision.tangent = Vector.perp(collision.normal);
collision.penetration = collision.penetration || {};
collision.penetration.x = collision.normal.x * collision.depth;
collision.penetration.y = collision.normal.y * collision.depth;
// Skip over tile-neighbour pairs that don't overlap
if (!Bounds.overlaps(tileBody.bounds, neighbourBody.bounds)) {
continue;
}
tv = tileVertices;
nv = neighbourVertices;
tn = tileBody.axes;
nn = neighbourBody.axes;
// Iterate over the vertices of both the tile and its neighbour
for (i = 0; i < tv.length; i++) {
for (j = 0; j < nv.length; j++) {
// Find distances between the vertices
let da = Vector.magnitudeSquared(Vector.sub(tv[(i + 1) % tv.length], nv[j])),
db = Vector.magnitudeSquared(Vector.sub(tv[i], nv[(j + 1) % nv.length]));
// If both vertices are very close, consider the edge coincident (internal)
if (da < maxDist && db < maxDist) {
tv[i].isInternal = true;
nv[j].isInternal = true;
tn[i].ignore = true;
nn[j].ignore = true;
}
}
}
}
}
},
function distance(firstVector, secondVector) {
return Vector.magnitudeSquared(Vector.sub(firstVector, secondVector));
}