> 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/api/flatredball/graphics/layer/usepixelcoordinates.md).

# UsePixelCoordinates

### Introduction

The UsePixelCoordinates can be used to easily create a 2D Layer. This method is often used in combination with attaching Entities to a Camera so that they can be placed in screen space. This method is used by [Glue](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) if a given Layer's "Is 2D" property is set true.

### Code Example

The following example shows a situation where a Camera which is 3D (default) draws a 2D layer which contains some text. Add the following using statement:

```
using FlatRedBall.Graphics;
```

Add the following to Initialize after initializing FlatRedBall:

```
Layer layer = SpriteManager.AddLayer();
layer.UsePixelCoordinates();

Text text = TextManager.AddText("Hello I am text that sits on the top-left of the screen");
TextManager.AddToLayer(text, layer);

text.HorizontalAlignment = HorizontalAlignment.Left;
text.VerticalAlignment = VerticalAlignment.Top;
text.X = -SpriteManager.Camera.DestinationRectangle.Width / 2.0f;
text.Y = SpriteManager.Camera.DestinationRectangle.Height / 2.0f;
text.SetPixelPerfectScale(layer);
```

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

### A note about attachments

The code above creates two different coordinate systems; however, if the Camera is moved, then the Text that is on the 2D layer will also be affected. Usually when creating 2D HUD and UI elements, they should also be attached to the Camera. If an object is attached to the Camera, it will always stay in the same place on-screen even if the Camera moves or rotates. Therefore, the positioning code could be modified as follows:

```
// After the Text is created and added to the Layer:
text.AttachTo(SpriteManager.Camera, false);

text.HorizontalAlignment = HorizontalAlignment.Left;
text.VerticalAlignment = VerticalAlignment.Top;
// Change the X and Y settings to relative
text.RelativeX = -SpriteManager.Camera.DestinationRectangle.Width / 2.0f;
text.RelativeY = SpriteManager.Camera.DestinationRectangle.Height / 2.0f;
// We want to make sure the Text is not at the same Z as the Camera.
text.RelativeZ = -40;
```


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/graphics/layer/usepixelcoordinates.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
