
Box Overlap Check
Downloads: 55
Download Description
Checks if one box is overlapping others from an array
//
// --------------------------------------------------------
// BoxOverlapCheck
// Checks if one box overlaps others from an array
// Written by @Fusedthreed – https://FusedThreeD.com
// --------------------------------------------------------
//
@customNode()
export class BoxOverlapCheck extends BasicScriptNode {
@input()
boxA: APJS.SceneObject;
@input()
boxASize: APJS.Vector3f;
@input()
boxBList: APJS.SceneObject[];
@input()
boxBSize: APJS.Vector3f;
@input()
use3D: boolean = true;
@input()
pixelsPerUnit: number = 32;
@output()
touching: boolean;
execute(): void {
const aPos = this.boxA.getTransform().getWorldPosition();
let boxASize = this.boxASize;
let boxBSize = this.boxBSize;
if (!this.use3D) {
boxASize = new APJS.Vector3f(
boxASize.x / this.pixelsPerUnit,
boxASize.y / this.pixelsPerUnit,
boxASize.z
);
boxBSize = new APJS.Vector3f(
boxBSize.x / this.pixelsPerUnit,
boxBSize.y / this.pixelsPerUnit,
boxBSize.z
);
}
const aMin = new APJS.Vector3f(
aPos.x - boxASize.x / 2,
aPos.y - boxASize.y / 2,
aPos.z - boxASize.z / 2
);
const aMax = new APJS.Vector3f(
aPos.x + boxASize.x / 2,
aPos.y + boxASize.y / 2,
aPos.z + boxASize.z / 2
);
this.touching = false;
for (let i = 0; i < this.boxBList.length; i++) {
const bPos = this.boxBList[i].getTransform().getWorldPosition();
const bMin = new APJS.Vector3f(
bPos.x - boxBSize.x / 2,
bPos.y - boxBSize.y / 2,
bPos.z - boxBSize.z / 2
);
const bMax = new APJS.Vector3f(
bPos.x + boxBSize.x / 2,
bPos.y + boxBSize.y / 2,
bPos.z + boxBSize.z / 2
);
const overlap = this.use3D
? (
aMin.x <= bMax.x && aMax.x >= bMin.x &&
aMin.y <= bMax.y && aMax.y >= bMin.y &&
aMin.z <= bMax.z && aMax.z >= bMin.z
)
: (
aMin.x <= bMax.x && aMax.x >= bMin.x &&
aMin.y <= bMax.y && aMax.y >= bMin.y
);
if (overlap) {
this.touching = true;
break;
}
}
}
}