Game Development Community

Sort Points and Z fighting

by Matthew Hoesterey · in Torque X 2D · 07/15/2009 (6:48 pm) · 6 replies

Heya, I'm having some issues with zfighting. I'm making a game where characters can jump up to higher platforms. When doing this other player characters may be on a higher platform and I'd like the character jumping up to be in front of the character when they are below them.

I succeeded by setting my layer to sort by Y.

The problem I'm having is that when the characters are on the same platform if they overlap they zfight all over the place.


I tried modifying the engine as follows:

in:
private class YAxisComparer : IComparer<ISceneContainerObject>
        {
            public int Compare(ISceneContainerObject x, ISceneContainerObject y)

as a test I changed this
return y1 < y2 ? -1 : y1 > y2 ? 1 : 0;

to this:
return y1 < (y2 -20) ? -1 : y1 > (y2 +20) ? 1 : 0;

My theory was that this test would cause the characters not to zfight when on the same platform as there hight would be the same and thus this class would return 0 (and not change the characters sort order) but no luck.

Any ideas how to fix this?

#1
07/17/2009 (11:18 am)
Not the best method, but you could try setting the sort point slightly off on the Y axis for one of the two sprites so they won't overlap when on the same plane? At least that'd get rid of the Z fighting and the end user probably isn't going to notice. I'm not at home, so I can't check, but doesn't the sort point use a float? If it does, it could be a miniscule amount... just enough so they don't argue on the same plane.

Brian
#2
07/17/2009 (12:46 pm)
Ya I was thinking about that. though they are clones so I'd have to detect the 2nd at runtime and move the sort point (I think that will require engine modification as well though as I think the sort point position is currently read only. have to varify that at home though)

Good Idea though something I can look into.
#3
07/17/2009 (1:22 pm)
There should be a way to do it. Since the sort point can be edited in TXB2D, I'm guessing there's also a way to do it in code though you might have to find where it gets it out of the XML (hopefully it doesn't turn out to be read only). Also, you might have to do an offset calculation with the CopyTo function for the component if you can edit sort points since you're using templates. Not sure though. I'll look at it when I get home since I'm still at work.

Brian
#4
07/19/2009 (11:46 am)
Any luck with this one?

Brian
#5
07/20/2009 (8:59 pm)
Haven't had a chance to dig in on this agian. I'll update you in a day or two though.
#6
07/26/2009 (11:42 am)
Heya Brian, Your right I found that SortPoint is writable on the t2dsceneobj level.

One strangeness the value doesn't seam to be by pixel.

Here is the line of code that fixed the problem :).

On player 2 spawn before the scene object is registered
_player2.SortPoint = new Vector2(_player2.SortPoint.X, _player2.SortPoint.Y -0.015f);