# AdjustRepositionDirectionsOnAddAndRemove

### Introduction

AdjustRepositionDirectionsOnAddAndRemove determines whether a TileShapeCollection adjusts the RepositionDirections property on contained AxisAlignedRectangles when a new rectangle is added. This value defaults to true, and in most cases it should be left unchanged. For more information on the RepositionDirections, including how it pertains to TileShapeCollections, see the [AxisAlignedRectangle.RepositionDirections](../../api/flatredball/math/geometry/axisalignedrectangle/repositiondirections) page.

### RepositionDirections and Multiple TileShapeCollections

By default, TileShapeCollections adjust the contained rectangle RepositionDirections to prevent snagging. For example, the following image shows how RepositionDirections are adjusted to prevent snagging on a flat surface.

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-358d5726eeccf9cb1eb7a6e5c5deade22854c234%2F2021-04-img_606dce4158d97.png?alt=media)

The image above displays the RepositionDirections of four rectangles, all of which belong to the same TileShapeCollection. However, consider a situation where two TileShapeCollections are needed. For example, a game may have two TileShapeCollections:

* SolidCollision (white, regular ground collision)
* IceCollision (blue, slippery collision)

In this case, each TileShapeCollection would adjust the RepositionDirections of each of its contained rectangles, but areas where the TileShapeCollections touch would not be properly handled, 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-f0fce056efdb7496f8ab1140099b1fc2eec239e4%2F2021-04-img_606dcf722bc2a.png?alt=media)

In this case if a player were to walk across the transition between SolidCollision and IceCollision, the player's may snag on the seam (horizontal velocity may stop). The RepositionDirections for the rectangles in the middle of the strip should consider adjacent rectangles in different TileShapeCollections, rather than only rectangles in their own TileShapeCollection. To solve this problem, we can use another TileShapeCollection which contains all rectangles from both the SolidCollision and IceCollision. Keep in mind, a rectangle can exist as multiple TileShapeCollections, so we can take advantage of this to properly adjust RepositionDirections. Once we solve this problem (as shown below), the center rectangles where the two TileShapeCollections meet will no longer have _inward_ reposition directions.

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-55cddb9397edecbc27440d979bbfa7ab22071fd7%2F2021-04-img_606e81c6cba6b.png?alt=media)

### Combining TileShapeCollections Example

For this example, consider two TileShapeCollections defined in a GameScreen in Glue: SolidCollision and IceCollision.

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

We can uncheck the **Adjust Reposition Directions On Add And Remove** option in the Variables tab for both TileShapeCollections.

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

Next we can create a combined TileShapeCollection in Glue.

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-929efe4f9cdb6cba65defdd22e038009279cf9ab%2F2021-04-img_606e7af403266.png?alt=media)

We'll mark this new TileShapeCollection as **Empty** so that we can fill it in code.

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-0518274afdad9f5ea8983514be6ab9897b463940%2F2021-04-img_606e7b17605f1.png?alt=media)

Finally, we can insert the TileShapeCollections which we'd like to adjust the RepositionDirections of, in context of each other, in code. To do this, we can modify the CustomInitialize of our GameScreen:

```
void CustomInitialize()
{
    CombinedTileShapeCollection.InsertShapes(SolidCollision);
    CombinedTileShapeCollection.InsertShapes(IceCollision);
}
```

It's important to note that the purpose of inserting the collisions into CombinedTileShapeCollection is to adjust the RepositionDirections of each of the contained rectangles. Each individual TileShapeCollection (SolidCollision and IceCollision in this case) are still fully-functional TileShapeCollections, and CollisionRelationships can still be created between entities and these TileShapeCollections.
