> 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/math/geometry/axisalignedrectangle/lastmovecollisionreposition.md).

# LastMoveCollisionReposition

### Introduction

The LastMoveCollisionReposition is a property that exists for all [Shapes](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php). If using CollisionRelationships, this property is set on any type of collision relationship that moves an object. This includes:

* Move Collision
* Bounce Collision
* Platformer Solid Collision
* Platformer Cloud Collision

If using manual collision calls, this property is set whenever a shape calls CollideAgainstBounce or CollideAgainstMove and the method test results in an actual collision. The LastMoveCollisionReposition property can then be tested to obtain information about collision.

### Code Example - Determining the Collision Side on a Manual Collision

When two AxisAlignedRectangles collide the collision side can be determined rather easily. The following code determines the side that two rectangles collided on: Add the following using statements:

```
 using FlatRedBall.Math.Geometry;
 using Side = FlatRedBall.Math.Collision.CollisionEnumerations.Side;
```

Assuming rectangle1 and rectangle2 are valid AxisAlignedRectangles:

```
  Side side = Side.None;

  if (rectangle1.CollideAgainstMove(rectangle2, 0, 1))
  {
      if (rectangle1.LastMoveCollisionReposition.X > 0)
          side = Side.Right;
      else if (rectangle1.LastMoveCollisionReposition.X < 0)
          side = Side.Left;
      else if (rectangle1.LastMoveCollisionReposition.Y > 0)
          side = Side.Top;
      else if (rectangle1.LastMoveCollisionReposition.Y < 0)
          side = Side.Bottom;
  }
```

### Common Usage

#### Manual Ground Collision in Platformers

The following code can be used in a platformer to detect if a character is on the ground.

```
// Assumes that character is an Entity that has a Collision shape property and
// that LevelCollision is a valid ShapeCollection
if(character.Collision.CollideAgainstBounce(LevelCollision, 0, 1, 0))
{
   // There has been a collision
   bool wasRepositionedUpward = character.Collision.LastMoveCollisionReposition.Y > 0;

   // If the character was repositioned upward, then that means it was on a surface
   character.IsOnGround = wasRepositionedUpward;
}
else
{
   // Since no collision occurred, the player isn't on the ground
   character.IsOnGround = false;
}
```

This code uses the [CollideAgainstBounce](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) method. For more information on this method, see [this page](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php). Let's look at the individual pieces of code above to see what is happening. The first line is:

```
if(character.Collision.CollideAgainstBounce(LevelCollision, 0, 1, 0))
```

This line of code tests to see if the character's collision (which we assume is a circle) collides against the "LevelCollision" which can be any shape or an entire [ShapeCollection](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php). This method does the following:

* It adjusts the velocity of the calling object (the character in this case) so that it is no longer falling. This prevents gravity accumulation errors.
* It adjusts the calling shape's (the character.Collision in this case) LastMoveCollisionReposition.
* It returns whether a collision has occurred.

So, as we can see, this first if-statement does \*a lot\*. Well, the most important thing initially is knowing \*if\* a collision has occurred. If it does, then we proceed to the body of the if-statement to find out if the player is actually on the ground. The next line of code is:

```
bool wasRepositionedUpward = character.Collision.LastMoveCollisionReposition.Y > 0;
```

This line of code tells us whether the collision that occurred should be treated as a ground collision. The reason this works is because "move" and "bounce" collision methods move the calling objects so they no longer overlap. For more information on how this works, check [this page](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php#Understanding_the_CollideAgainstMove_Implementation).![CollideAgainstMoveReposition.png](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-ff4264d69ed96f956f44fbe43bb00b1691b75d71%2Fmigrated_media-CollideAgainstMoveReposition.png?alt=media) If the player is standing on the ground, then gravity will move the player "into the ground", but the CollideAgainstBounce method will separate the two - repositioning the Circle. If the player is on the ground, then this position is vertical; in other words, it has a Y value of greater than 0. By contrast, if the character is jumping and hits the ceiling with his head, then he will be repositioned downward; the Y value will have a value less than 0. Finally we assign this value to our character's IsOnGround property:

```
character.IsOnGround = wasRepositionedUpward;
```

In this code we assume that character is an Entity that you've created that has an IsOnGround property. IsOnGround is a property that you must create in your Entity - either in custom code or as a new variable in Glue. Of course, you can store this information however you'd like; we've just presented the most common way if using Entities.

#### Manually recording LastMoveCollisionReposition

The LastMoveCollisionReposition property gives you the reposition of the last shape that a given shape has collided against. This information may not be very useful if you are colliding against a collection of shapes. In this case, you may want to manually keep track of your collision reposition:

```
Vector3 positionBefore = myEntity.Position;
CollideEntityAgainstEverything( myEntity );
Vector3 collisionReposition = myEntity.Position - positionBefore;
```


---

# 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/math/geometry/axisalignedrectangle/lastmovecollisionreposition.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.
