don't click here

How does the tile layer/plane system work in games past Sonic 1?

Discussion in 'Engineering & Reverse Engineering' started by foobles, Oct 9, 2022.

Tags:
  1. foobles

    foobles

    Member
    18
    6
    3
    I know that Sonic 1 had only one layer for tiles -- which is why there are no places in the game where paths cross over themselves (except loop-de-loops, which as far as I know are treated specially by the physics engine to work properly).

    But, from various quotes here and there, I've come to understand that there are 2 "layers" that tiles can be on in all the genesis games after the original, yet I can find no clear explanation for how they work.

    I did see in the debug mode for S3&K that at the top of loop-de-loops, there are invisible objects which are ostensibly used to switch Sonic's plane when he crosses over it in either direction, but what if he were to somehow jump over the loop, and then try running in from the opposite side? He would not have crossed that object, yet the game still seems to function correctly here. How does Sonic's collision work in respect to these 2 layers? How does the game know when to switch which layer he collides with?
     
  2. Blue Spikeball

    Blue Spikeball

    Member
    1,780
    574
    93
    I believe that's because the path switcher extends beyond the loop. In some levels it's possible to go over path switcher, breaking things. Emerald Hill for instance, especially as Super Sonic.
     
    Last edited: Oct 9, 2022
  3. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    This is actually not correct.

    You see, it doesn't merely just swap the path ID as if it were a switch, but rather it sets the path ID based on which side of the path swapper Sonic touches, and you can manually set the path ID per side (you can even have them set the same path ID).

    Let's take this for example:
    [​IMG]

    2 path swappers: 1 in the loop, and 1 at the end of the loop. Both are set to set Sonic to path 0 when he touches it from the left, and path 1 if he touches it from the right. When he first goes through the loop from the left, when he gets to the top, he'll be touching the right side of the path swapper, and thus get set to path 1. At the end, he'll then touch the one at the end from the left side, and get set to path 0. When he moves back towards the left, he'll then touch one at the end from the right side and end up on path 1, and then when he climbs up the loop, he'll touch the other one from the left side, and end up on path 0. You can actually see how the path swappers operate based on the path IDs and arrows labeled in SonLVL here.

    Here's another example, with the path IDs flipped:
    [​IMG]

    Moving from the left, Sonic will touch the path swapper and end up on path 1. Path 1 in this case is set to allow Sonic to climb the loop. When Sonic then hits the other path swapper, he'll touch it from the right side and end up on path 0, and be allowed to run downwards path the loop. The path swapper extending past the top of the loop allows you to enter an alternate path past the loop. The one that's in a horizontal orientation... no clue what that's about, but you get set to path 0 when you hit it from the bottom, so it's not like that it matters or causes any problems, but still kinda weird.

    In the end, no matter if he goes through the loop or skips it, the path swapper is set up to make sure he ends up on the right path with this setup.

    Path swappers can be used for more than just loops. Take this section:
    [​IMG]

    All the path swappers are set to set Sonic on path 0 if he touches it from the left, and path 1 if he touches it from the right, which is needed for this set up. Path 0 is set for all the slopes facing right, and path 1 is set for all the slopes facing left. If they merely just toggled the path ID like a switch, then what would happen would be that Sonic would touch the path swapper, swap paths, hit the spring, touch the path swapper again... and end up on the path he originally was. But, since that's not the case, this setup works.

    There's all kinds of neat things you can do with these path swappers, especially for secret alternative paths.
     
    Last edited: Oct 9, 2022
    • Like Like x 2
    • Informative Informative x 2
    • List
  4. saxman

    saxman

    Oldbie Tech Member
    This is a good explanation, but it's missing one key point I think is also important (unless I missed it). A path switcher can be configured (and is configured so in the case of loop-de-loops) to only activate if the player is crossing it while upside-down. That means if you're first approaching the loop and jump while crossing the path switcher from left to right, it won't activate. It's only activated when you're at the top, upside-down. That prevents breaking the loop.

    Not all patch switchers operate that way though. Outside of loop-de-loops, I don't think many do.
     
    • Informative Informative x 3
    • List
  5. foobles

    foobles

    Member
    18
    6
    3
    Thank you all for your explanations -- this all clears up a lot!

    So even though these are all the same object, they have configurable length, properties of which side switches to which layer, when they trigger, etc.?

    And can a single tile exist on both paths at once (for instance, the tiles near the springs in the Chemical Plant switchback area)?
     
  6. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    They have a configurable length and you can set which paths are set per side.

    The way the 2 collision paths actually work is actually due to how the collision flags work for 16x16 blocks. Each block gets a top solid flag and a left/right/bottom solid flag. In Sonic 1, it was just those 2, but for Sonic 2 and 3K, there's actually a duplicate of those flags for the 2nd collision path. So, in the end, it's just a matter of setting the appropriate collision flags for the corresponding collision paths. Yes, this means that setting the flags to be the same between the 2 collision paths will get you the desired result.

    Since you mentioned that specific section, here's how the collision flags are set between the 2 collision paths.

    Path 0:
    [​IMG]

    Path 1:
    [​IMG]

    Oh yeah, I did miss that. Yeah, if you set bit 7 in the subtype, then the path swapper will only be activated if Sonic's Y flip flag is set (in all orientations).
     
    Last edited: Oct 9, 2022
    • Informative Informative x 2
    • List
  7. Blue Spikeball

    Blue Spikeball

    Member
    1,780
    574
    93
    Sorry if I was unclear. I was addressing the question of how the characters switch paths properly even if they go over the loops. I meant that in those cases, it's usually because the levels are designed with the path swappers extending beyond the loops. You can see it in AIZ1 -- the swappers on the right side of the loops go all the way to the ceiling.

    In levels that don't do this like EHZ, it's relatively easy to skip the path swappers and break things.
     
    Last edited: Oct 10, 2022
  8. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    I see what you mean.
    [​IMG]

    However, it should be noted in this case that the path swapper on the very right is spaced out, allowing some wiggle room to fall within the gap. The one on top of the loop forces Sonic to go on path 1 if we moves towards the gap, so that if he falls through it and tries to go up the loop, he'll still be on the right path ID. Also, of course, the rightmost path swapper extends upwards to account for potentially falling into that gap from the right side.

    Mushroom Hill has a similar set up with a gap between the loop and rightmost path swapper, but the solution for accounting for that is different:
    [​IMG]

    Here, the rightmost path swapper DOESN'T extend past the top of the loop, but rather, the top of the gap is covered with a path swapper that sets Sonic to path 1 if he falls through it.

    All in all, their approach to the path swappers in this case was to try to contain a section of the loop in a certain collision path. It's especially apparent here:
    [​IMG]

    The path swappers are probably spaced out to account for some edge case.

    Regarding being able to break Emerald Hill as Super Sonic, I did find one instance where you could do that, but it's not for the reason you described:
    [​IMG]

    Here, there is actually an additional path swapper that extends from the top of the loop, to account for the one of the top right setting Sonic to path 1 if he passes through it from the right side. However, as Super Sonic, you can jump over the path swapper on top of the loop and remain on path 1 on the left side of the loop, which is not what's supposed to happen.
    [​IMG]

    There's also some path swappers that are placed at the end of loops that don't reach up high enough:
    [​IMG]

    It is actually possible to clip through the gap between the loop collision and the path swapper and not activate the path swapper.
    [​IMG]

    It's also possible to just jump right over these shorter path swappers once you are set to path ID 1 inside the loop, even as regular Sonic:
    [​IMG]

    In this particular section, path 1 actually blocks off the hidden tunnel after it:
    [​IMG]

    Here's another quirk:
    [​IMG]

    You can hit that spring as Super Sonic, touch the right path swapper from the right and end up on path 1... and skip over the path swapper at the end of the corkscrew that's supposed to take you to path 0 from the right side. On path ID 1, collision looks like this in this section:
    [​IMG]
    [​IMG]

    There's also loop towards the left where you can also jump over the path swapper at the end:
    [​IMG]
    [​IMG]

    In act 2, there's also this section:
    [​IMG]

    Right by coconuts, there's a path swapper that sets Sonic to path ID 1 if he touches it from the left, which causes the hidden tunnel to the right to be blocked off. It's also possible to use the spring there as Super Sonic to jump over the path swapper next to the one next to the Coconuts and remain on path 1, causing the bottom path to be blocked off until you switch back to path 0:
    [​IMG]

    I did talk about this section when explaining how the path swappers worked, but there's also an issue:
    [​IMG]

    The path swapper that's supposed to set Sonic onto path 1 when he goes over the loop could be jumped over from the right. Remember that the left side of the loop is path 1 and the right/bottom side is path 0. Touching the path swapper at the top of the loop from the right will set you to path 0, and block off the loop:
    [​IMG]

    In fact, looking at the path swapper that causes this, I'm starting to think that the parameters for it were accidentally mixed up.

    Last one, this time in Aquatic Ruin:
    [​IMG]

    You can go through the first loop, end up on path ID 1, jump on top of the second loop, jump to the top of the first loop, and jump over the path swapper that resets the path ID back to 0, and block the first loop off.

    [​IMG]
     
    Last edited: Oct 9, 2022
  9. foobles

    foobles

    Member
    18
    6
    3
    Wow! Thank you for such a detailed set of examples, this is amazing!

    Where did you get the images for the placement of the path switchers? Did you make them yourself?
     
  10. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    SonLVL and the Sonic 2 disassembly
     
  11. Xiao Hayes

    Xiao Hayes

    Classic Eggman art Member
    Then, how do the loops work on Sonic 1? My fault if I should know because I already knew most of what has been said here, but I still don't know. :eng99:
     
  12. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    Sonic 1 actually marks which chunks are loops and uses 2 separate chunks that gets swapped when Sonic runs through it.

    [​IMG]
    [​IMG]
    [​IMG] [​IMG]

    Sonic 1 only allows up to 2 chunks to have this property per zone, though, so it's very limiting. Green Hill just uses one, but Star Light uses both.
     
    Last edited: Oct 10, 2022
    • Informative Informative x 2
    • Like Like x 1
    • List
  13. Xiao Hayes

    Xiao Hayes

    Classic Eggman art Member
    I see, so they more or less "hacked" the first game so those chunks behaved similarly to the real thing they implemented in the second game. The usual dev thing of "let's see if we can first, and we'll do it right later if so".
     
  14. Lostgame

    Lostgame

    producer/turnablist. homebrew dev. cosplayer. Oldbie
    4,130
    54
    28
    Toronto, ON
    The O.I.C.
    Oh, wow; this is very cool, I wasn't aware this was how it was handled!
     
  15. foobles

    foobles

    Member
    18
    6
    3
    Looking over some of these examples again, I am noticing that it must not be *only* the [top solid] and [sides/bottom] solid flags that are swapped; are the height/width maps themselves also swapped when sonic enters different paths? Like at the point where a loop crosses itself, the tile there must have data for when you are going around the circle, and for when you are passing through. So is it swapping between 2 maps the same way it swaps between 2 different sets of flags?
     
  16. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    Nope. It just switches which collision flags it checks. Some tiles just use the same collision flags for both collision paths.
     
  17. foobles

    foobles

    Member
    18
    6
    3

    upload_2022-10-9_23-26-32.png upload_2022-10-9_23-27-25.png

    In that case, then how does the collision seem to change its entire height map in the section I highlighted? Are there actually 2 different planes of tiles that are just overlapping, and some get activated/deactivated by Sonic's current mode?
     
  18. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    Wait, I am stupid. Yeah, I forgot that there's actually 2 sets of collision block IDs. My bad.
     
  19. foobles

    foobles

    Member
    18
    6
    3
    So for each tile in the map, there are actually 2 different tile IDs, which refer to different structures containing {width map, height map, path 0 flags, path 1 flags}?
    Or is there only 1 tile ID per tile, and it refers to a single structure containing {path 0 height map, path 0 width map, path 0 flags, path 1 height map, path 1 width map, path 1 flags}?

    And in either case, let's say both overlapping blocks had active collision for Sonic's current path. Would it check both of them? Or just the one corresponding to his current path?

    Thanks again for all your very helpful answers :)
     
  20. Devon

    Devon

    There's nothing left but faith Tech Member
    854
    572
    93
    For each 16x16 block in a chunk, it has 2 sets of flags and and 2 IDs that correlates to a "collision block". The Genesis games each use a global bank of what I call "collision blocks", each containing the width/height maps.

    This is the bank in Sonic 2:
    [​IMG]

    Here, I have this one block selected:
    [​IMG]

    As shown here, each collision path has their own individual collision flags:
    [​IMG]

    Which is set inside the chunk data, so you can reuse a block, but have different flags set.

    As for just the block itself, it has 2 collision block IDs, taken from the collision block bank. Here's what they look like:
    [​IMG]

    Now, the collision block IDs are also directly tied with the block IDs, so unlike the collision flags, you cannot just swap them out for a block; you'd have to make a duplicate block that uses a different set of collision blocks.
     
    Last edited: Oct 10, 2022
    • Informative Informative x 2
    • List