Prime wrote:
Hi tokumaru
Very unusual way to start a topic, since not all members read everything... luckily I read almost everything that isn't SNES, music, repros or mods!
When in doubt... DRAW! Here's a character (blue rectangle) running right into a wall, inside a tiny 3x3 metatile area:
Attachment:
collision-right.png [ 4.91 KiB | Viewed 3131 times ]
Since the object moved left (something we know because the horizontal displacement was positive), we have to check all the blocks from the top right corner ($24, $04) to the bottom right corner ($24, $1F), to know whether they're solid or not. Dividing by 16, we have to look at all blocks between (2, 0) and (2, 1). If any of those blocks are solid, you'll know the object has hit a wall, which is the case here.
Now we have to figure out how many pixels to push the object back. Regardless of where the object's hospot is, and how you calculated the rightmost horizontal coordinate, you can easily tell just from looking that this coordinate is 5 pixels into the wall. Math confirms this: $24 AND $0f = $04, the fifth pixel of the metatile. You can now simply subtract 4 + 1 (since we count pixels from 0) from the object's coordinate and you can be sure that its right side will not be inside the wall anymore.
The math is a bit different for the left side, since objects enter the wall from the opposite side (column $0f):
Attachment:
collision-left.png [ 4.9 KiB | Viewed 3131 times ]
Now we're looking at the corners ($0B, $04) and ($0B, $1F). $0B AND $0F = $0B, which is the 12th column of the metatile, but since the object is entering from the right, that doesn't mean it's 12 pixels in. The actual value you're looking for is 16 - 12 = 4, which is the same we'd get if the object was moving right, like in the previous example. In assembly, you can use a shortcut to find this number: just EOR it with $0F. $0B EOR $0F = $04, which is the number you're looking for. Now you can add that (plus 1) to eject the object from the wall.
Does this help?