# Adding a Circle

### Introduction

FlatRedBall provides a number of **primitives** which can be used to render your game, and to perform collision. These include:

* Sprite - draws an image to screen. This image is often referred to as a *Texture*.
* Text - draws text to screen.
* Circle - draws a circle to screen and can be used for circular collision.
* AxisAlignedRectangle - draws a rectangle to screen and can be used for collision. AxisAlignedRectangles cannot be rotated.

This tutorial shows how to create, move, and destroy a circle. It uses input from the keyboard to control the circle.

### A Note About Organization

FlatRedBall provides Screens and Entities for organizing your code. These are built-in to the FlatRedBall editor, but we will not use these in this tutorial so we can focus on the raw FlatRedBall engine. Instead, this tutorial will place all code in Game1. Feel free to move this code to other classes to fit your own patterns.

### Initializing the Camera

FlatRedBall provides a built-in Camera object which can be used to scroll, zoom, or even display graphics using a 3D perspective. For this tutorial we will set our camera up to be a pure 2D camera, where one unit equals one pixel on screen. To do this, add the following code to Game1's Initialize method, after the InitializeFlatRedBall call.

```csharp
Camera.Main.UsePixelCoordinates();
```

### Creating a Circle

The Circle object can be created and managed using the ShapeManager class. The ShapeManager class is responsible for:

* Creation of new instances
* Movement (velocity and acceleration)
* Drawing
* Destroying (removing references)

We will also use the InputManager which provides common classes for accessing keyboard, mouse, and Xbox gamepad input. We will be writing our code in the **Update** method in **Game1.cs**. By default our Update method looks like this:

```csharp
protected override void Update(GameTime gameTime)
{
    FlatRedBallServices.Update(gameTime);

    FlatRedBall.Screens.ScreenManager.Activity();

    GeneratedUpdate(gameTime);

    base.Update(gameTime);
}
```

Some of this code is needed if using FRB editor's code generation functionality. If you are absolutely sure you do not want to use the editor, you can remove some code in Update as shown in the following snippet:

```csharp
protected override void Update(GameTime gameTime)
{
    FlatRedBallServices.Update(gameTime);

    base.Update(gameTime);
}
```

You may notice other calls to partial methods which begin with the word "Generated" in your code. You are free to remove those methods as well.

To add a circle whenever the mouse is clicked, we will use the Cursor class to detect input and create a new circle, as shown in the following code:

```csharp
protected override void Update(GameTime gameTime)
{
    FlatRedBallServices.Update(gameTime);

    IsMouseVisible = true;

    var cursor = FlatRedBall.Gui.GuiManager.Cursor;

    if(cursor.PrimaryClick)
    {
        var circle = FlatRedBall.Math.Geometry.ShapeManager.AddCircle();

        circle.X = cursor.WorldX;
        circle.Y = cursor.WorldY;
        circle.Radius = 8;
    }

    base.Update(gameTime);
}
```

<figure><img src="/files/12JdhcYieDZpMONhuD6y" alt=""><figcaption></figcaption></figure>

### Circle Movement

Circles (and most other FlatRedBall objects) can be positioned directly by setting the X and Y values, or their position can be modified by changing their velocity and acceleration values. For example, we can modify the code to give the circle a positive XVelocity and negative YAcceleration:

```csharp
protected override void Update(GameTime gameTime)
{
    FlatRedBallServices.Update(gameTime);

    IsMouseVisible = true;

    var cursor = FlatRedBall.Gui.GuiManager.Cursor;

    if(cursor.PrimaryClick)
    {
        var circle = FlatRedBall.Math.Geometry.ShapeManager.AddCircle();

        circle.X = cursor.WorldX;
        circle.Y = cursor.WorldY;
        circle.Radius = 8;

        circle.XVelocity = 120;
        circle.YVelocity = 220;
        circle.YAcceleration = -180;
    }

    base.Update(gameTime);
}
```

The circles will now move to the right and up, but fall naturally.

<figure><img src="/files/3pRg9CD4QX1DWXRBUtOg" alt=""><figcaption></figcaption></figure>

Notice that the velocity and acceleration values apply automatically once they are assigned. The ShapeManager keeps track of all Circles and applies these values every frame. For more information on working with the Circle object in code, see the [Circle page](/flatredball/api/flatredball/math/geometry/circle.md).


---

# 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/tutorials/code-only-project-tutorial/02-adding-a-circle.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.
