> 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/tiled-plugin/glue-gluevault-component-pages-tile-graphics-plugin-tileshapecollection/collideagainstclosest.md).

# CollideAgainstClosest

### Introduction

The CollideAgainstClosest method can be used to perform collision between a Line and a TileShapeCollection. The method returns whether a collision has occurred. Also, it marks the closest collision point. Line vs TileShapeCollection closest collision can be used for a variety of gameplay purposes such as:

* Laser vs. level
* Grappling hook vs. level
* Fast-traveling bullet vs level

Other types of shapes usually are checked over the course of multiple frames as a collidable object moves, and these shapes have a fixed size. However, lines can be of infinite size, and often times are checked just once, such as when a bullet is fired.

### Code Example - Marking the Closest Point

The following code can be added to a GameScreen to draw a line from the camera's position to the cursor's position. CollideAgainstClosest is used to find the last collision point which is used to draw a circle. This code assumes that your screen (such as GameScreen or Level1) contains a TileShapeCollection named SolidCollision.

![](/files/KuXQsbisry55TpgOT7lB)

```
// add the following using at the to of your file to access EditorVisuals:
using GlueControl.Editing;

Line line;
void CustomInitialize()
{
    line = new Line();
    line.Visible = true;

}

void CustomActivity(bool firstTimeCalled)
{
    line.SetFromAbsoluteEndpoints(
        Camera.Main.Position.AtZ(0),
        GuiManager.Cursor.WorldPosition.ToVector3(), 
        // Must have position at endpoint 1
        Line.SetFromEndpointStyle.PositionAtEndpoint1);

    if(SolidCollision.CollideAgainstClosest(line))
    {
        var collisionPoint = line.LastCollisionPoint.ToVector3();

        EditorVisuals.Circle(8, collisionPoint);
    }
}

void CustomDestroy()
{
    line.Visible = false;
}
```

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


---

# 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/tiled-plugin/glue-gluevault-component-pages-tile-graphics-plugin-tileshapecollection/collideagainstclosest.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.
