# Collision detection with scrolling 2d

## Recommended Posts

wizardpc    282
i got my scrolling tiles to work. i have a jump button. but im confused on how to create collision detection. i tried just checking all the possiblities of tiles around my character but that didn't work. anyone with a solution to my problem that can help me would be very aprecieted. thank you

##### Share on other sites
jyk    2094
Can you tell us a little more about the game and the environment? Tiles suggest top-down 2d, but then how does jumping work?

Given a little more detail, it should be fairly easy to suggest an appropriate collision detection method.

##### Share on other sites
wizardpc    282
here is some of my code

the game is a side scroller like mario i have the map in a text file that looks like

0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1

etc. so i have the file loaded into a 2d array. called map[][];

sPlayer is my sprite stuct with a x and y cord.

here is some source code for my input / jumping/ functions.

BOOL cApp::Frame(){	  // Get input  m_Keyboard.Read();  m_Mouse.Acquire (TRUE);  m_Mouse.Read();	if(m_Keyboard.GetKeyState(KEY_UP) == TRUE)		if(jump == false)		{			startjump = true;			jump = true;			yAccel = -20;		}	if(m_Keyboard.GetKeyState(KEY_RIGHT) == TRUE) 	{                //512 is half screen, so the screen moves when the sprite is half way on the screen		if(sPlayer.x > 512)		{									if(mapx < 3068) // 64 * (size of tile) - 4 for movement				mapx += 4;			else				sPlayer.x+=4;				if(sPlayer.x > 4032)					sPlayer.x = 4032;								}		else			sPlayer.x += 4;	}	if(m_Keyboard.GetKeyState(KEY_LEFT) == TRUE)	{						if(mapx >8)		{						mapx -= 4;					}		else			if(sPlayer.x > 0)				sPlayer.x -= 4;				if(sPlayer.x < 0 )					sPlayer.x = 0;	}   	Gravity(sPlayer.x+mapx, sPlayer.y);		//took out rendering code for length reasons but the drawing code is map_draw			map_draw(mapx, 0);  if(m_Keyboard.GetKeyState(KEY_ESC) == TRUE)       return FALSE;   	return TRUE;}int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,                             LPSTR szCmdLine, int nCmdShow){  cApp App;  return App.Run();}void cApp::map_draw(int map_drawx, int map_drawy){	   int i, j;   int mapx, mapy;   int map_xoff, map_yoff;   mapx = map_drawx / 64;   mapy = map_drawy / 64;   map_xoff = map_drawx & 63;   map_yoff = map_drawy & 63;   for(i = 0; i < 12; i++) {      for(j = 0; j < 17; j++)         tTiles[map[mapy +i][mapx +j]].Blit( j * 64 - map_xoff, i * 64 - map_yoff);   }}void cApp::Gravity(int map_drawx, int map_drawy){	bool collided = false;	int mapx = map_drawx / 64;	int mapy = map_drawy / 64;	int movingy = 0;	if(yAccel <0 )	{		yAccel += .3;		movingy += yAccel;	}	movingy +=8; 	if(!startjump)	{		if(map[mapy+1][mapx] == 1)			collided = true;		if(map[mapy+1][mapx+1] ==1)					collided = true;	}  	if (collided == true)	{		movingy = 0;		jump = false;	}		startjump = false;	sPlayer.y += movingy;}

im using directx and c++ if you didn't notice. the collision detection works fine on the gravity function. but obviously when my sprite jumps the up and right and left can cut though the blocks that are suppose to be solid(ex. brickwall)

##### Share on other sites
jyk    2094
I don't have a definite answer, but just a couple of things you could look at. I've seen people link to a side-scroller/platform collision detection tutorial of some sort here, but I'm not sure what or where it is. Maybe someone else can provide the link, or you could track it down through google. Also, I posted some AABB collision code here. Although the code is untested, it might give you some ideas about how to perform collision detection and response in your game.