Jump to content

  • Log In with Google      Sign In   
  • Create Account





Flickering overlapping objects - Common bug

Posted by Arthur Souza, 21 June 2014 · 1,723 views

I have recently been playing Don't starve, and have come to find a common bug that happens in many 2d games. It's something a little hard to come by when test developing.

Sometimes, two objects that slightly overlap have a flickering effect. This happens because one object is being drawn in front of the other at a time.

The common cause for this is the sprite sorting behavior not having a fixed decision for when two objects have the same sorting variable.

Let's assume whats usual, that you are using your Y value, vertical position, as the means to sort what object is in the front. When two objects have the same Y value, you have to take a decisive action to make sure that every time the sorting is called, the same result will be wielded.

Assume that:
A.Position = { x:5, y = 10}
B.Position = { x.7, y = 10}
And your sorting method is:
function sort(obj a, obj b){
     return a.Y > b.Y;
}
The result of the sorting based on Y will basically be decided on what object you first passed into the function. This may cause flickering.

In this case, you have to setup a second decision variable. My recommendation is to simply, if both Y values are equal, use the X value to decide what object is drawn first. I may post an example showing the bug and the fix.




I traditionally have used the object ID as a tie-breaker, giving front priority to older objects with smaller IDs. Since I always assign the player object an ID before other objects, this means that in case of ties the player is drawn in front.

PARTNERS