If I understand correctly there should be a piece of data in an obstacle and the player that identifies it's location on a grid (possibly an array) so that the collision detection function can see what obstacles are near the player and might be causing a collision?
i just implemented a collision detection in my 2d Engine. You can take a look at the pictures on my blog how it works . Each tile has a bool value 0 or zero, which states if a tile is walkable or not.
then when i move the character, i scan all the tiles around the character (not all on the screen, because thats unnecessary) and if he collides i scan from which side. than i write everything in an char and go on..
here is the code
void AnimatedChar::collisionWithTile(WorldMap ¤t_map, u_int8_t &collision)
{
int m,n,i2,j2;
Rect2D tempRect;
tempRect.pos.y = this->rect.pos.y+this->rect.height/2;
tempRect.pos.x = this->rect.pos.x;
tempRect.width = this->rect.width;
tempRect.height = this->rect.height/2;
// starting points
m = tempRect.pos.x / TILE_SIZE;
n = tempRect.pos.y / TILE_SIZE;
// end points
i2 = ((tempRect.width+tempRect.pos.x)/ TILE_SIZE);
j2 = ((tempRect.pos.y+tempRect.height)/TILE_SIZE);
bool colup = false;
bool coldown = false;
bool colleft = false;
bool colright = false;
for (int i=m-2; i < i2+2; i++)
{
for (int j=n-1; j < j2+2; j++)
{
if (current_map.returnTileAt(i, j).isWalkable==false)
{
if (tempRect.pos.x + tempRect.width-CHARACTER_COLL_OFFSET < current_map.returnTileAt(i, j).x)
collision = NO_COLLISION;
else if (tempRect.pos.x+CHARACTER_COLL_OFFSET > current_map.returnTileAt(i, j).x + TILE_SIZE)
collision = NO_COLLISION;
else if (tempRect.pos.y + tempRect.height-CHARACTER_COLL_OFFSET+2 < current_map.returnTileAt(i, j).y)
collision = NO_COLLISION;
else if (tempRect.pos.y+CHARACTER_COLL_OFFSET > current_map.returnTileAt(i, j).y + TILE_SIZE)
collision = NO_COLLISION;
else
{
// COLLISION
if (tempRect.pos.y + tempRect.height > current_map.returnTileAt(i, j).y && tempRect.pos.y < current_map.returnTileAt(i, j).y)
{
coldown = true;
}
if (tempRect.pos.x < current_map.returnTileAt(i, j).x + TILE_SIZE &&
tempRect.pos.x + tempRect.width > current_map.returnTileAt(i, j).x + TILE_SIZE)
{
colleft = true;
}
if (tempRect.pos.x + tempRect.width > current_map.returnTileAt(i, j).x &&
tempRect.pos.x < current_map.returnTileAt(i, j).x)
{
colright = true;
}
if (tempRect.pos.y < current_map.returnTileAt(i, j).y+TILE_SIZE && tempRect.pos.y + tempRect.height > current_map.returnTileAt(i, j).y + TILE_SIZE)
{
colup = true;
}
}
}
}
}
// write all values in collision
collision = (coldown<<3)|(colleft<<2)|(colright<<1)|colup;
}
Then i check when the character moves the following:
void AnimatedChar::update_position(WorldMap ¤t_map)
{
if (this->isMoving)
{
if (this->rect.pos.x < 0)
{
this->rect.pos.x = 0;
}
else if (this->rect.pos.y < 0-this->rect.height/2)
{
this->rect.pos.y = 0-this->rect.height/2;
}
else if (this->rect.pos.x+this->rect.width > current_map.get_world_width())
{
this->rect.pos.x = current_map.get_world_width()-this->rect.width;
}
else if (this->rect.pos.y+this->rect.height > current_map.get_world_height())
{
this->rect.pos.y = current_map.get_world_height()-this->rect.height;
}
else
{
Point2D temp;
temp.x = rect.pos.x;
temp.y = rect.pos.y;
if (this->viewDirection==DOWN)
{
this->rect.pos.y += this->movingSpeed;
}
else if(this->viewDirection==LEFT)
{
this->rect.pos.x -= this->movingSpeed;
}
else if(this->viewDirection==RIGHT)
{
this->rect.pos.x += this->movingSpeed;
}
else if(this->viewDirection==UP)
{
this->rect.pos.y -= this->movingSpeed;
}
u_int8_t collision=0;
this->collisionWithTile(current_map, collision);
if (this->viewDirection==DOWN && collision>>3&0x1)
{
this->rect.pos.y = temp.y-6.0f;
this->isMoving=false;
}
if (this->viewDirection==LEFT && collision>>2&0x1)
{
this->rect.pos.x = temp.x+1.0f;
this->isMoving=false;
}
if (this->viewDirection==RIGHT && collision>>1&0x1)
{
this->rect.pos.x = temp.x-1.0f;
this->isMoving=false;
}
if (this->viewDirection==UP && collision&0x1)
{
this->rect.pos.y = temp.y+1.0f;
this->isMoving=false;
}
}
}
}
maybe it helps u a little bit, if u have any questions feel free to ask.