Code:
$Box Glitch Fix [tauKhan]
0404C98C 41800090
C204C988 00000004
98030034 C00285C4
FC030040 C00285F0
40800008 D0430008
60000000 00000000
Box glitch is an interesting collision detection glitch where a character becomes trapped "between" a floor, and a ceiling that is
below that floor, unable to move vertically from the floor. The glitch occurs once a character comes into contact with such a floor and a ceiling simultaneously. Once the simultaneous contacts have been made, the ECB top corner of the character gets pushed
below the ceiling while the character is placed
above the floor.
To understand why that happens, one needs to look at a concept I've dubbed "ECB Squeezing":
When a character is in a tight gap between a ceiling and a floor, such that contact is made with both, the game needs to make a choice on how to act. The collisions have opposing effects: the floor collision would push the character through the ceiling, while the ceiling collision would push the character through the floor. The solution Melee chooses in the instance is to place the character where the floor collision would leave it. By itself that would cause the ECB to clip through the ceiling. That is unwanted as it would allow the character to jump through the ceiling, for instance. So in addition, the game squeezes the ECB to fit into the gap, by lowering the ECB top corner to below the ceiling. (as visible in the picture)
The ECB squeezing always happens once a simultaneous floor and ceiling contacts occur. Thus the ECB top is lowered under the contacted ceiling even when that ceiling is below the contacted floor. ECB top being placed below a ceiling under a floor creates a state that fuels itself: The ECB tries to regain it's normal shape with ECB top attempting to return to it's normal elevation. That of course causes a ceiling contact, as the ECB top was just below a ceiling previously. The ceiling contact negates any possible upward movement, and also causes a downward displacement. Naturally that downward displacement leads to a floor contact, as the character (and it's ECB bottom) was just above floor previously. The floor contact negates any downward movement. And, both floor and ceiling contacts were made simultaneously, and thus the ECB top squeezes to below the ceiling once again, completing a loop.
The code fixes the glitch by adding a special case to the ECB squeezing function (The function is located at 0x8004C91C in NTSC 1.02): ECB corner changes are skipped if and only if the contacted ceiling was below the floor. Then, a simultaneous contact with a floor and a ceiling below that floor simply lead to the character being moved onto the floor.
The omission of that special case in Melee is understandable, as the requirements of contacting both a ceiling and a floor at the same time would appear to be contradictory. Contacting a floor requires a line from previous ECB bottom to current ECB bottom to go through a floor. Hitting a ceiling in turn requires ECB top to cross the ceiling in same manner from below. ECB top is (without squeezing) always above ECB bottom, so it seems impossible to have the lower ECB bottom move down through higher boundary, while the higher ECB top moves through the lower boundary.
The secret to initiating the box glitch lies in a
third simultaneous contact: if a character moves from above a floor, then collides with a wall that's facing a ceiling below the floor, then the char can hit both the ceiling and the floor at once.
The main collision logic function looks for wall contacts first. Then as it detects a contact on a wall, it'll determine a new target destination for the character, where the characters ECB would no longer clip through its surroundings. This is where a ceiling contact is noticed, and the target destination is set low enough that the ECB top would fit exactly under the ceiling.
All of that is done
before floor contact is checked. Since the previous ECB bottom was above the floor, and the current one is below, a floor contact is still made.
Since both floor and ceiling contacts were made, the game proceeds as if the character is between a ceiling and a floor. The character is placed above the floor, and the ECB top is lowered below the ceiling, initiating the box glitch.
It is also possible to trigger box glitch from the opposite direction, by moving up through a ceiling to on top of a floor above that ceiling, and hitting a wall that's connected to the floor. That version works pretty much exactly same as moving down below a ceiling: the wall contact makes the game detect a floor below the character, in that case. Ceiling collision detection also happens after the wall collision, thus that works the same way.