Collision Detection - 2D side scroller

SGDK only sub forum

Moderator: Stef

Post Reply
SephirothDVR
Newbie
Posts: 3
Joined: Tue Nov 09, 2021 8:07 am

Collision Detection - 2D side scroller

Post by SephirothDVR » Thu Nov 25, 2021 6:29 am

Hi Everyone

Awesome forum! I've been getting a lot of information here but now I'm stuck and hope someone here can assist me.

I'm currently busy creating a 2D side scroller (like Shinobi, Sonic etc).
I've worked through the Sonic example in the samples folder of SGDK and I've worked through this tutorial: https://under-prog.ru/en/sgdk-creating- ... a-genesis/ for collision detection.

The tutorial creates a 2D array

Code: Select all

u8 level[row][col] //size of whole stage
and each value in the array represents a tile on the stage. The values are 0 for blocking and 1 for allowed movement. Then when the code that moves the sprite runs, it first checks if the sprite is going to move into space it is allowed to:

Code: Select all

for(s16 i=leftTile; i <=rightTile; i++)
{
	for(s16 j=topTile; j <=bottomTile; j++)
	{
		if(level [j][i] == 0) {
			return FALSE;
		}
	}
}
return TRUE;
leftTile, RightTile, TopTile and bottomTile is just the corner tiles of the sprite.

I tried then to apply the collision detection to the Sonic example and ran into the problem that I cannot create a 2D array for a stage that is 160 x 1280 tiles big. If I create the array as a global variable the code does not compile and if I create it inside my stage_1() function then I get an "illegal instruction" error once the stage tries to load.

What I found interesting is that I can create the array and even have the following:

Code: Select all

if(level [j][i] == 0) {
	//return FALSE;
	int dummy = 0;
}
And the code will compille and game will run, I'm guessing the compiler is clever enough to realise the if statement does not do anything and just skips compiling that code into the ROM?

So my question is: What is the right way to code collision detection in 2D side scrollers that have stages that are bigger then just a screen or two's width?

I am assuming this is a RAM space issue, how can I then load the array into RAM for a big stage? My code is failing even with a 30 x 100 array (which is very small for a stage in any case)

cero
Very interested
Posts: 306
Joined: Mon Nov 30, 2015 1:55 pm

Re: Collision Detection - 2D side scroller

Post by cero » Thu Nov 25, 2021 7:56 am

You don't load it to RAM. You keep it in ROM.

SephirothDVR
Newbie
Posts: 3
Joined: Tue Nov 09, 2021 8:07 am

Re: Collision Detection - 2D side scroller

Post by SephirothDVR » Thu Nov 25, 2021 8:52 am

Surely when the CPU needs to access the array to check for collision the array must be loaded into the RAM?

Or am I not understanding this correctly?

SephirothDVR
Newbie
Posts: 3
Joined: Tue Nov 09, 2021 8:07 am

Re: Collision Detection - 2D side scroller

Post by SephirothDVR » Thu Nov 25, 2021 1:40 pm

So it seems I tried to be too clever and just made life difficult for myself. Currently my project consists of an INTRO state and GAME state.
INTRO includes a SEGA logo screen, introduction section and title screen. if start is pressed while the cursor is on "Start" then state is changed to GAME and stage_1 is loaded.

Up until this point everything was working well, I loaded the sonic example as my Stage_1 and even made it that if Sonic reached the end of the level that it should transition to stage_2 (There is no Stage_2, I just made it transition to the beginning of the INTRO state)

Then when I tried to add collision detection, I encountered so many issues. My biggest mistake was thinking it is due to the large stage and maybe I'm not using the right way of doing collision detection.

I started fresh today, got the sonic example compiling and then just added the collision detection and everything is working! and running smoothly!
Here is sonic standing on a ledge that is programmed as a solid ground! :D :D
Image

I need to look at my initial code structure and find out what is going wrong there that is causing my game to crash. But I'll create a new thread once I have tried to find the issue and am stuck once again.

Post Reply