Infinite Sprite Tiling For Parallax - Pretty sure I'm doing this incorrectly

So I need to be able to auto tile on the X and Y axis for parallax backgrounds with a game I’m developing; I didn’t see anything that would allow me to do this with sprites on the asset store (most require it to be a texture), so I decided to roll my own.

The parallax is working great, and the tiling is adding and removing tiles wonderfully until you start moving very fast. The camera goes faster than the Update can check if it needs to add a new tile, and it eventually deletes all of the tiles since no new ones are created, and it removes ones that get too far from the camera.

Here’s some pseudo-code for how I’m checking to add new tiles:

Add Left If
    CamBounds.min.X - SpriteBounds.min.X >= AddThresholdX
Add Right If
    SpriteBounds.max.X - CamBounds.max.X >= AddThresholdX
Add Up If
    SpriteBounds.max.Y - CamBounds.max.Y >= AddThresholdY
Add Down If
    CamBounds.min.Y - SpriteBounds.min.Y >= AddThresholdY

I also have a grid system that makes sure it doesn’t place multiple tiles in the same place.

So this depends on the camera to be close enough to the edge of the texture for it to be able to calculate that it needs to add an adjacent tile. Is this the proper way to go about this, or is there a better way?

Here’s the full tile code block:

foreach (var obj in TiledObjects)
{
    #region AutoTileX
    if (obj.AutoTileX)
    {
        if (!obj.hasLeftTile || !obj.hasRightTile)
        {
            if ((obj.SpriteBounds.max.x - CamBounds.max.x) <= obj.AddThresholdX && !obj.hasRightTile)
            {
                int rowCheck = obj.row;
                int colCheck = obj.col + 1;

                if (!TiledObjects.Any(o => o.row == rowCheck && o.col == colCheck))
                    CreateNewTile(obj, TileDirection.RIGHT);
            }

            if ((CamBounds.min.x - obj.SpriteBounds.min.x) <= obj.AddThresholdX && !obj.hasLeftTile)
            {
                int rowCheck = obj.row;
                int colCheck = obj.col - 1;

                if (TiledObjects.Any(o => o.row == rowCheck && o.col == colCheck))
                    CreateNewTile(obj, TileDirection.LEFT);
            }
        }

        if ((CamBounds.min.x - obj.SpriteBounds.max.x >= obj.RemoveThresholdX) ||
            (obj.SpriteBounds.min.x - CamBounds.max.x >= obj.RemoveThresholdX))
        {
            if (!TilesToRemove.Contains(obj))
                TilesToRemove.Add(obj);
        }
    }
    #endregion

    #region AutoTileY
    if (obj.AutoTileY)
    {
        if (!obj.hasTopTile || !obj.hasBottomTile)
        {
            if (obj.SpriteBounds.max.y - CamBounds.max.y <= obj.AddThresholdY && !obj.hasTopTile)
            {
                int rowCheck = obj.row - 1;
                int colCheck = obj.col;

                if (TiledObjects.Any(o => o.row == rowCheck && o.col == colCheck))
                    CreateNewTile(obj, TileDirection.UP);
            }

            if (CamBounds.min.y - obj.SpriteBounds.min.y <= obj.AddThresholdY && !obj.hasBottomTile)
            {
                int rowCheck = obj.row + 1;
                int colCheck = obj.col;

                if (TiledObjects.Any(o => o.row == rowCheck && o.col == colCheck))
                    CreateNewTile(obj, TileDirection.DOWN);
            }
        }

        if ((obj.SpriteBounds.min.y - CamBounds.max.y >= obj.RemoveThresholdY) ||
            (CamBounds.min.y - obj.SpriteBounds.max.y >= obj.RemoveThresholdY))
        {
            if (!TilesToRemove.Contains(obj))
                TilesToRemove.Add(obj);
        }
    }
    #endregion
}

Usually parallax backgrounds are not tiled and use larger images moving in opposing directions at varying speeds. I assume since this is an infinite level scenario you have tile data being created on demand in chunks? Don’t do it on a column or row basis, use chunks.

I would implement a bitwise method for autotiling if I were you, it will speed up your autotiling process tremendously. Next, see where you can trim the fat on your mesh building. My tile engine can build dozens if not hundreds of 32x32 tile mesh chunks per second. There are tons of ways to trimming the fat, such as tile storage method (Lists/arrays of tile instances are slower than an array of bytes/ushorts for example). But overall your autotiling method is a bit heavy, try bitwise autotiling.

Also, you could increase the size of your chunks so they go off screen a ways. I wouldn’t wait to build a chunk until the camera is at the edge of your chunk. I would start building the next chunk when your player is at the center of the current chunk and have it displayed before the camera reaches the edge of the chunk. So create a trigger, player is at center or greater of chunk, start building next chunk and display when ready. Player is more than 1 chunk distance from chunk center, recycle chunk. That way your old chunks recycle into a pooling system or something, and you have that trigger to create the next chunk.

Thanks for your reply! I’ll look up how to do chunks and how to do a bitwise tiling system. If you know of any good resources for learning those, I’d greatly appreciate it! In the meantime, I’m off to Google to start learning. :wink: