# 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. ![](/files/Lcq10egsPFPDhmk3BT5m) At runtime this will produce the following collision if not using merged collision (if calling AddCollisionFromTilesWithProperty ) : ![](/files/aaBQ933RiQAgakQVjgo1) If we change the call to use AddMergedCollisionFromTilesWithProperty then the number of rectangles is significantly reduced, as shown in the following image:

![](/files/jFLEQVzBa3zQ3RiymYNN)

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

![](/files/LcIaUATf8wIg1sB9Z0lR)

### 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.

![](/files/YuTIPesSc8TeCAnrXj2U)

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");
```

![](/files/n58Bjp6Kg6O1rDfgwhYd)

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/flatredball/tiled-plugin/glue-gluevault-component-pages-tile-graphics-plugin-tileshapecollection/addmergedcollisionfromtileswithproperty.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
