## Introduction

The ShapeCollection class is a container for a variety of shapes. The ShapeCollection class is often used for collision maps and to define triggers. ShapeCollections are often loaded from .shcx files created by the PolygonEditor. ShapeCollections are a very common class due to the support of .shcx files in Glue.

## Accessing Shapes

The ShapeCollection provides a number of methods that can be used to perform actions on all contained Shapes (such as AttachTo). However, you may be working on a game that requires access to individual shapes. The ShapeCollection exposes its shapes. The following members are available:

- AxisAlignedRectangles
- AxisAlignedCubes
- Capsule2Ds
- Circles
- Lines
- Polygons
- Spheres

These are all PositionedObjectLists which can be accessed like regular lists. You can add and remove elements from these lists as well as modify the individual members inside the lists.

If your need to perform special actions on all elements in a ShapeCollection you can do the following:

1 2 3 4 5 6 7 8 9 10 |
// Assuming ShapeCollectionInstance is a valid ShapeCollection for(int i = 0; i < ShapeCollectionInstance.AxisAlignedRectangles.Count; i++) { // do something with ShapeCollectionInstance .AxisAlignedRectangles[i]; } for(int i = 0; i < ShapeCollectionInstance.AxisAlignedCubes.Count; i++) { // do something with ShapeCollectionInstance .AxisAlignedCubes[i]; } // Continue writing loops for the other categories here... |

## Loading a ShapeCollection

ShapeCollections can be created by loading .shcx files.

### Loading ShapeCollection From File

The following code loads a .shcx file into memory, adds all contained shapes to the ShapeManager. If you are using Glue you can add ShapeCollections to Screens and Entities just like other files. For an example on how to add a ShapeCollection to Glue, see the Beefball tutorial on creating Screen collisions.

File used: ShapeCollection.shcx

Add the following using statements:

1 |
using FlatRedBall.Math.Geometry; |

Add the following to Initialize after initializing FlatRedBall:

1 2 |
ShapeCollection shapeCollection = FlatRedBallServices.Load<ShapeCollection>("shapeCollection.shcx"); shapeCollection.AddToManagers(); |

## Saving a ShapeCollection

Although most games do not need ShapeCollection saving support, FlatRedBall provides easy-to-use classes for saving a ShapeCollection. For more information, see the ShapeCollectionSave page.

## ShapeCollection Shapes

All shapes in the ShapeCollection can be accessed through its member lists. The available lists are:

- AxisAlignedRectangles
- AxisAlignedCubes
- Circles
- Polygons
- Lines
- Spheres

The ShapeCollection can be thought of as a container for all of these lists. Therefore, you can use the individual lists to do anything you would do with a normal list such as:

1 2 3 4 5 6 7 |
// Adding: myShapeCollection.Circles.Add(someCircleInstance); // Removing: ShapeManager.Remove(myShapeCollection.AxisAlignedRectangles[0]); // ...and anything else you'd want to do with shapes |

## ShapeCollection Members

- FlatRedBall.Math.Geometry.ShapeCollection.AttachTo
- FlatRedBall.Math.Geometry.ShapeCollection.AxisAlignedRectangles
- FlatRedBall.Math.Geometry.ShapeCollection.CollideAgainst
- FlatRedBall.Math.Geometry.ShapeCollection.CollideAgainstBounceWithoutSnag
- FlatRedBall.Math.Geometry.ShapeCollection.CollideAgainstMove
- FlatRedBall.Math.Geometry.ShapeCollection.CollideAgainstMoveWithoutSnag
- FlatRedBall.Math.Geometry.ShapeCollection.LastCollisionAxisAlignedCubes
- FlatRedBall.Math.Geometry.ShapeCollection.LastCollisionAxisAlignedRectangles
- FlatRedBall.Math.Geometry.ShapeCollection.LastCollisionCapsule2Ds
- FlatRedBall.Math.Geometry.ShapeCollection.LastCollisionCircles
- FlatRedBall.Math.Geometry.ShapeCollection.LastCollisionLines
- FlatRedBall.Math.Geometry.ShapeCollection.LastCollisionPolygons
- FlatRedBall.Math.Geometry.ShapeCollection.LastCollisionSpheres
- FlatRedBall.Math.Geometry.ShapeCollection.SortAscending
- FlatRedBall.Math.Geometry.ShapeCollection.Visible

Did this article leave any questions unanswered? Post any question in our forums for a rapid response.