Hard FPS lost when movin meshes

I think Roland nailed it, you need to remove the disposed obstacle from the obstacles array like he did, otherwise you’ll keep checking it for intersections, each frame checking intersections against all the disposed meshes. And in the first version if you don’t remove the observer then it keeps checking for intersections against the old meshes as well (Roland nailed that part early on too I think). :slight_smile:

Hmm still don’t get why the FPS doesn’t drop when not moving the obstacles thou, if you’re still spawning new ones… :thinking:

1 Like

Thanks @Blake! :slight_smile:

The second thing that if you don’t remove the obstacle from the array but you dispose() the mesh you will perform operations on a disposed mesh.

However I can’t see any performance issues running @Alecell 's current code.

1 Like

Hey @roland I made the video you suggested, I hope you could understand my poor english speech :sweat_smile:

(I upload the video on youtube cause I’m not able to post my video here, also watch it directly on youtube, the resolution right here is very low I don’t know why)

EDIT: I forgot to mention that with spacebar we can jump, even jumping the obstacles (they don’t disappear) the FPS still drop

1 Like

Hey @Alecell!
I just saw the video. I am going to try to run the game on my Raspberry Pi4 so I’ll match your OS and to rule out the possibility that there is actually an fps drop on my HW as well however it’s powerful enough to play the scene smoothly despite the performance loss. I’ll get back to you shortly. Your English is perfect :blush: better than mine :joy: the cat in the background was cute :heart_eyes_cat:

1 Like

Try this:


EDIT: This is temporary, we need to do it another way I am just curious whether this helps.

1 Like

It just works!!!

Also tested if I jump all the obstacles (without the splice), and doing this the FPS don’t drop too!

What an interesting thing, maybe something happens on the babylonjs that if I try to update a disposed object it leads to that FPS loss :thinking:

Thank you very much @roland!!

Also thank you @Blake

You guys help me a lot :grin:


The quick hack I’ve provided has a flaw. It works now because you have only 1 obstacle on the scene at a time. If you add more it will skip one obstacle in the forEach loop because splice() removes the array entry and reindexes the array immediatelly.

  1. you have to use a for loop from the last entry to the first, this way it will not skip any entries


  1. you can use this solution which I like better. Solution 1 will be a little bit more performant but I’m not really keen on classic for loops and you’ll end up with a few obstacles on scene anyway and it really doesn’t matter in this case.
  constructor(private scene: Scene, private player: Mesh) {
    const toRemove: number[] = [];
    this.scene.onBeforeRenderObservable.add(() => {
      const animationRatio = this.scene.getAnimationRatio();

      this.obstacles.forEach((obstacle, idx) => {
        obstacle.position.x += -0.05 * animationRatio;

        if (this.player.intersectsMesh(obstacle)) {

      this.obstacles = this.obstacles.filter(
        (_, i) => toRemove[i] === undefined
      toRemove.length = 0;

A disposed mesh is simply disposed and you can’t rely on a disposed mesh or any disposed object to behave normally. So this is expected. That’s what I was writing about earlier and that’s what @blake was trying to remind of.

There is a mesh.isDisposed() function available so if you are not sure whether your mesh is disposed at the time you are accessing it for any reason, use it.

Glad to help dude! @blake thanks for participating!