#### Archived

This topic is now archived and is closed to further replies.

# 2d collision detection

This topic is 6086 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how do i do 2d collision detection? hmmm interesting

##### Share on other sites
Hi I will explain one of the simplest ways to implement sprite to background and sprite to sprite collision.This code included below is from a RPG engine I am developing for Game Boy Advanced.
Well here I go.

first you want to declare dimensions of your map
#define MAP_WIDTH 15
#define MAP_HEIGHT 10

now declare tile size
#define TILE_WIDTH 16
#define TILE_HEIGHT 16

//variables for player coordinates
int g_x=107,g_y=80;

declare collision map where 1 is wall and 0 is passable
int collmap[15*10]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,0,0,0,0,0,0,1,1,0,1,1,
1,0,0,0,0,0,0,0,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,1,1,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,
1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
};

actual collision detection function
just put your sprite coordinates in arguments
int Collide(int x,int y)
{
//variables for old coordinates and grid index
int px,py,ox,oy;

ox=x;oy=y;

px=x/TILE_WIDTH;
py=y/TILE_HEIGHT;

if(collmap[px+py*MAP_WIDTH]==1)
{
x=ox;y=oy;
return 1; //return this if collision occured
}

ox=x;oy=y;
return 0;//return this if no collision occured
}

now this is where you update players position and check for collision

void ProcessInput(void)
{
int speed=1;

//if collision occured undo corrdinate increasement
g_y-=speed;
if(Collide(g_x,g_y)>0)g_y+=speed;
}

g_y+=speed;
if(Collide(g_x,g_y)>0)g_y-=speed;
}

g_x-=speed;
if(Collide(g_x,g_y)>0)g_x+=speed;
}

g_x+=speed;
if(Collide(g_x,g_y)>0)g_x-=speed;
}

}

that was pretty simple now for sprite to sprite collision
first I would define a struct to hold dimension data for sprite

typedef struct tagRECT {
int left;
int top;
int right;
int bottom;
}RECT;

RECT player,enemy;

enemy coordinates

int ex,ey;

this isnt really necessary but this is my way of doing things

void Update()
{
//player size
player.left=g_x; player.top=g_y;
player.right=player.left+16; player.bottom=player.top+20;

//enemy size
enemy.left=ex; enemy.top=ey;
enemy.right=enemy.left+16; enemy.bottom=enemy.top+20;
}

now this fucntion test for collision and is used like Collide() function above

int CollideSprite(RECT* r1,RECT* r2)
{
Update();

//this checks for non-collisions and anything else
//means coordinates collided

if(r1->bottom < r2->top) return 0;
if(r1->top > r2->bottom) return 0;
if(r1->right < r2->left) return 0;
if(r1->left > r2->right) return 0;

return 1;
}

NOTE: remeber collision detection is just checking if two spaces\planes and or coordinates overlap. Dont make things more complicated then they have to be. I hope this helps if not I can write more detailed tutroial.Anyway post reply lettin me know how things went or email me at Royale00@hotmail.com

##### Share on other sites
thanks it''s worked . Now i''m going to make my own one. yay i can make a proper game now

hmmm interesting

##### Share on other sites
I would use a circle detection! Because: If you want to rotate an object, you simply rotate the middle point of the circles. You can't do this with bounding boxes. But: Don't use the squareroot when you calculate the range between two middle points. You simply have to multiply the other side of the "=" by itself.

not: if(c<=sqrt(a*a+b*b)) ...
do: if(c*c<=a*a+b*b) ...

Edited by - WinXP User on November 17, 2001 3:52:05 PM

##### Share on other sites

email:Royale00@hotmail.com

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631759
• Total Posts
3002138
×