How to determine what is in front or not in a 2D world?


(Linck) #1

Take a look at this situation:

Imagine this is a 2D game, and the red areas on the base of the objects are their collision.

How would I order the layers, so when the players are arround the objects, they appear in the right layer?

So it would look like this:

Some players are behind the objects, and some are in front. But I cant just order them with their Y position, because assuming the origin of the objects are in the top left corner, if I did that, the player in the middle would be in front of the object, even though he is not supposed to.


(JP Mortiboys) #2

Make the origin of the objects their base - between the feet for humanoids. Set the layer property to -y on each update; it will work fine.

Oddly-shaped things might not work properly; trial and error will help here - or cutting them up into smaller objects.


(Linck) #3

Thanks @NotARaptor. I’m actually doing something like:

if(this.y + 200 > player.y){
	this.layer = player.layer - 4;
}
else{
	this.layer = player.layer + 4;
}

On each physic object in my map.

As I don’t see any better solution for these oddly shaped things as you said, for now I’m just setting the collision in a way so that nothing weird happens, or doing a lot of hardcoded “ifs” to deal with the layer of the object.


(rostok) #4

Check out my post: Isometric problem

I think you should reposition entity’s center and set layer based on -y coordinate like NotARaptor suggested. If you will have a lot of objects update layer only when object will move. This fires setter and may have slight impact on performance.

Below a screenshot with objects in similar isometric environment. Green squares are entities centers, while red are hitboxes. However for collisions I used custom code based on convex polygons.


(Linck) #5

The game I’m developing is not a perfect isometric game. It’s exactly like this one:

It’s a messsed up perspective.

So…

You can see that dividing this object in the middle, the player in the left is behing the image, because it is above the separation line, even thought it is not supposed to. Some similar issue happens with the player in the right. Even if I drew a line in the very bottom of the base, or in the very top, either one side of the object or another would have an ordering issue.

My solution for now:

I have found out that no metter the complexity of the base of the object represented by an image, if its convex, It can be subivided in 6 parts, drawing one vertcal line and two horizontal. The vertical one needs to be somwere in the middle, and the two horizontals must be in the same Y as one of the points of the polygon. Doing that, you can then define which parts will make the player stay behind, and which ones will make him be in front.

To implement that, I intend to create a txt or csv file for each object, containing the X of the vertical line, and both Ys of the two horizontals, and a boolean telling if, between the two horizontal lines, the area to the left is in front or behind.

How do you like it?


(Linck) #6

Wrong…

I didn’t put enough thought on it. Complex polygons would need to be much more subdivided. Something like this

Still doable I guess.