0

I'm creating a game where you can to all four directions ( up, down, right, left ) and I've encoutered a problem: I can't find a way to make player to be displayed behind the object if player is behind that object.

While I was doing it totally top-down style, it was ok. But now I'm trying to add some "3D" effect to it ( see images below ).

And one more thing - I'm making so that when the player enters a house, the full house look ( the full height wall and roof ) disappears and only the layout of the wall ( where is the actual collision layer ) is beeing shown( I haven't done that yet, but I have a pretty clear vision of how I'm going to implement that ).

To make it more clear, I'm gonna give some images as examples:

When player is below object - everything is ok!

In front of the house In fron of tree

But when player get's behind object - things start to become pretty weird :D

He should be behind it... On the roof On the tree

Anyone knows how to put the player behind the objects which he should atcually be behind ?

EDITED:

@Override public void show( )
{
    /** Initializing InputProcessor **/
    Gdx.input.setInputProcessor( this );

    /** Creating map **/
    tiledMap = new TmxMapLoader( ).load( "maps/medievalPlace.tmx" );
    mapRenderer = new OrthogonalTiledMapRenderer( tiledMap );

    /** Initializing Camera **/
    camera = new PlayerCamera( ( TiledMapTileLayer ) tiledMap.getLayers( ).get( 0 ) );

    /** Initializing spriteBatch **/
    spriteBatch = new SpriteBatch( );
    spriteBatch.setProjectionMatrix( camera.combined );
    spriteBatch.maxSpritesInBatch = 1;

    /** Initializing uiBatch **/
    uiBatch = new SpriteBatch( );
    uiBatch.maxSpritesInBatch = 5;

    /** Creating Player **/
    player = new Player( new Texture( "char/down/walk0.png" ) );

    // Initializing font for FPS displaying //
    font = new BitmapFont( );
}

private void renderScreen( )
{
    Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );

    /** Rendering map **/
    mapRenderer.setView( camera );
    mapRenderer.render( );

    /** spriteBatch rendering **/
    spriteBatch.begin( );

    player.draw( spriteBatch );

    spriteBatch.end( );

    /** uiBatch rendering **/
    uiBatch.begin( );

    arrow_right.draw( uiBatch );
    arrow_left.draw( uiBatch );
    arrow_down.draw( uiBatch );
    arrow_up.draw( uiBatch );
    action.draw( uiBatch );

    font.draw( uiBatch, "Fps: " + Gdx.graphics.getFramesPerSecond( ), 100, Gdx.graphics.getHeight( ) - 100 );

    uiBatch.end( );
}
Community
  • 1
  • 1
Sw3das
  • 157
  • 1
  • 12

2 Answers2

1

One more way would be to extend the OrthogonalTiledMapRenderer and handle the character rendering while rendering the layers. I found this post when I was facing the same problem.

http://www.gamefromscratch.com/post/2014/05/01/LibGDX-Tutorial-11-Tiled-Maps-Part-2-Adding-a-character-sprite.aspx

Sachin Gorade
  • 1,427
  • 12
  • 32
  • This is what I'm doing and I can confirm it's quite easy. You simply add all of Tiled's objectsprites that you want to be able to walk behind and in front of to a list. Then you add your playersprite and any other entitysprites to the list as well. Then you use a comparator and you order all the sprites in that list using their Y coordinate. Then make it so that sprites with a higher Y coordinate get rendered first, which will make them appear behind sprites with a lower Y coordinate value. – Tipsi Dec 19 '19 at 18:14
0

the easiest way is just to render all objects that player can go behind after you are rendering player so it should looks like

Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );

/** Rendering map **/
mapRenderer.setView( camera );
mapRenderer.render( );            //all "background" things here (player is above them)

/** spriteBatch rendering **/
spriteBatch.begin( );

player.draw( spriteBatch );

for(Sprite sprite : firstPlaneSprites) //array with objects player can be behind
sprite.draw( spriteBatch );
spriteBatch.end( );

but it will need to be handled to create front objects in a special way (I assume you are using Tiled or something ike this to render map)


the second idea little harder is to cut off the parts of player sprite when you enter a "front" area using TextureRegion. The steps should be

  • check how much you have to cut from top and create a TextureRegion tr1 from original texture
  • check how much you have to cut from bottom and create a TextureRegion tr2 from tr1
  • check how much you have to cut from left and create a TextureRegion tr3 from tr2
  • check how much you have to cut from right and create a TextureRegion tr4 from tr3
  • render tr4

you should add some invisible objects that will tell you if you entered "front" area (the area your character get behind)


at least you can use Scene2d and its Image actor class - this would be the best but you would have to change type of creating like everything in your application. The way you would use it is simple like the first idea but you would do not have to render it just to set actors position using toFront() and toBack()

m.antkowicz
  • 13,268
  • 18
  • 37
  • The first method is not going to work, because my player can be behind or in front of the same object, at diffrent time, of course. Even if it would work, it would take too much resources to do the calculation, in my opinion. But the Scene2D thing looks promising. Gonna try the tutorial that *EssEllDee* gave to me. – Sw3das Sep 27 '15 at 14:15
  • I'd rather think about dividing object for two pieces - one that character can be above (like wall of the house) and second that character can be behind (like roof) and rendering them separetely. Is there even en object that character can be once behind and another time above? – m.antkowicz Sep 27 '15 at 14:52
  • Yes, the tree, for example. Once player is lower than the tree - the player is in front. Once the player is higher than the tree - he should be behind the tree. – Sw3das Sep 27 '15 at 17:19
  • Not behind tree but a part od tree - leafs. You can divide it – m.antkowicz Sep 27 '15 at 17:34