[SOLVED] Self destruction of objects(or nodes) when they fall off the scene

Okay, i have tried it & it works successfully …

i have added some customizations to it to remove the spatial controls or physics(Rocks or laser gun particles) from the physics space as well & CharacterControl instead of a Spatial(EDIT)

Thanks @jayfella & @pspeed

My Code snippet:

public class RockDecayControl extends AbstractControl {

private BulletAppState physics;
//character control physics of that spatial
private CharacterControl player;

private final float maxDistance = 180; // 180 world units.
private float maxtime=300;//max time is 300 seconds approximately when the rocks are out of the scene level
private float elapsedTime=0; //incremental time stack

public RockDecayControl(CharacterControl player,BulletAppState physics) {
    this.player = player;
    this.physics=physics;
}

@Override
protected void controlUpdate(float tpf) {

    elapsedTime+=0.5;//increment the time
    //calculate distance from spaceship & spatial according to local translation
    float distance = FastMath.abs(getSpatial().getLocalTranslation().distance(player.getPhysicsLocation()));
    
    if ( elapsedTime > maxtime || distance > maxDistance) {
        //remove spatial(Rocks & Laser Gun Particles) from parent Node
        getSpatial().removeFromParent();
        //remove spatials ' physics from physics space to free it
        physics.getPhysicsSpace().remove(getSpatial());
        //reset time stack system
        elapsedTime=0;
    }
    
}

@Override
protected void controlRender(RenderManager rm, ViewPort vp) {

}
}

Calling it in main GameApplication context:

rock.addControl(new RockDecayControl(player,physics));

After ;

    NodeControls = new RigidBodyControl(CollisionShapeFactory.createDynamicMeshShape(rock), mass);
                //add the physics to the mesh Spatial
                rock.addControl(NodeControls);

& so on & so force for Laser Gun spatials

Images of what’s happening

not too much rocks in the site & they disapper quickly when reaching limits

& i think this helped me too ; increasing the heap size of JVM for my game

Again thank you !

1 Like

This isn’t a great idea. The variable tpf is Timer Per Frame. This holds the value of the time it took to invoke the update method from the last time it invoked the method - a.k.a Time Per Frame.

If you use static times like you did (0.5f) then the frame rate will determine the speed. For example if the game is running at 300 fps it will add 0.5 300 times per second. If I have a less powerful machine and it runs at 74 fps - the update method will be invoked 74 times per second.

The tpf value will always equal 1 per second - no matter what the frame rate is - which gives you a constant - no matter what the framerate of the game is.

TLDR; you should increment it using tpf.

2 Likes

Okay , I will fix it :+1:then maxTime = 10;