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

Collision Logic

Collision Logic
by on (#194703)
I've been thinking about improving my code for 'what to do if collision'.

I've been doing something like...
collision left, add 8, and $f0
collision right, and $f0
(plus or minus an adjustment).

ie, aligning to the metatile.

SMB1, just does a-
collision and facing right (-1 x)
collision and facing left (+1 x)
and if you manage to face the wrong direction, you pass through the wall (well known glitch).

So I looked at a few more games. This seems to be a better approach...


collision right

LDA $0720,X @ $0720 = #$77 ;x position
ADC $D433,Y @ $D434 = #$0A ;right side offset
STA $0006 = #$81
LDA $0006 = #$81
AND #$0F
STA $0006 = #$01
LDA $0720,X @ $0720 = #$77
SBC $0006 = #$01
STA $0720,X @ $0720 = #$76 ;new x position

collision left

LDA $0720,X @ $0720 = #$28 ;x position
ADC $D433,Y @ $D436 = #$F6 ;left side offset
STA $0006 = #$1E
LDA $0006 = #$1E
AND #$0F
EOR #$0F
ADC $0720,X @ $0720 = #$28
STA $0720,X @ $0720 = #$2A ;new x position

This seems a bit complicated to me.

What do think is a good method? Is there any other approaches to this?
Re: Collision Logic
by on (#194720)
I guess they do the *(-1) to save a byte per collision rect in the ROM? the store centre pos of their objects and then +/- a fixed offset?

If you move the world but keep the player in a fixed position on the screen, they your checks just become fixed index look ups ;) Or if you have a small dead zone you can keep counters to shift the locations you need to check.

If you move around the screen, I usually convert the player pos to "tile" space, which is then aligned to POW2 and masks make it easy to pull out of the "walls", then you offset by X,Y Scroll and store back.

However sometimes I need to do multiple points to test, so while I might just do AABB checks, I sometimes also add feet and body and head based checks. Depends on what the game needs.

But then I normally don't have "meta sprites" and all my positions are stored based on TL.

Also max movement speed per frame is important. If you only move 1 or 2 px per frame, then you can do simple stuff, if you move 4-8 and you have single char walls, you have to be more advanced.
Re: Collision Logic
by on (#194725)
My "world" collision detection routine (which does need to be tightened up a lot!) is written in a way that gives me easy access to the remainder of the coordinate that pushes the object into a wall/floor. Since we're usually working with 8x8 or 16x16 collision tiles in NES games it's basically just AND'ing the low byte of the coordinate.
Depending on how your movement logic works, it's really just a matter of subtracting that value from your coordinate or velocity (if it hasn't been applied yet)

Collision detection in Super Mario Bros is terrible, to be honest. But it does have a fairly advanced movement scheme, so I won't hold it against it. :)