1
\$\begingroup\$

Im developing a 2D game with some fake 3D effects (parallell projection). 2D-units are able to walk in front of buildings aswell as behind them, although themselves being just 2d-objects. This leads to a problem I will show you with a series of images (1-5). Can you tell me how to solve it?

Image 1: Animation for objects used in this example: Unit and Building.

enter image description here

Image 2: Unit and Building with animations and the physical bodies used in the game engine. Note the extruding arms and the building overhang enabling units to walk behind it.

enter image description here

Image 3: By using rendering order ground(0), units(1), buildings(2) the overall goal is reached. But as you can see the limbs of the unit is hidden by the building. This is my problem. How to solve it?

enter image description here

Image 4: I have tried splitting the building in a "front side" and a "back side" for rendering, but that leads to another issue - the corners. (Notice the penetrating hand on the rightmost unit.)

enter image description here

Image 5: So the final quick fix which I would rather avoid is this method: Enlarged buffered physical buildings and shortened unit limbs.

enter image description here

Please, what are youre ideas on how to solve the issues? If it matters, the game is continious and not built on grids or discrete values.

\$\endgroup\$
  • \$\begingroup\$ If your units have arms that extrude outside of their collision volume, intersection with other objects is unavoidable. From what I gather, that's not a problem to you. The problem is that the character should always be completely in front or completely behind a building. Am I getting this right? \$\endgroup\$ – Anko Aug 5 '14 at 11:45
  • \$\begingroup\$ Correct, collisions are not a problem. Though when walking behind a building the arms might not be visible, or only partly visible. They should also not be able to penetrate the way they do in image 4. In picture number 3 the arm of the unit in front of the building should not be hidden. Hope this helps understanding? \$\endgroup\$ – Anders Aug 5 '14 at 11:52
2
\$\begingroup\$

You need to enable some kind of z-buffering: first render unobstructed units, then buildings, then units that are obstructed (complete or just partially) by buildings and then the ground. If you render them in this order make sure no pixel is overwritten: do not draw over a pixel that has already been drawn, else only the terrain will render. Flush the buffer when beginning the render process and once a pixel is assigned a value, do not write over it until you flush the buffer again.

By using rendering order ground(0), units(1), buildings(2) the overall goal is reached. But as you can see the limbs of the unit is hidden by the building. This is my problem. How to solve it?

If you really want to overwrite the pixels (that's what you do from what I can tell), then you need to render them in the inverse order, but it isn't very efficient since some pixels will get assigned some values that will get overwritten by other values. So, the rendering order is ground(0), obstructed-units(1), buildings(2), unobstructed-units(3).

You now need to know if a unit is obstructed or not. You can check the circle of every unit with the square in my image. If they collide then mark the unit as obstructed.

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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