How to render TCastleScene between two layers of Tiledmap?

@soastao Hm, I’m thinking about it.

I created GitHub issue to track it – Tiled maps: Make it possible to render a character (any TCastleTransform) in a flexible way behind or in front of a layer with walls · Issue #440 · castle-engine/castle-engine · GitHub .

I see the problem – when you want layer to behave like a wall, then you want characters to be sometimes behind, sometimes in front of it. Effectively the character should be perceived at the same depth as the tile it is standing on.

To be clear, I have ideas how to do it, but they both are uneasy for now:

  1. We could augment Z values of walls to provide space for it: Each row could be at slightly larger Z. This is a direct way to say “each row of walls if effectively at different depth, a bit closer to camera”. And then you can insert character at any Z you want. But doing this in practice requires some uneasy things:

    • It would need to wait for a blending fix I mentioned in one of the earlier posts. That is, one scene (character) would have to be rendered between 2 shapes of another scene (tiled map).
    • It would mean that each row with different depth is a different shape. OK, this is not really a problem, this is trivial – when something like RowShiftZ is non-zero, then split shapes between rows.
    • Ideally, you should not need to calculate the Z of characters yourself to make them hit perfectly between wall rows. There should be automatic mechanism for it. I was already thinking about a behavior like TCastleMapPiece that could “snap” character to a proper position of given tile - in X, Y and/or in Z.
  2. Another approach would be to render with depth testing off, and then we don’t need to care about dealing with Z. We just need to render in proper order. This is the same thing as when rendering with 2D with TDrawableImage, one just has to keep strict order – so the order “rules everything”, and Z is meaningless.

    • Again for this we need a way to “attach” a character to map tile.

This echoes what @saaf writes,

manually adjusting the character’s Z-coordinate will be quite troublesome and would need to be updated in real-time based on the Y-coordinate.

Indeed – I mean such synchronization is needed by AD 1 above, and we need to make this easy, so that you don’t need to do this manually.

I do not yet have a good answer, just acknowledging the problem. This is something for me to think about.

Ultimately we need a way to “attach” a TCastleTransform (like TCastleScene) to a map tile, and ideally it should just make it rendered at the same layer as the tile, and user should not need to “fiddle” with Z values.

I have created a GitHub issue Tiled maps: Make it possible to render a character (any TCastleTransform) in a flexible way behind or in front of a layer with walls · Issue #440 · castle-engine/castle-engine · GitHub , and I encourage to subscribe to it – I will post there about the progress.

To be clear, if you want to draw the map yourself, using TDrawableImage (inside overridden Render of some TCastleUserInterface) then you can. As we talked in thread CGE 2D game development road - #2 by michalis where I gave pointers to manual how to draw using TDrawableImage.

Although you cannot “render using TDrawableImage in TCastleScene”. TCastleScene doesn’t work like that — to put things in TCastleScene you define nodes to render in TCastleScene. So you could define the nodes, that define tiles, in TCastleScene, and you could update these nodes each frame. But it is much easier to just render everything using TDrawableImage inside a TCastleUserInterface, if you just want a 2D map.

To be clear, I admit you found a critical problem with TCastleTiledMap here. We will solve it, but this one is not so trivial that I’ll come back tomorrow with a solution :slight_smile: I advised earlier to use a built-in solution from CGE to render Tiled maps, and I think we made a good progress in TCastleTiledMap (and more will come, I hope to add animations to map this weekend, as promised on TCastleTiledMap cannot render animations - #3 by saaf ). TCastleTiledMap makes sense for some use-cases. But I absolutely admit that this is a “killer” – TCastleTiledMap cannot really be used in this situation, it cannot give you a “layer with walls created in Tiled” that will be satisfactory here. Yet. So, mea culpa – I admit I was wrong, I simply didn’t predict this problem.

But if you are ready to just do direct rendering yourself, then you can. TDrawableImage, with all optimizations (batching introduces last weekend) remains available, the Tiled map data (TCastleTiledMapData, previously it was just called TTiledMap) also remains available and you can read map from file this way.

So while we try to have a solution for Tiled maps: Make it possible to render a character (any TCastleTransform) in a flexible way behind or in front of a layer with walls · Issue #440 · castle-engine/castle-engine · GitHub in CGE, if you are prepared to render map yourself, then this is remains an option.

( And use castle-engine/src/scene/castletiledmap_control.inc at master · castle-engine/castle-engine · GitHub as a starting point. )