This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Detecting overlap with a "special" tile type

Detecting overlap with a "special" tile type
by on (#153137)
When doing object-to-background collision checks against solid blocks, it's usually sufficient to check for collisions only at the leading edge of the bounding box. By "leading edge" I mean the edge in the direction of movement.

What about when we want to detect overlap with some special tile type, such as water? The leading edge check can detect when the object enters such a tile, but we would still need to check the trailing edge to see when the object leaves the special tile. Alternatively we could scan tiles under the entire bounding box of the object to see which tile types it's overlapping.

So, which method is better? I think scanning the entire bounding box is easier to understand, but probably a tiny bit more inefficient. Maybe it doesn't matter much if the bounding box is small (as it usually is), though. Are there other things to consider here?

One other thing that comes to mind is that if special tiles are detected during ejection from a solid tile, they should be ignored, because the post-ejection position should be what matters.

EDIT: A really pedantic/generic implementation might also need a counter that is increased on enter and decreased when leaving (when counter is 0, not overlapping). Otherwise the object could possibly leave a special tile while still overlapping another special tile. EDIT2: Or maybe this isn't needed if the trailing edge check is only done if leading edge isn't still overlapping (like tokumaru suggested).
Re: Detecting overlap with a "special" tile type
by on (#153140)
I thing you could have flags indicating the contact with different types of special blocks, which would get set when the leading edge of the bounding box touched them. To come out of these states, you'd check the trailing edge, but only when the leading edge isn't touching special blocks anymore, so it doesn't impact performance much.

For water in particular, it would actually make sense to have one point (which makes it dead simple to check the block types) within the object used to define whether the object is submerged or not. Maybe a point near the chest or the neck, so you could still have shallow pools of water without slowing down the physics.
Re: Detecting overlap with a "special" tile type
by on (#153144)
tokumaru wrote:
For water in particular, it would actually make sense to have one point (which makes it dead simple to check the block types) within the object used to define whether the object is submerged or not. Maybe a point near the chest or the neck, so you could still have shallow pools of water without slowing down the physics.

Good point (...) that a simple point check can be enough for some of these cases!
Re: Detecting overlap with a "special" tile type
by on (#153491)
tokumaru wrote:
I thing you could have flags indicating the contact with different types of special blocks, which would get set when the leading edge of the bounding box touched them. To come out of these states, you'd check the trailing edge, but only when the leading edge isn't touching special blocks anymore, so it doesn't impact performance much.

I ended up doing pretty much this. The flag is always set when the leading edge enters a special tile. If leading doesn't enter a special tile, the trailing edge is calculated based on the unmodified position to find out whether the trailing edge was on top of a special tile before movement. If not, no further checks are necessary. If it is, another overlapping check is made with the modified (moved) trailing edge, and if it doesn't overlap the special tile the flag is unset. This second check could be skipped if the modified trailing edge doesn't move into a new tile. Obviously all of the trailing edge checks can be skipped if the flag is unset at the time.

I guess it might be possible to forego the unmodified trailing edge check if the code kept track of the overlap state of each edge separately. But that's not trivial either because movement in any direction could affect the state of every other edge. It might be useful to know the state of each edge for some gameplay stuff, though.
Re: Detecting overlap with a "special" tile type
by on (#153773)
Now I'm trying to think whether there's any situation where a simple point check wouldn't suffice in practice. All of the scenarios where I can think of an overlap check being useful in the first place (vines/ropes, water, doors, ...) seem like they should work acceptably with a point check. :)
Re: Detecting overlap with a "special" tile type
by on (#153780)
That's perfect!
Re: Detecting overlap with a "special" tile type
by on (#153937)
tokumaru wrote:
That's perfect!

Where is this coming from? :)

If you're implying that it was stupid to come up with a "perfect" solution for a (possibly) non-existent problem, then all I can say is that I agree with you. That said, I'm constantly trying to find the balance between solutions that are too specific and solutions that are over-generalized, since I'm not working on anything concrete (with a more limited problem space) at this time.
Re: Detecting overlap with a "special" tile type
by on (#153951)
I just meant it's great that you can make it all work using the simplest possible solution. There was no hidden meaning or sarcasm in my message, it was just good to get confirmation that the common cases of this problem can be solved without too much complexity.

I was probably posting from my phone, so I ended up not writing much, sorry if I sounded weird.
Re: Detecting overlap with a "special" tile type
by on (#153956)
Ah, such a short message seemed uncharacteristic coming from you, which was why I wasn't sure what you meant with it.