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)
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 !