Weird behaviour of ingame AppState when instantiated multiple times

Hi there,

hopefully you wonderful people can save my mood one more time!
I am not too experienced with handling AppStates, so i suspect my mistake may have to do something with wrong implementation of cleanup() or general state handling - but i don’t want to influence your opinion in advance!

So here’s the deal: My game has only 3 states: MainMenuState, ShopState and IngameState. Everything works fine until i instantiate and attach IngameState more than once within the same lifecycle of the app.

This is how it looks the first time (and how it should look):

Note that the glowing around the planet and stuff is actually a thin glowing torus with transparent material so you only see the glowing of it.

Now i detach IngameState, attach a new MainMenuState, and vice versa (fresh instance of IngameState!) and it looks like this:

Everything is screwed up somehow. Like the coordinate systems are all confused. Also note that the ship and base are brighter!

Third iteration of the process:

Materials are even brighter, models draw weird lines from their egdes to their misplaced glowing rings.

My IngameState holds the complete ingame logic and everything. It implements physics, inputs and all. I know this isn’t optimal, but i am learning :smile:
Anyway, i try to clean up properly: detach everything from rootNode, detach bulletAppState, everything. I mean it’s a fresh instance of IngameState everytime! Any ideas what could be the problem, here?

NOTE: right now i realize if i turn on the debug mode of jbullet, the wireframes of the collision shapes are in place during the first ingame run and just as misplaced as everything else when i go ingame a second time!

Sorry i don’t post code, the game is already very big and i don’t want to throw 2000 lines of code at you.

Are you creating a new appstate every time you switch or just disabling?

It’s a new instance of the IngameState every time, actually. It gets mission- and savegame- data and stuff with the constructor, so it should completely start from scratch every time…

If you are using JME 3.0, I have noticed something weird with ambient lights. They don’t get removed correctly. That could explain your models getting brighter and brighter. If you keep adding ambient lights in your AppStates.

Hmm apparently something isn’t being resetted. Is this issue just appearing when you attach an new IngameAppState or does it sometimes also appear after playing in the same AppState for a long time? Do you really reset everything? Lights(because the materials are brighter), the camera position, the rootNode, all your other appstates etc.etc. Detaching all that stuff can really be annoying as I keep forgetting things :slight_smile:

You are right. I separately removed the light from the rootNode and this light-related issue is gone now. Thanks!
But the other issues are still the same (misplaced graphics and weird display of physics debug wireframes).

I managed to capture two screens which show a good comparison between OK and NONONO…

First run:

Third (or so) run:

Note how even the particles of the ships engine are completely off :frowning:

Is there any way i miss how to reset any coordinate systems or anything? This question doesn’t even seem to make sense to me… i am desperate :smile:

Do you maybe change Vector3f.ZERO or something as you forget to clone it? Just making wild guesses here but that’s a really strange issue… Maybe you keep other vectors or something like that… You shouldn’t be able to brake anything from the coordinate system afaik so your vectors must be off somehow…

i realized the weird lines going from the spheres to where ever mark exactly the “top” and “bottom” of those spheres - maybe this could be a hint? Here’s another screen, this time with physics debug on:

In the first run, everything was in place though…

I could track the problem down to a control i wrote for positioning the cam according to the ships position and speed. Tomorrow, i’ll try to figure out how to fix it and report back! Thanks for your help so far!

If you still need our help then you must post code that includes your initialize(), cleanup(), setEnabled() (if you have it) code.

Else if you like just seeing a bunch of random stabs in the dark just continue like you are.

Yeah sorry, you are right, of course. As i said - this is already quite huge and i moved a lot of initializing code into several other methods. Without knowing where to look for the problem, i could’ve just given you the link to the repo, as posting the whole code here would have been a bit of an overkill.
Anyway, you are right. Thanks for now!

Haha, this was actually a very good “random stab in the dark”, as it is exactly what happened! I forgot to call .clone() in some places i used Vector3f.ZERO and Vector3f.UNIT_XYZ… It solved the problem!
Thank you guys, i learned from this!