r/howdidtheycodeit • u/marioferpa • Jul 27 '24
How did they make characters in Stardew Valley hide behind parts of some objects?
In this screenshot you can see the character hiding behind the armrest while sitting down. I expected that the sprite was split in two, like they do with bedsheets, as you can see in the furniture spritesheet. However it seems it is not the case, because you can find the sprite for this specific armchair on the top left, and it's in one piece.
There was a similar post here about how they did this in the Sims 1, and the answer was that objects had both sprites and z-maps, and the game used that for ordering. But I haven't found anything like that in Stardew.
49
u/R4TTY Jul 27 '24
I would guess the sprite is actually split. The top portion is 1 sprite and the bottom is another. With the bottom having a higher z-index than the player.
14
u/jmaargh Jul 27 '24
There are lots of possible ways to do this (others have already mentioned some), but if I were doing it the simplest way would be just to render two sprites like with your bed example. Just in this case it doesn't look like a second entry in the spritesheet is necessary because the arm of the chair is (almost) entirely horizontal: so you can just map to the bottom half of the same sprite image to build the foreground sprite for the arm of the chair.
My guess is that the foreground sprite is just a copy of a rectangular portion of the same sprite in the spritesheet. That would be the easiest to implement.
19
u/cantpeoplebenormal Jul 27 '24
It still could have been split in code, using UV coordinates on the sprite atlas that only cover part of the chair.
Or a mask.
3
u/robbertzzz1 Jul 27 '24
The y position is used for sorting in such games. Higher up the screen means further to the back. The origin point of all objects needs to be near its base to make it work as intended, because that's the position being used. So characters have their origin between their feet, trees at the bottom center of their trunk, etc.
2
u/beautifulgirl789 Jul 27 '24
Stardew uses a 16px-based tileset. In your armchair example, there doesn't need to be a separate "front-arm" graphic, because the front-arm can re-use the bottom half of the original graphic (you'll notice the front-arm starts precisely on a 16-pixel boundary and is perfectly horizontal). It has two entries in the tile data but one points to the whole chair, while the second points only to the bottom half.
(they have to have separate graphics for other objects e.g. beds where they can't align the cut on a tile boundary).
1
u/angelosat Jul 27 '24
Sims 1 did this by having each object have 2 textures, the normal bitmap texture and a depth texture. The depth texture is a grayscale image where black pixels are closer to the camera and white pixels are further. When you have that, you can calculate exactly which parts of the object to draw in front of the player sprite through shaders.
That's the way i do it in the game i'm making.