A little problem..... (Rendering)

So I am playing around, testing stuff etc… And I saw this :

First one is like okey, but i walk down and WTF?
I am using slick2D, Code:



private Player p;
private StrangeMan sm;

public void init(GameContainer c, StateBasedGame game) throws SlickException {
	sm = new StrangeMan(300,300);
	p = new Player(100, 100,AnimationHandler.Player);
}


public void render(GameContainer c, StateBasedGame game, Graphics g) throws SlickException {
	g.translate(-p.getX()+c.getWidth()/2-p.getWidth()/2,-p.getY()+c.getHeight()/2-p.getHeight()/2);
	p.render(c, game, g);
	sm.render(c, game, g);
}

The question : How can i make my player, when walks in front of the man, render/draw/whatever in front?

I had this problem this morning in fact! this is how i did it and i hope you can learn from this snippet


	public void renderEntitys(Screen screen,Player p) {
		HashMap<Integer, ArrayList<Entity>> newList = new HashMap<Integer, ArrayList<Entity>>();
		Rectangle r = new Rectangle(p.x-200,p.y-100,400,200);
		for (Entity e : entitys) {
			if(r.contains(e.x, e.y)){
				if(newList.get(e.y)==null){
					ArrayList<Entity> a = new ArrayList<Entity>();
					a.add(e);
					newList.put(e.y, a);
				}else{
					ArrayList<Entity> a = newList.get(e.y);
					a.add(e);
					newList.put(e.y, a);
				}
			}
		}
		
		for(int i = p.y-100;i<p.y+100;i++){
			if(newList.get(i)!=null){
				for(Entity b : newList.get(i)){
					b.render(screen);
				}
			}
		}
	}

What this dose is renders all entities that are on a higher y value first before the onces infront, giving a illusion of forced prospective like this

I need an a explenation i really don’t understand how would i port that? xD

You’re always rendering the strange man after the player, that’s why he’s being drain “on top” of the player when really the player should cover the strange man in the second picture.

To fix it, sort all the entities you’re going to draw by their y-position and draw them in that order, i.e. the higher up on the screen the earlier they should be rendered.

Ed’s implementation would be much faster using a comparator to sort the list of entities before rendering instead like so (snipet from my game code):


		entities.sort(Entity.ySorter);
		for (Entity e : entities) {
			AABB b = e.getBoundsForRendering();
			if (b != null) {
				if (b.intersects(cam.getViewportBounds()))
					e.render();
			} else {
				e.render();
			}
		}

Entity.ySorter is as follows:


	public static SortByY ySorter = new SortByY();
	
	public static class SortByY implements Comparator<Entity> {
		public int compare(Entity o1, Entity o2) {
			return (o1.getY() > o2.getY()) ? -1 : (o1.getY() < o2.getY()) ? 1 : 0;
		}
	}

Better comparator:


public static final Comparator<Entity> rev_YOrder = new Comparator<Entity>() {
    public int compare(Entity a, Entity b) {
        return Integer.compare(b.y, a.y);
    }
};
Collections.sort(entities, rev_YOrder);

// or, if java8:
Collections.sort(entities, (e1, e2) -> Integer.compare(e2.y, e1.y));