[SDL] Sprite, collision with tiles

Torsten Giebl wizard at syntheticsw.com
Sat Sep 2 13:05:24 PDT 2006


Hello !


In my game i use this function
to detect collisions with the map :

bool Map::Collide_Map (CRect *rect)
{
    Sint32 lx = 0, ly = 0, rx = 0, ry = 0;
    Uint32 index_layer = 0, index_tile_x = 0, index_tile_y = 0;
    Uint32 start_tile_x = 0, start_tile_y = 0;
    Uint32 end_tile_x = 0, end_tile_y = 0;

    Check_CRect (rect);

    lx = rect -> lx;
    ly = rect -> ly;
    rx = rect -> rx;
    ry = rect -> ry;

    if (lx < 0 || ly < 0) return true;

// FIXME: This check is not working correct ?!?!?!
/*
    if ( ( lx > Map_Ptr -> Max_Map_Offset_X  ) ||
	 ( ly > Map_Ptr -> Max_Map_Offset_Y  )	    )	return false;
*/

    if ( ( rx < 0			    ) ||
	 ( ry < 0			    )	    )	return false;

    if (lx < 0)
	start_tile_x = 0;
    else
	start_tile_x = (Uint32) ( lx / Map_Ptr -> Tile_Size_X );

    if (ly < 0)
	start_tile_y = 0;
    else
        start_tile_y = (Uint32) ( ly / Map_Ptr -> Tile_Size_Y );

    if (rx < Map_Ptr -> Max_Map_Offset_X)
        end_tile_x = (Uint32) ( rx / Map_Ptr -> Tile_Size_X );
    else
	end_tile_x = Map_Ptr -> Nr_Tiles_X - 1;

    if (ry < Map_Ptr -> Max_Map_Offset_Y)
        end_tile_y = (Uint32) ( ry / Map_Ptr -> Tile_Size_Y );
    else
	end_tile_y = Map_Ptr -> Nr_Tiles_Y - 1;

    for (index_layer = 0; index_layer < Map_Ptr -> Nr_Layers; index_layer ++)
    {
	for (index_tile_y = start_tile_y;
		index_tile_y <= end_tile_y;
				index_tile_y ++)
	{
	    for (index_tile_x = start_tile_x;
		    index_tile_x <= end_tile_x;
				    index_tile_x ++)
	    {
		if (Map_Ptr -> Layers [index_layer].
					Tiles [index_tile_x][index_tile_y]
							    .Type == TYPE_WALL)
		{
		    return true;
		}
	    }
	}
    }

    return false;
}


CU





More information about the SDL mailing list