LibGdx ScissorStack Strange Glitch

I’ve been wrestling with the ScissorStack in libgdx for quite some time now. I’ve got much of it working, but now there is a really strange occurrence with the rendering. I have a Tiled map with different “chunks” of layers that the player can move between. So when a player enters an interior room in one of the chunks I’m using the ScissorStack to come in and clip out the rest of the layer so the player can’t see anything but the room they’re in. It almost works. Except one of the layers of one of the chunks bleeds through somehow. It’s a bit complicated so I have some pictures to explain what is going on.

Imgur

This first image is the player standing inside one of the rooms. You can see that the door marker and a few tiles I’ve put outside the room for reference are showing when they shouldn’t be. Each chunk contains a series of layers. The objects that bleed through are only in the foreground layer of the chunk. These are the last layers to be rendered for a chunk.

Imgur

This next image show other objects that bleed through the clipping. Again, these are only objects from the foreground layer.

Imgur

But this final image shows that the player can move just a few steps to the right and then the objects get correctly clipped. That part really confuses me.

Unfortunately, I think the problem could be in a relatively broad area of the code. Here is the basic setup of the ScissorStack. It’s written in JRuby, but I think anybody familiar with libgdx will have no problem recognizing what is happening. I’ll be happy to answer any questions if needed.


unless @player.room.nil?
                 
     @cam.position.y = @player.room.y + @player.height / 2 + 1 + @camOffset
     @roomCam.position.y = @player.room.y - @player.height / 2 - 1 - @camOffset
      
     ScissorStack.calculateScissors(
       @roomCam, @roomCam.position.x, @roomCam.position.y, @roomCam.viewportWidth, @roomCam.viewportHeight, 
       @renderer.getSpriteBatch.getTransformMatrix, @player.room, @scissor
     )
       
     scissorPos = Vector2.new(@scissor.x, @scissor.y)
 
     ScissorStack.toWindowCoordinates(@roomCam, @renderer.getSpriteBatch.getTransformMatrix, scissorPos)
       
     @scissor.x = scissorPos.x - Gdx.graphics.width / 2
     @scissor.y = 64
     @scissor.width = @scissor.width * C::BTW
     @scissor.height = @scissor.height * C::BTW
 
     ScissorStack.pushScissors(@scissor)
      
  end

This actually worked fine and clipped everything perfectly when I popped @scissor from the stack after rendering all of the chunks. But now because I’m working on transitioning between the layers I wanted to be able to apply the ScissorStack clipping to only some of the chunks and not all of them. So I moved the popping of @scissor to inbetween chunks by doing this in the render method:


if chunk.id > @curChunk.id
  ScissorStack.popScissors unless @player.room.nil?
end
      
chunk.render(@renderer)

This waits until the current chunk has been rendered and then pops the scissors before rendering the chunks in front of the current. But now I have the bleed through problems I’ve shown in the pictures. Just to show how weird this is if I place a single tile in the foreground layer that is causing the bleeding problems in a very specific spot on the map then the clipping starts working. But it has to be a in a specific spot and it has to be in the problem layer… Sorry, I know this is a complicated thing to explain. So I appreciate anyone reading this much, and especially any suggestions for what might be going wrong. This is a big project and it is slightly annoying to get hung up on something that is essentially aesthetic. Thanks guys.