githubEdit

Object Sorting

Introduction

Many games require Spritesarrow-up-right to overlap to create realistic scenes. FlatRedBall offers a number of ways to control sorting. This article will discuss and link to various methods of controlling how Spritesarrow-up-right (and other objects) overlap.

What are the methods?

There are a number of methods of controlling overlapping Spritesarrow-up-right. Keep in mind that these methods are not just used for Spritesarrow-up-right, but for Textsarrow-up-right as well. DrawableBatchesarrow-up-right and PositionedModelsarrow-up-right also use many of the methods mentioned below. These methods are:

Setting Z Value

Setting the Z value of objects is an easy way to control sorting of objects. Of course, this method is mostly used when the Cameraarrow-up-right is in its default, unrotated state. Spritesarrow-up-right and Textsarrow-up-right are, by default, created as "ordered" objects. This means that FlatRedBall sorts these objects by their Z values every frame, then draws them "back-to-front" so that objects in back are overlapped by objects in front. This method of sorting is very common and is acceptable for many games. Often if the Cameraarrow-up-right is not orthogonalarrow-up-right, then the Z value can also be used to place objects in the distance, making them smaller due to the natural perspective of a 3D Cameraarrow-up-right. In this case, the Z value has the dual purpose of making objects seem like they are in the distance due to on-screen size changes as well as controlling the sorting. If the Cameraarrow-up-right is orthogonalarrow-up-right (also known as a 2D Cameraarrow-up-right), then the Z value won't impact the on-screen size of an object, but it will still impact its sorting. In these cases, the Z value is used purely to control sorting. It is also common when using a 3D Cameraarrow-up-right to use very small differences in the Z value between objects to force a particular sorting. For example, if you are making a 2D platformer which has a player and grass Spritesarrow-up-right both on the ground, you may set the player's Z to 0 and the grass Spritesarrow-up-right to -.0001 so that they are drawn in front of the player.

Objects with the same Z

If two objects have the same Z value, their sorting priority is undefined. While there technically is a method to how FlatRedBall picks the order, this order is subject to performance optimizations and can vary between different versions of the engine. The bottom line is, you should never rely on the particular sorting order that you see for two objects that have the same Z value. If you require a particular sorting order, enforce it by explicitly setting different Z values, or by using one of the other sorting methods mentioned below.

Sorting on Y

Many top-down games have objects that should overlap each other depending on their Y positions. This functionality is built-in to FlatRedBall. For more information see the SpriteManager's SortYSpritesSecondary article.

Using the depth buffer

The depth buffer can be used when more complex sorting is needed. Simple sorting by Z values always results in an object being drawn in full over all objects which are behind it. It is impossible to have two objects which intersect each other draw properly without using the depth buffer. The depth buffer is automatically used on PositionedModelsarrow-up-right, and objects which are ordered by their Z values sort properly with objects that modify the depth buffer. Spritesarrow-up-right can optionally modify the depth buffer as well. For information on this, see the AddZBufferedSpritearrow-up-right article. The Textarrow-up-right object cannot modify the depth buffer - but objects that modify the depth buffer will sort properly with Textsarrow-up-right. DrawableBatchesarrow-up-right can use and modify the depth buffer depending on the state values that are set in their Draw method.

Layersarrow-up-right can be used to sort all types of objects. Objects on Layersarrow-up-right will always draw on top of objects that are not on any layers, or objects that are on lower Layersarrow-up-right (unless multiple Camerasarrow-up-right are used, which we'll get to in the next section). Objects within a Layerarrow-up-right will use Z ordering and the depth buffer to sort, so the Layerarrow-up-right provides a hierarchical form of ordering. Layersarrow-up-right are also effective in situations where you want to override the natural sorting performed by Z position or the Depth Buffer. For example, you may want a game's HUD to always appear on top of other objects, but you may also have a game where objects move freely towards the camera (if your game is 3D, or if you are using 3D particles). In this situation, Layers can guarantee that the HUD will never be covered by any other objects. The Layerarrow-up-right article includes a considerable amount of information on how it can be used to perform sorting, so for more information, click herearrow-up-right.

Using multiple Camerasarrow-up-right

FlatRedBall supports multiple Cameras. These cameras can be used to create split-screen views, or Camerasarrow-up-right can be overlapped to result in one Cameraarrow-up-right drawing over another. Camera order is the highest-level form of controlling now objects sort. That is, an object on a second Cameraarrow-up-right will always draw on top of an another object that is drawn by a first Cameraarrow-up-right regardless of Z position, depth buffering, or layer membership. For more information on how to use multiple Camerasarrow-up-right, see the Multiple Cameras articlearrow-up-right.

Diagram

The following diagram shows the order in which objects are drawn. Categories in the same vertical space will sort with each other (such as Depth Buffer and Coordinate Sorting) while categories which appear above will draw on top of categories below. For example, layered objects will always draw on top of unlayered objects.

This diagram can best be read bottom-up. The very bottom section (Camera 0, Unlayered) is where objects are added by default when added through managers like the SpriteManagerarrow-up-right or TextManagerarrow-up-right. Layersarrow-up-right added to the SpriteManagerarrow-up-right are added as world Layersarrow-up-right. Layersarrow-up-right added to Camerasarrow-up-right are specific to the Cameraarrow-up-right they are added to. To move objects to specific Layersarrow-up-right, call AddToLayer on the appropriate manager for the objectt to be moved.

Last updated

Was this helpful?