I used BabylonJS to build a small demo https://knutsoned.itch.io/brackeys-2024-1 and posted the code
The code was made by following the Using Vite tutorial and then migrating GitHub - RaananW/babylonjs-webpack-es6: Babylon.js basic scene with typescript, webpack, es6 modules, editorconfig, eslint, hot loading and more. Will even make coffee if you ask nicely. (thank you RaananW, your comment on another post also helped me figure out how to get Ammo to work with Vite) from a Webpack to a Vite structure.
I have a few questions so Iâll start with the biggest one. (Should I make a post for each question or put them all together?) When the player presses the R key, I set a gameOver flag. Iâm checking for the flag in the main render loop, calling scene.dispose(), then running createScene again.
In a PG context, especially with no physics, it seems ok to just make a scene, dispose, then replace the value of the old scene variable with a reference to a new one, so I tried it like this:
The main problem is the scene seems to âfall awayâ from the viewport. I canât figure out exactly why this is happening, although I suspect it might be leftover physics bodies in Ammo. So even though I am disposing of the scene, and would expect the impostors to remove the corresponding bodies from the physics engine on disposal, that doesnât seem to be the case.
I am repeating the process to initialize Ammo with every call to createScene DoorQuest/src/scenes/mainScene.ts at c970c2a9a57a0582273abf40862e192cef07d4d2 ¡ knutsoned/DoorQuest ¡ GitHub (this is the only program code in the babylonjs-webpack-es6 template that needed to be changed as mentioned above)
Looking at Babylon.js docs the suggestion is to not just call createScene over and over, but to recycle the scene by removing everything and adding it again, and perhaps that is the piece I am missing. Is there a way to empty the scene? What I really want to do is remove everything from the scene and just recreate and add fresh copies of everything back into it.
Iâve seen suggestions that an empty mesh can be used as an invisible parent container to group objects, and by removing the parent, a similar effect to âresettingâ the scene can be achieved. Does this sound like the best way to go about what Iâm trying to accomplish? Iâm trying to support procedurally generated scenes (like a dungeon crawler) so I donât want the thing to crash because of leaking objects and the like.
So, main question 1: what is happening to the new scene when I press the R button? Is the ghost of the old scene pushing it away, or is something else going on?
Main question 2: can I use empty meshes as a âmainâ container and just remove the container when I want to reset the scene, or is there a better way?