# Working with Tiled Shapes

### Introduction

Tiled object layers support "tile objects" (images) and geometric shapes. The following geometric object types can be created in Tiled:

* Rectangle
* Ellipse (similar to Oval)
* Polygon
* Polyline
* Tile (Image)

![](/files/iW9WDlgRojEhChzhfcjg)

The first four types (geometric shapes) are added to the Map.ShapeCollections list when loaded. These can be accessed and used at run time for custom collision, such as the creation of triggers.

### Map.ShapeCollections vs. Collidable Entities

As shown in the [Creating Entities from Object Layers page](/flatredball/tiled-plugin/using-the-tiled-plugin/07-creating-entities-from-object-layers.md), it is possible to create collidable entities using Tiled. These collidable entities ultimately hold references to shapes which can be accessed in code. Some situations (such as triggers in a level) will be easier to create with "raw shapes" rather than entities. These free floating shapes can be of any shape or size, and creating them in Tiled lets us visualize the shapes in context of the entire level.

### Creating Shapes on an Object Layer

Shapes can be added to any object layer (such as the existing GameplayObjectLayer in Level2Map), but we'll keep things organized by creating a dedicated ShapesLayer.

1. In Tiled, click the Add Layer button
2. Select "Add Object Layer"
3. Enter the name "ShapesLayer"

![](/files/CpLWmFnGK9FaWmo9L0Rj)

To add a rectangle:

1. In Tiled, click the Insert Rectangle button
2. Push and drag the left mouse button to draw a Rectangle

<figure><img src="/files/ut8pSvf2cVPzDnsuPeWE" alt=""><figcaption></figcaption></figure>

3. Enter the name "Rectangle1" for the new rectangle. This is needed to find the shape at run time.

   ![](/files/jqhPh1V0AmGBbpBoHJQv)

As always, don't forget to save your changes in Tiled.

### Working with ShapeCollections

Each object layer with one or more shape is loaded as a separate ShapeCollection at runtime. This tutorial covers the basics of working with a ShapeCollection, but more information can be found on the [ShapeCollection page](/flatredball/api/flatredball/math/geometry/shapecollection.md). All ShapeCollections are invisible by default, but can be made visible. Add the following method to GameScreen :

```csharp
private void ShapeTutorialLogic()
{
    foreach(var shapeCollection in Map.ShapeCollections)
    {
        shapeCollection.Visible = true;
    }
}
```

We could also selectively make the shape collections visible:

```csharp
private void ShapeTutorialLogic()
{
    var shapeCollection =
    Map.ShapeCollections
        // This name matches the layer name 
        .FirstOrDefault(item => item.Name == "ShapesLayer");

    if(shapeCollection != null)
    {
        shapeCollection.Visible = true;
    }
}
```

We need to modify CustomInitialize to call ShapeTutorialLogic :

```csharp
void CustomInitialize()
{
    ...
    ShapeTutorialLogic();
}
```

![White rectangle from the Map's ShapeCollection](/files/P95wWyut9Vwaqxxn7kdr)

### Accessing Individual Shapes

The TMX loading code performs the following logic when deciding where to add shapes:

* A perfect circle (not ellipse) are added to the ShapeCollection's Circles list
* An un-rotated rectangle are added to the ShapeCollection's Rectangles list
* All other shapes (ovals, rotated rectangles, and polygons) are added to the ShapeCollection's Polygons list

We can perform shape-specific logic by accessing the individual lists. For example, we can access the Rectangles instance as shown in the following code:

```csharp
private void ShapeTutorialLogic()
{
    var shapeCollection = Map.ShapeCollections
        // This name matches the layer name 
        .FirstOrDefault(item => item.Name == "ShapesLayer");

    var rectangle = shapeCollection?.AxisAlignedRectangles
        ?.FirstOrDefault(item => item.Name == "Rectangle1");

    if(rectangle != null)
    {
        rectangle.Visible = true;
    }
}
```


---

# 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/using-the-tiled-plugin/09-working-with-tiled-shapes.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.
