# 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.

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-7af664ebc2a7da90521973a91fe5c467a644e7fa%2F2023-05-img_646e09dec8499.png?alt=media)

```
// 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="https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-ae1016a4991186e0fe7ee2427e605cfe0f8c9324%2F2023-05-24_06-09-22.gif?alt=media" alt=""><figcaption></figcaption></figure>
