Game Development Community

TorqueX3D beta - 2d Objects disappearing, solution.

by Joshua A. Thomas · in Torque X 2D · 01/10/2008 (5:04 pm) · 16 replies

Many have been noticing this problem in the beta where the engine only seems to render a certain number of 2d objects(80 to be specific). I recently discovered the solution to this problem. Set your scenegraph's UseLayerSorting property to false and that will correct your problem.

The thread that turned me on to the problem.
www.garagegames.com/mg/forums/result.thread.php?qt=69640


I just wanted to make the answer more accessible instead of it hiding in a thread titled Purple Screen. enjoy.

#1
01/10/2008 (7:58 pm)
Excellent idea to make it more visible... and again, great find!!!

John K.
#2
01/10/2008 (9:27 pm)
Just curious, does the UseLayerSorting property refer to the use of layers for rendering? I am relying pretty heavily on the layers (I have the 30 layers mapped out) and hope that by turning it off, everything wont drop to one layer !! Good catch Josh!
#3
01/11/2008 (10:42 am)
Torque still sorts the objects in the scenegraph by layer before rendering. This property affected the LayerDepth of a sceneobject, which is used to set the objects Z coordinate during rendering. From what I could tell in version 1.0.5.1, Z was always 0.

While I was testing, my project had 2 layers. In that case the object on layer 1 was always behind layer 0. Feel free to test that out. I don't have any large projects going at the moment.
#4
01/11/2008 (11:08 am)
It doesn't work for me. If I set the property UseLayerSorting to false, my T2DStaticSprites are are no more sorted.

I used 3 differents layer values (0, 1, 2).
Any clues ?
#5
01/11/2008 (12:24 pm)
Thanks Edwin.

Ok, _useLayerSorting was checked before soring by layer first, I missed that. So the only option then might be to comment out the line I had a problem with. It was at or around 342 in T2DScenegraph.cs(I added comments while I was investigating). Do not set UseLayerSorting to false.

sceneObject.LayerDepth = _useLayerSorting ? i - count : -(float)sceneObject.Layer;

I'm still looking for alternatives.
#6
01/11/2008 (12:47 pm)
OK... This also accomplishes the same result, assuming you don't have more than 10000 objects in your scene. No source editing. Again, UseLayerSorting is TRUE.

In BeginRun()
T2DSceneCamera cam = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
cam.FarDistance = 10000f;

Question. Does changing the FarDistance affect performance?
#7
01/12/2008 (2:36 am)
It works Joshua !
I don't know if it affect performance because my project is at the beginning

Thank you very much.
#8
03/24/2008 (4:05 pm)
Hey, aren't the cases reversed in the line:

sceneObject.LayerDepth = _useLayerSorting ? i - count : -(float)sceneObject.Layer;

Shouldn't it be
sceneObject.LayerDepth = _useLayerSorting ? -(float)sceneObject.Layer : i - count;

I mean, if you are using layer sorting, shouldn't the layer depth be based on the layer???

With that change, you don't need the "set UseLayerSorting to false" or "cam.FarDistance = 10000f;" solutions to the greater than 80 objects problem.

BTW, in the case where LayerDepth gets set to i-count, that seems weird to me. Why not just 0 for all (when not sorting)?
So maybe it should be:
sceneObject.LayerDepth = _useLayerSorting ? -(float)sceneObject.Layer : 0;
#9
05/17/2008 (1:52 pm)
Quote:Question. Does changing the FarDistance affect performance?

No, what it does is it makes the resolution of the depth buffer less useful. This is a minor digression from 2D games, but the way this is all working is TorqueX is being drawn using DirectX, which is a 3D game engine. As a result, we have access to the effect of a depth buffer. What that does is keep track of the nearest pixel to the camera that has been drawn for each pixel on the screen, and not allow any farther away pixels to be drawn over it. It does this by checking the z-coordinate of polygons that hit pixels.

NearDistance and FarDistance are the smallest and largest z-values that the depth buffer can represent, using whatever storage format the buffer is set up for. Anything farther than FarDistance or nearer than NearDistance will not be drawn. The disadvantage to pushing FarDistance away (which shouldn't be too much of a problem here) is that a change of, say, 1 unit in the z direction will correspond to less of a change in depth buffer value (since the range Near-Far is scaled to [-1,1] in the buffer). It becomes a problem when two objects wind up with the same numerical depth value, even when one should be in front of the other, because then you can't reliably have them drawn correctly.

Quote:I mean, if you are using layer sorting, shouldn't the layer depth be based on the layer???

I'm pretty sure layerSorting means sorting within a layer, so that there's always a consistent order, and sorting across layers is always on. That is to say, layer 0 is always in front of layer 1, but useLayerSorting means that consistently object X will always be in front of object Y, even though they're both in layer 0. I don't have the source, but the one line you linked indicates that to me.
#10
05/17/2008 (2:23 pm)
Here's a relevant comment in T2DSceneGraph._SetupProjection:

// the far distance needs to be greater than the total number of objects on screen at once
// since each object is assigned a depth value based on it's sort order in the scene

So it seems that the intention is for us game developers to set the camera's FarDistance to some number greater than however many objects are in the scene?

If so, this should probably be included in a 'generic game' tutorial or set of instructions for setting up your game. All this time I was thinking FarDistance = 10000f was a hack, but it appears to be an intended step for setting up a game. Though 10,000 is probably overkill. The default seems to be 1000... I'm pretty sure I don't have 1000 objects in my scene but sprites disappear when I zoom if I don't set FarDistance first.
#11
02/19/2009 (6:06 pm)
Thanks for this.
#12
07/06/2009 (2:50 am)
If a ton of objects are needed on the screen, just adding 1.0f to the far distance everytime a dynamic object is created seems to work.
#13
07/07/2009 (9:57 am)
Of course, of you actually have 10000 objects on screen at a time, I think depth buffering will be the least of you problems as it crawls along at 1/100 frame per second :)
#14
08/23/2009 (11:03 am)
Just wanted to say a big thank you for solution. Was on the verge of giving up and going back to raw XNA because I couldn't work out what I was doing wrong
#15
01/06/2010 (9:42 am)
I'm having problem implementing this solution to my Torque X 2D project. I must mention that I'm just starting to get into this (a.k.a. n00b) so this might seem like a silly question, but

how do I do this:
"Set your scenegraph's UseLayerSorting property to false"?

Is that something I can do from Torque 2D Builder?
Can I do it in code (I dont have full source code)?
Can I do it in BeginRun() of Game.cs?
I cant seem to find this Scenegraph property, but I feel like I'm looking in all the wrong places.

I've found T2DSceneGraph tag in levelData.txscene and adding "<UseLayerSorting>false</UseLayerSorting>" there
solves the problem (all textures appear) but that line get overwriten every time I save in Builder.
#16
04/11/2010 (6:49 am)
@Jakov...you should be able to do:

TorqueObjectDatabase.Instance.FindObject<T2DSceneGraph>("DefaultSceneGraph);

To get a handle to it. Then you can set its properties. Sadly enough...this does NOT work for me. I still have disappearing sprites.

--RB