[libGDX] How to draw an outline around sprite when behind another image

Hello

I’m developing an Isometric game in libGDX with Tiled maps. I’ve set different layers for different things in Tiled. Basically, i want to draw an outline around the player sprite(texture region) when it goes behind some Tiled tiles. It’ll help to see the player when it is behind a large object. So,

• How to detect the player sprite is behind some tiles or an image? (All objects are drawn in Tiled)
• How to draw an outline around the player sprite which is visible through the blocking object?

1 - I think this is what you want: http://www.gamefromscratch.com/post/2014/05/01/LibGDX-Tutorial-11-Tiled-Maps-Part-2-Adding-a-character-sprite.aspx

2 - Maybe you can draw a rectangle Shape (not filled) around the sprite and only draw when you are behind some tile/image. Also, you can use pixmap to know exactly which pixels are behind the image.

To me, that effect usually looks like some layering trickery so the outline is only rendered on the areas being occluded, kind of like a mask using the occluding object as the visible area.

But I’m not really sure, and I’m also looking into it.

Another solution which allow you to configure your tiled map to have this feature

http://gameover.co.in/2014/04/perspective-with-2d-files-map-in-libgdx/

Basically this is what I’m looking for (Age of Empires 2! yay!)

Does libGDX allow you to disable depth testing? I assume no, but if it does then you can do a find edges on your sprite disable the depth test and then draw the edges over all the objects.

Edit: you don’t even need to disable depth testing, if you do a find edges on your sprite then you can render the non edge parts as transparent. Just make sure to draw the outline last and only if there is an object in front.

you could use a fairly hacky frag shader or just continually render an outline above the other layers.

I get the drawing part but how do i detect my player is behind an object?

I’m still trying to find a solution to this. Any help would be greatly appreciated.

First create a texture with just has the same outline as the sprite. Then define a boolean variable in the class and call it background. Every frame when you are updating the entity, set that variable to false. Then in the collision detection, set it to true if it collides with any other entity. Finally when rendering, draw the sprite normally, and if the variable is true, render the outline.

There is a small caveat here, that still the outline will be in the background. To prevent that, keep the entity position and the flag in the game, and render the outline texture once you have rendered all the entities, making the outline texture be drawn after everything is rendered.

@SHC: then the outline also shows on non-occluded parts of the sprite. Flipping the depth-func in the ‘post-processing’ step would do the trick.

Yeah I forgot that he only needs the occluded part of the outline to be rendered. You are right Riven.