refactor: eliminate redundant collision checks

This commit is contained in:
cha0s 2019-03-23 20:19:43 -05:00
parent 0ea3e0f6c5
commit 8418665892

View File

@ -125,6 +125,7 @@ export class World {
}
// Contact checks.
const allContacts = new Map();
const checkedSoFar = new Map();
for (const entity of this.entities) {
const body = entity.body;
// Find bodies in AABB.
@ -139,10 +140,19 @@ export class World {
// Uniques only.
otherBodies = arrayUnique(otherBodies);
// TODO: full collision check
const checkSet = new Set();
checkedSoFar.set(entity, checkSet);
for (const otherBody of otherBodies) {
if (otherBody.entity) {
const otherEntity = otherBody.entity;
// Self contacts.
// Only one check per pair.
const otherCheckSet = checkedSoFar.get(otherEntity);
if (otherCheckSet) {
if (otherCheckSet.has(entity)) {
continue;
}
}
// Self contacts.
let contacts = allContacts.get(entity);
if (!contacts) {
contacts = I.Set();
@ -156,6 +166,8 @@ export class World {
}
otherContacts = otherContacts.add(entity);
allContacts.set(otherEntity, otherContacts);
// Mark as checked.
checkSet.add(otherBody.entity);
}
}
if (!allContacts.get(entity)) {