Object Sorting
Last updated
Was this helpful?
Last updated
Was this helpful?
Many games require 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 (and other objects) overlap.
There are a number of methods of controlling overlapping . Keep in mind that these methods are not just used for , but for as well. and also use many of the methods mentioned below. These methods are:
Setting Z value
Using the depth buffer (also known as the z buffer)
Using
Using multiple
Setting the Z value of objects is an easy way to control sorting of objects. Of course, this method is mostly used when the is in its default, unrotated state. and 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 is not , 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 . 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 is (also known as a 2D ), 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 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 both on the ground, you may set the player's Z to 0 and the grass to -.0001 so that they are drawn in front of the player.
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.
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 .
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.
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 , and objects which are ordered by their Z values sort properly with objects that modify the depth buffer. can optionally modify the depth buffer as well. For information on this, see the article. The object cannot modify the depth buffer - but objects that modify the depth buffer will sort properly with . can use and modify the depth buffer depending on the state values that are set in their Draw method.
can be used to sort all types of objects. Objects on will always draw on top of objects that are not on any layers, or objects that are on lower (unless multiple are used, which we'll get to in the next section). Objects within a will use Z ordering and the depth buffer to sort, so the provides a hierarchical form of ordering. 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 article includes a considerable amount of information on how it can be used to perform sorting, so for more information, .
FlatRedBall supports multiple Cameras. These cameras can be used to create split-screen views, or can be overlapped to result in one drawing over another. Camera order is the highest-level form of controlling now objects sort. That is, an object on a second will always draw on top of an another object that is drawn by a first regardless of Z position, depth buffering, or layer membership. For more information on how to use multiple , see the .
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 or . added to the are added as world . are specific to the they are added to. To move objects to specific , call AddToLayer on the appropriate manager for the objectt to be moved.