Jump to content

Recent Comments

0 user(s) viewing

0 members, 0 guests, 0 anonymous users

- - - - -

Flickering overlapping objects - Common bug

4: Adsense

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.

Jun 21 2014 05:33 PM

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.

Note: GameDev.net moderates comments.