2d collision detection
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
if(read_key(KEY_UP)){
g_y-=speed;
if(Collide(g_x,g_y)>0)g_y+=speed;
}
if(read_key(KEY_DOWN)){
g_y+=speed;
if(Collide(g_x,g_y)>0)g_y-=speed;
}
if(read_key(KEY_LEFT)){
g_x-=speed;
if(Collide(g_x,g_y)>0)g_x+=speed;
}
if(read_key(KEY_RIGHT)){
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
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
if(read_key(KEY_UP)){
g_y-=speed;
if(Collide(g_x,g_y)>0)g_y+=speed;
}
if(read_key(KEY_DOWN)){
g_y+=speed;
if(Collide(g_x,g_y)>0)g_y-=speed;
}
if(read_key(KEY_LEFT)){
g_x-=speed;
if(Collide(g_x,g_y)>0)g_x+=speed;
}
if(read_key(KEY_RIGHT)){
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
thanks it''s worked . Now i''m going to make my own one. yay i can make a proper game now
hmmm interesting
hmmm interesting
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
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
Hi I forgot to log in when I made that post but Im glad it helped you That was the easiest way I could place it and Im really glad you got it email me letting me know about updates to your game.
email:Royale00@hotmail.com
email:Royale00@hotmail.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement