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