Game Development Community

How to set rendering Z index?

by Jason McIntosh · in Torque Game Builder · 02/27/2005 (6:53 pm) · 10 replies

How can I specify a layer (or object in a layer) z index to sort the rendering order?

#1
02/27/2005 (7:03 pm)
Two ways, it seems:
1. The order in which you create fxStaticSprite2D's seem to be their hardcoded Z-order within a given layer
2. Use the %sprite.setLayer command (0 - 31) to force the z-order you'd like for an object
#2
02/27/2005 (7:04 pm)
Just do .setLayer( # );

Layer 0 is the topmost layer, all the way to layer 31. Thats how I put a background sprite, then have the foreground sprites floating on top of it. Background for me is .setLayer(1); foreground is .setLayer(0);
#3
02/27/2005 (7:08 pm)
Ah, ok, I was thinking setLayer( 0 ); would be the bottom-most layer, thus my confusion. Thanks guys!

Edit: (I think this qualifies as a "doh!" moment.) :)
#4
02/28/2005 (1:17 am)
Lol. It has been added to the list ;-)
#5
02/28/2005 (1:28 am)
Really?

When playing with the Map Editor, layer 0 is the default layer. When you add a new layer, it appears on top of it, making layer 0 the bottom.

So this behaviour is reversed when dealing with sprite layers (as opposed to map layers)?
#6
02/28/2005 (1:37 am)
@Philip: The layers as referenced in the tile-editor don't really relate to the rendering layers in T2D. When I wrote the tile-editor, I though that it was best to have the first layer you define be the one at the back and then as you add layers, they appear over the top (in front). The layers in the editor are just logical tile layers and don't relate to the T2D scene layers.

I can totally understand how confusing this is and I'm more than happy to entertain suggestions for alternative naming etc.

An important thing to note is that the tile-editor only currently allows you to setup the tilemap/layers. It doesn't currently give you the ability to setup their rendering layers, positions, sizes etc. When you save the map, it will be saved in the state that the editor used it so you need to setup the tile-layers when you load them. At some point the tile-editor will have these kinds of scene-setup screens, possibly even setting stuff like auto-panning etc.

All stuff to come.

- Melv.
#7
02/28/2005 (2:23 am)
OK, that's cool. Just another example of something I need to learn :) I'll get there though!
#8
02/28/2005 (4:31 am)
More waffle from me...

This is really getting into what I'd categorise as the advanced side of T2D but there's actually some fine-grained control over Z order.

Oh what the hell, let me tell you about it...

First, as mentioned above, you can set the discreet rendering layer using "setLayer()", this defaults to layer#0. If you generate lots of objects in the same layer, T2D has a default for the ordering here but it isn't fixed. Like most things in T2D, it can be changed and done so in scripts.

If you repeatedly add objects to the same layer, the last object you add will be in front. If you want to manipulate an objects position within a layer or even within layers and then across layers, you can use a couple of functions hanging off the scene. These are "setLayerDrawOrder()" and "setSceneDrawOrder()". Both these calls allow you to move the object forward and backwards or send to front or back. The first will adjust the objects postion within the layer only and will not adjust the layer. The second does the same thing but will also move the object through layers as well (if needed when moving backwards/forwards). Look for more details in the reference document under fxSceneGraph2D.

Okay, now we're into the advanced side of things...

As I mentioned previously, when you add an object to a scene, the object will appear in-front of everything else in that layer but this can be changed. This setting though has a restriction you should be aware of. The restriction here is that the scene must be empty when you change it (it will be cleared if not) and therefore it's something you configure up front and not use dynamically.

To do this, you define a scenegraph datablock (fxSceneGraphDatablock2D) which has four parameters. You can find these parameters in the reference document. The defaults for these parameters currently are:-

"containerBinSize" = 4.0
"containerBinCount" = 128
"useLayerSorting" = true
"lastInFrontSorting" = true

Ignoring the first two parameters, the last two are what we're interested in.

The "lastInFrontSorting" field controls whether the last object added is in the front of the layer or not. Setting this to fault means that subsequent objects appear behind (but within the same layer) previous objects.
The "useLayerSorting" simply ignores the previous setting and objects added to the scene are not sorted either in-front or behind. Why do this? Well, there's a little overhead in doing the sorting although not that much and this allows you to turn the sorting off.

I did tell you this was the advanced section right? ;)

Finally then, if I wanted to setup something like this to have the sorting opposite the default, I'd use something similar to the following:-

datablock fxSceneGraphDatablock2D(myFlippedSceneDatablock)
{
	lastInFrontSorting = false;	
};

// Create fxSceneGraph2D.
new fxSceneGraph2D(t2dSceneGraph) { config = myFlippedSceneDatablock; };


Okay, I really should get back to work now, hopefully this clarifies some quite advanced features of T2D though.

- Melv.
#9
02/28/2005 (4:52 am)
Thanks, Melv. This is exactly what I was looking for. Don't be shy about getting into advanced details with us! :)
#10
02/28/2005 (5:10 am)
@Jason: Cool. Just want to warn people that I'm about to frag their brains. :)

- Melv.