Best way to detect 'out of bounds'?

Hi there, I need a smart way to detect when model gets out of collision bounds
18cc160243ac8f8030802d148ac75f0393d91454
By getting out I mean: When it just starts touching the imaginary collision wall

What I did so far: Calculating bounding box for all mesh instances

Thanks

Hi @Newbie_Coder,

You can use the intersects method by looping through your mesh instances aabb and testing it against the boundary bounding box.

As soon as it touches that wall it will return true:

https://developer.playcanvas.com/api/pc.BoundingBox.html#intersects

1 Like

I’ve built a very simple scene to test it out, small box is moved inside big box, the state changes only when small box is fully outside the other (touching the wall is not enough for state to change)

aabb = this.entity.render._meshInstances[0]._aabb;
//console.log(aabb);

otherEntity = this.app.root.findByName('Small');
otheraabb = otherEntity.render._meshInstances[0]._aabb;

console.log(aabb.intersects(otheraabb));

Update:
Final solution

aabb = this.entity.render._meshInstances[0]._aabb;

otherEntity = this.app.root.findByName('Model');

otheraabb = new pc.BoundingBox();


// once model is available (if preloaded, then straight away)
var meshes = otherEntity.model.model.meshInstances;
if (meshes.length) {
    otheraabb.copy(meshes[0].aabb);
    for(var i = 1; i < meshes.length; i++)
        otheraabb.add(meshes[i].aabb);
}


boundX = aabb.halfExtents.x - otheraabb.halfExtents.x;
boundY = aabb.halfExtents.y - otheraabb.halfExtents.y;
boundZ = aabb.halfExtents.z - otheraabb.halfExtents.z;
offsetX = otheraabb.center.x;
offsetY = otheraabb.center.y;
offsetZ = otheraabb.center.z;



var mainBound = this.entity.getPosition();


if (Math.abs(mainBound.z - offsetZ) > boundZ) {
    console.log('Exceeds on Z');
}

if (Math.abs(mainBound.y - offsetY) > boundY) {
    console.log('Exceeds on Y');
}

if (Math.abs(mainBound.x - offsetX) > boundX) {
    console.log('Exceeds on X');
}

Any way to get aabb of a blank entity that has collision element only? Debug draw displays it with no problem

So, for a blank entity with a box collision component you can use a proxy aabb. Example:

const aabb = new pc.BoundingBox();

aabb.center.copy(entity.getPosition());
aabb.halfExtents.copy(entity.collision.halfExtents);
1 Like