# AddMergedCollisionFromTilesWithProperty

### Introduction

AddMergedCollisionFromTilesWithProperty adds rectangles to the calling TileShapeCollection for all tiles in the first argument (LayeredTileMap). Rather than creating individual rectangles for each tile, the rectangles will be _merged_ - all adjacent rectangles in either a row or column will be combined into one to reduce memory consumption and to speed up collision calls. Merging will happen either along rows or columns depending on the SortAxis property.

### Code Example

The following code creates adds collision to a LayeredTileMap called SolidCollision from a LayeredTileMap called Level1 . It adds collision for all tiles with the property HasCollision .

```lang:c#
SolidCollision.Visible = true;
SolidCollision.AddMergedCollisionFromTilesWithProperty(Level1, "HasCollision");
```

### Visual Example

Consider the following image (from Tiled). In this map all painted tiles have the HasCollision property, so all tiles will create collision. ![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-5ba35f2ec50361172fa6e7017023ffc7624c44c1%2F2017-11-img_5a10673318856.png?alt=media) At runtime this will produce the following collision if not using merged collision (if calling AddCollisionFromTilesWithProperty ) : ![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-b343ff3d0b96b2fcbf6c4fe83f3c6cc7016a94f3%2F2017-11-img_5a106798b374f.png?alt=media) If we change the call to use AddMergedCollisionFromTilesWithProperty then the number of rectangles is significantly reduced, as shown in the following image:

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-7971c32c78c5e5b6eae86d484d8e6d327f33f2e4%2F2017-11-img_5a106812bef15.png?alt=media)

Merging will work even for more complex maps, such as maps with gaps and concave areas, as shown in the following image:

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-f64ad21a46ed2afaff426774d040cd1500ccee13%2F2017-11-img_5a106965ab714.png?alt=media)

### Row vs. Column Merging

As mentioned above, merging will be performed according to the SortAxis property. By default the SortAxis value is set to X, so collision will be merged along columns, so that the rectangles can be sorted along the X axis.

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-4eccc30f194bc51a265353422c45aaa21d6a97e3%2F2017-11-img_5a106bfda5e98.png?alt=media)

If we change the SortAxis to Y prior to adding collision, then merging will happen along rows.

```lang:c#
SolidCollision.Visible = true;
SolidCollision.SortAxis = FlatRedBall.Math.Axis.Y;
SolidCollision.AddMergedCollisionFromTilesWithProperty(Level1, "HasCollision");
```

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-2bd79be388551aa7de74a2f25865dc381ae3b1d3%2F2017-11-img_5a106d097031e.png?alt=media)

In general the SortAxis should be X if the map is wider than it is tall, and it should be Y if the map is taller than it is wide. Adjusting the SortAxis according to the map's width or height will allow collision partitioning to operate efficiently.
