Remove a clone when out of Camera View

Good day,

I have a project with an Orthographic camera.

How would I go about removing an entities clone from the game when it’s out of the camera’s view?


You could do something like in the image; add collision boxes (nested within camera hierarchy) just out of the camera’s view-port (off screen). And any of the cloned entities that collide with the collision boxes are destroyed.

Hi Kevin,

Thank you for your response.

I have seen something similar to this work for some games but the problem is it might not work as well for a static orthographic camera system like the one we’re using.

We we’re thinking about rather calculating the orthographic width from the height provided in the camera entity and adding a programtic listener to remove the clones when they’re position is out of the orthographic height and width.

Thanks again,

If it’s a game like a shoot’em up i suggest do disable the entity and enable it again (when used) to save memory coz deleting it and creating a new one is more expensive as memory usage as the game progress

1 Like

I normally have a pool of entities:

var alienPoolSize = 10;
this.alienPool = [];
for (var i = 0; i < alienPoolSize; i++) {
    var clone = alienTemplate.clone();

Then, when I need an entity, I grab it from the pool:

spawnAlien: function (x, y, z) {
    var alien = this.alienPool.pop();
    alien.setPosition(x, y, z);

    var gameRoot ='Game');

    alien.enabled = true;

And when it’s destroyed, put it back in the pool:

destroyAlien: function (alien) {
    var gameRoot ='Game');
    alien.enabled = false;

Something like that. This is untested code - written just to show the principle of using a pool of preallocated entities to avoid the expense of cloning mid-game.

So how to remove aliens when they’re out of view? Well, there is a frustum cull option on the camera which can ensure out of frustum ones don’t get rendered. But you might want to totally remove them from the scene as you say. To do that, you can do a quick and easy test of some bounding volume against the camera frustum. You might know that your entities are, at most, 1 unit in diameter (just as an example). You can use the pc.Frustum class to test a bounding sphere:

initialize: function () {
    this.boundingSphere = new pc.BoundingSphere(this.entity.getPosition(), 1);

update: function (dt) {
    var cameraEntity ='Camera');;
    if (! {
        // Disable entity and return it to the object pool