Sw3das
Sw3das

Reputation: 157

LibGDX - Displaying player in fron or behind the object

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( );
}

Upvotes: 0

Views: 2324

Answers (2)

Sachin Gorade
Sachin Gorade

Reputation: 1467

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

Upvotes: 1

m.antkowicz
m.antkowicz

Reputation: 13581

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()

Upvotes: 0

Related Questions