> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/tiled-plugin/glue-gluevault-component-pages-tile-graphics-plugin-tileshapecollection/addmergedcollisionfromtileswithproperty.md).

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
