So I'm implementing an isometric sorter for my sprites and I'm having some issues with the comparison of when the tiles should be rendered. I'm sorting all the isometric sprites that will be rendered by implementing them as comparable.
The problem is, when I'm implementing the following compareTo method:
// 1 = render this after
// 0 == render same
// -1 = render this before
@Override
public int compareTo(IsoSprite o) {
if(z >= o.z && maxY <= o.minY && maxX <= o.minX){
return 1;
}
if(z >= o.z && maxY >= o.minY && maxX >= o.minX){
return -1;
}
if(z > o.z){
return 1;
}
if(z < o.z){
return -1;
}
//z == o.z && maxY == o.maxY && minY == o.minY && minX == o.minX && maxX == o.maxX
return 0;
}
I get the error "Comparison method violates its general contract!" from the array.sort call in the LibGDX Array (which I use for sorting). I can't tell how I am supposed to solve this when looking at other peoples issue with this error, but those problems are mostly trivial. Anyone know how I should solve this in my isometric comparison?
My isometric world (for reference):
Edit: Found something interesting when only sorting by Z:
//Doesn't work
public int compareTo(IsoSprite o) {
if(maxZ > o.z){
return 1;
}
if (maxZ < o.z){
return -1;
}
return 0;
}
//Works
@Override
public int compareTo(IsoSprite o) {
if(z > o.z){
return 1;
}
if(z < o.z){
return -1;
}
return 0;
}
I realised I won't be able to do the comparisons needed in a comparable. So instead I'm using my own implementation of Quicksort to sort using my own compareTo method that basically checks if a sprite is behind or infront of another one.
Thanks for all the help anyway!