It is currently Tue Jan 22, 2019 5:25 am

All times are UTC - 7 hours

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Collision Logic
PostPosted: Sun Apr 30, 2017 6:45 pm 
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 2392
Location: DIGDUG
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?

_________________ -- blog/tutorial on programming for the NES

 Post subject: Re: Collision Logic
PostPosted: Mon May 01, 2017 12:01 am 

Joined: Tue Feb 07, 2017 2:03 am
Posts: 644
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.

 Post subject: Re: Collision Logic
PostPosted: Mon May 01, 2017 12:32 am 
User avatar

Joined: Thu Sep 15, 2016 6:29 am
Posts: 855
Location: Denmark (PAL)
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. :)

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 7 hours

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group