I am developing a Universe game that spawn spaceship as a character control & spawn rocks …5 rocks randomly in a fixed range every 200 ms I have put my method that makes rocks in the simpleUpdate of the game context & everything works normally until some time the fps began to dropout downto 10 fps I think it’s from building up objects at the end of the scene
The point :- is I want to make these fallen rocks out of the scene to be self destructed to free memory…I have tried making a control to the skymap & attach it as collision shape but it fails
Can you explain yourself a little more clearly? You say “fallen rocks” but you also mention a space ship which makes me think “zero gravity” where things do not “fall” so I struggle to “see” what you mean.
What are they colliding with in order to disappear?
Can you maybe show a screen shot? A picture says a thousand words.
Each object has its own gravitational force or linear force if you will , spaceship attached to a node & has a character control physics w/o gravity , other planets have zero gravity as well,only rocks have randomly generated gravitational forces w/ fixed ranges
-they do not disappear but you visualize them as if they are disappeared but actually they are out of draw distance of my map!
see this is the ship with spawning rocks of different gravities mainly on (-x axis)
& this is the rocks fallen out actually its outside of my draw distance not the scene , so i wanna destroy them if they are outside my draw distance "NB: they all have the same node name so if i will really check (if getChildName(“Rock”).getPhyscisLocation()==specificLocation) then detach it will work but it will actually detach new Rocks being spwaned w/ the same node name:
as for each 5500 ms … all rocks are destoyed to free some memory as this problem appears after 25 mins from running the game indeed but the problem persists
If it were me, I’d add System.out.println to verify all of the hundred assumptions being made.
As I read the code, every 5500 frames (some random amount of time), you grab a random node called “Rock node” and a random node called “Laser Gun” and remove them… never checking that you actually found one.
Very fragile.
If you want to remove a spatial after some period of time then you could add a custom control to the spatial that counts time (time -= tpf) and removes the spatial from its parent after some amount of time.
And periodically you should debug your scene to see what’s actually increasing. Could be you are adding other stuff and not thinking about it.
System.out.println() or the debugger will be your best friend.
Okay,They are not random nodes , they are indeed attached to the scene !
Is there is an example for the checking method of time tpf the in simpleUpdate ?
Not sure how better to explain because I don’t know which part is confusing.
But if you want a SPATIAL to die after a certain amount of TIME then you want to use a CONTROL… (presuming you want to keep treating spatials like game objects at all but that’s a longer and different discussion.)
Bottom line: I’ve written many programs that add remove megabytes of data from a scene at runtime and I never hit frame drop issues or out of memory issues or any of that… because I know what I’m adding and I know what I’m removing.
rather than using the simpleUpdate method in your main app to handle the destruction of the rocks (or stray laser blasts, once they are a certain distance away from the camera), it may be better to let the rock object handle it’s own destruction. (Object Oriented)
that way you don’t have to keep track and iterate through a huge list of rock objects checking their positions relative to the camera, and you skip the added effort of having to explicitly determine which object to destroy from a pool of many.
this could be done by giving each rock object it’s own control with a distance check or even a lifespan timer, as @pspeed suggested above.
Okay , i have used System.out.println() & fpstext to trigger the problem location in my code & it seems that i was calling wrong name in rootNode.detachChild(rootNode.getChild("Rock node")); i think i have fixed most of my problem now after i have matched names of nodes & removed the internal called methods & decreased the update threshold , but the problem persists when the game runs for about 40-60 mins ,i think it needs time to get rid of those rocks…( I have made some changes)
inside simpleUpdate , it works efficinetly w/ rocks but w/ laser gun not very well because if the user shoots too much particles ,the detach method wonot handle them all so , i will add a power to the gun shooter to shoot arround 10 particles only per 500 ms for example .
& i have added a small optimization for the physics space , it has decreased the time for the objects to reach out off the site:
Add the control below to every rock. It will remove itself from the scene if the time exceeds maxTime or the distance from the spaceShip exceeds maxDistance.
rock.addControl(new RockDecayControl(spaceShip);
public class RockDecayControl extends AbstractControl {
private final Spatial spaceShip;
private final float maxDistance = 10; // 10 world units.
private final float maxTime = 100; // 100 seconds.
private float elapsedTime = 0;
public RockDecayControl(Spatial spaceShip) {
this.spaceShip = spaceShip;
}
@Override
protected void controlUpdate(float tpf) {
elapsedTime += tpf;
float distance = FastMath.abs(getSpatial().getLocalTranslation().distance(spaceShip.getLocalTranslation()));
if (elapsedTime > maxTime || distance > maxDistance) {
getSpatial().removeFromParent();
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
}