Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've got a tricky issue I'm not sure how to tackle best: I have an animated tile of a door. When it's closed it should be sorted one way, but when it's openend it will need to be sorted a different way, as it belonging to a different(neighbouring tile).

Here's the door closed:

door closed

and the door opened:

door opened

I imagine it would be possible to override the sorting system for such tiles and adjust the sorting based on the frame, but it feels a bit hacky.

Has anyone encountered a similar scenario ? Any elegant solutions ?

share|improve this question
2  
How does your sorting system currently detect the draw order of tiles? –  Roy T. Jun 21 '12 at 17:45
add comment

4 Answers

Maybe you could make a door out of two objects, on different tiles, and only show one of them at a time. This way, opened door might conceivably block movement too, which seems kinda realistic.

share|improve this answer
add comment

There are 2 tricks I use for sorting in 2D:

  • Add ground position to sprites, this is a point where sprite touches the ground (f.e. mens feet, trees trunk, doors frame) and sort by it instead of sprites pixels. This way sprite will not be affected by animation

  • Add non-sortable childs to objects (f.e. men can have several childs which do not affect sorting - cloak, bow, sword, etc, door frame has child door). This way animation of child sprites does not affects sorting either.

share|improve this answer
add comment

Create three sprites...

  1. Door closed (like your first sprite) - blocks movement
  2. Just the frame (post in your case) of the door, but no door - doesn't block movement
  3. Just the door in open position - blocks movement

When the door is closed, draw sprite 1, and treat it as a "blocker" so players can't walk through it.

When the door is opened, replace that sprite with #2 (just the door post). Then place sprite #3 (just the door) in the adjacent cell that the door would be in.

Closing is just removing the door, and replacing the post with post+door in closed position.

With this setup, you don't have to worry about rendering the opened door in any special order. Just render it in the order as any other object in the square it is in.

The downside is that you can't have the player or any other object in the square where the opened door will go.

share|improve this answer
add comment

The best way to solve the sorting of objects is by not sorting at all. If you use a depth buffer, you can render everything in one pass. Objects that have a higher z-value will be further away than objects with a lower z-value.

Even if your game is 2D, you should still consider using 3D coordinates with a depth buffer, otherwise you will forever tango with the painter's algorithm.

share|improve this answer
    
True, but how to determine Z position? Only by sorting –  Krom Stern Jun 22 '12 at 6:46
    
Yes, but that sorting now happens offline, instead of during rendering. Now the level designer can build the level with the Z values in mind, without having to worry about clipping objects. –  knight666 Jun 22 '12 at 6:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.