• entries
    34
  • comments
    102
  • views
    47381

Flickering overlapping objects - Common bug

Sign in to follow this  
arthurviolence

2740 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.
Sign in to follow this  


1 Comment


Recommended Comments

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.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now