FlatRedBall.Math.Geometry.AxisAlignedRectangle.LastMoveCollisionReposition

Introduction

The LastMoveCollisionReposition is a property that exists for all Shapes. 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

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:

Assuming rectangle1 and rectangle2 are valid AxisAlignedRectangles:

Common Usage

Platformers

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

This code uses the CollideAgainstBounce method. For more information on this method, see this page.

Let’s look at the individual pieces of code above to see what is happening. The first line is:

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

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.CollideAgainstMoveReposition.png

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:

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: