Basic 2D Platformer Physics, Part 7: Slopes Groundwork
Demo
The demo shows the end result of the slope implementation. Use WASD to move the character. Right mouse button creates a tile. You can use the scroll wheel or the arrow keys to select a tile you want to place. The sliders change the size of the player's character.
The demo has been published under Unity 5.5.2f1, and the source code is also compatible with this version of Unity.
Slopes
Slopes add a lot of versatility to a game, both in terms of possible interactions with the game's terrain and in visual variance, but their implementation can be very complex, especially if we'd like to support a vast number of slope types.
As was true for the previous parts in the series, we'll be continuing our work from the moment we left off the last part, even though we'll be reworking a big chunk of the code we've already written. What we'll need from the start is a working moving character and a tilemap.
You can download the project files from the previous part and write the code along with this tutorial.
Changes in Movement Integration
Since making the slopes work is pretty difficult, it'd be nice if we could actually make things easier in some aspects. Some time ago I stumbled upon a blog post on how Matt Thorson handles the physics in his games. Basically, in this method the movement is always made in 1px intervals. If a movement for a particular frame is larger than one pixel, then the movement vector is split into many 1px movements, and after each one the conditions for collision with the terrain are checked.
This saves us the headache of trying to find obstacles along the line of movement at once, and instead, we can do it iteratively. This makes the implementation simpler, but unfortunately it also increases the number of collision checks performed, so it might be inappropriate for games where there are many moving objects, especially highresolution games where naturally the speed at which the objects are moving is higher. The plus side is that even though there will be more collision checks, each check will be much simpler since it knows that the character moves by a single pixel each time.
Slopes Data
Let's start defining the data that we'll need to represent the slopes. First of all, we'll need a height map of a slope, which will define its shape. Let's start with a classic 45degree slope.
Let's also define another slope shape; this one will serve as more of a bump on the ground than anything else.
Of course we will want to use variants of these slopes, depending where we'd like to place them. For example, in the case of our defined 45degree slope, it will fit nicely if there's a solid block to its right, but if the solid block is on its left then we'd like to use a flipped version of the tile we defined. We'll need to be able to flip the slopes on the X axis and Y axis as well as rotate them by 90 degrees to be able to access all the variants of a predefined slope.
Let's look at what the transformations of the 45degree slope look like.
As you can see, in this case we can get all the variants using flips. We don't really need to rotate the slope by 90 degrees, but let's see how things look for the second slope we defined earlier.
In this case, the 90degree rotation transform makes it possible to place the slope on the wall.
Calculate Offsets
Let's use our defined data to calculate the offsets which will need to be applied to the object that is overlapping with a tile. The offset will carry the information about:
 how much an object needs to move up/down/left/right in order not to collide with a tile
 how much an object needs to move to be right next to the top/bottom/left/right surface of a slope
The green parts of the above image are the parts where the object overlaps with the empty parts of the tile, and the yellow squares indicate the area in which the object overlaps with the slope.
Now, let's start to see how we'd calculate the offset for case number 1.
The object does not collide with any part of the slope. That means we don't really need to move it out of collision, so the first part of our offset will be set to 0.
For the second part of the offset, if we want the bottom of the object to be touching the slope, we would need to move it 3 pixels down. If we wanted the object's right side to touch the slope, we would need to move it 3 pixels to the right. For the object's left side to touch the right edge of the slope, we'd need to move it 16 pixels to the right. Similarly, if we wanted the top edge of the object to touch the slope, we'd need to move the object 16 pixels down.
Now, why would we need the information of how much distance there is between the object's edge and the slope? This data will be very useful to us when we want an object to stick to the slope.
So, for example, let's say an object moves left on our 45degree slope. If it moves fast enough it will end up in the air, and then eventually it will fall on the slope again, and so on. If we want it to remain on the slope, each time it moves left, we'll want to push it down so it remains in touch with the slope. The below animation shows the difference between having slope sticking enabled or disabled for a character.
We'll be caching a lot of data here—basically, we want to calculate an offset for every possible overlap with a tile. This means that for every position and for every overlap size, we'll have a quick reference of how much to move an object. Note that we cannot cache the final offsets because we can't cache an offset for every possible AABB, but it's easy to adjust the offset knowing the AABB's overlap with the slope's tile.
Defining Tiles
We'll be defining all the slope data in a static Slopes class.
1 
public static class Slopes 
2 
{

3 
}

First of all, let's handle the heightmaps. Let's define a few of them to process later on.
1 
public static readonly sbyte[] empty = new sbyte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
2 
public static readonly sbyte[] full = new sbyte[16] { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }; 
3 
public static readonly sbyte[] slope45 = new sbyte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 
4 
public static readonly sbyte[] slopeMid1 = new sbyte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1 }; 
Let's add the test tile types for the defined slopes.
1 
public enum TileType 
2 
{

3 
Empty, 
4 
Block, 
5 
OneWay, 
6 

7 
TestSlopeMid1, 
8 
TestSlopeMid1FX, 
9 
TestSlopeMid1FY, 
10 
TestSlopeMid1FXY, 
11 
TestSlopeMid1F90, 
12 
TestSlopeMid1F90X, 
13 
TestSlopeMid1F90Y, 
14 
TestSlopeMid1F90XY, 
15 

16 
TestSlope45, 
17 
TestSlope45FX, 
18 
TestSlope45FY, 
19 
TestSlope45FXY, 
20 
TestSlope45F90, 
21 
TestSlope45F90X, 
22 
TestSlope45F90Y, 
23 
TestSlope45F90XY, 
24 

25 
Count

26 
}

Let's also create another enumeration for tile collision type. This will be useful for assigning the same collision type to different tiles, for example a grassy 45degree slope or stone 45degree slope.
1 
public enum TileCollisionType 
2 
{

3 
Empty, 
4 
Block, 
5 
OneWay, 
6 

7 
SlopeMid1, 
8 
SlopeMid1FX, 
9 
SlopeMid1FY, 
10 
SlopeMid1FXY, 
11 
SlopeMid1F90, 
12 
SlopeMid1F90X, 
13 
SlopeMid1F90Y, 
14 
SlopeMid1F90XY, 
15 

16 
Slope45, 
17 
Slope45FX, 
18 
Slope45FY, 
19 
Slope45FXY, 
20 
Slope45F90, 
21 
Slope45F90X, 
22 
Slope45F90Y, 
23 
Slope45F90XY, 
24 

25 
Count

26 
}

Now let's create an array which will hold all the tiles' heightmaps. This array will be indexed by the TileCollisionType
enumeration.
1 
public static readonly sbyte[] empty = new sbyte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
2 
public static readonly sbyte[] full = new sbyte[16] { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }; 
3 
public static readonly sbyte[] slope45 = new sbyte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 
4 
public static readonly sbyte[] slopeMid1 = new sbyte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1 }; 
5 

6 
public static sbyte[][] slopesHeights; 
Processing the Slopes
Before we start calculating the offsets, we'll want to unfold our heightmaps into full collision bitmaps. This will make it easy to determine whether an AABB is colliding with a tile and also will enable more complex tile shapes if that's what we need. Let's create an array for those bitmaps.
1 
public static readonly sbyte[] empty = new sbyte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
2 
public static readonly sbyte[] full = new sbyte[16] { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }; 
3 
public static readonly sbyte[] slope45 = new sbyte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 
4 
public static readonly sbyte[] slopeMid1 = new sbyte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1 }; 
5 

6 
public static sbyte[][] slopesHeights; 
7 
public static sbyte[][][] slopesExtended; 
Now let's create a function which will extend the heightmap into the bitmap.
1 
public static sbyte[][] Extend(sbyte[] slope) 
2 
{

3 
sbyte[][] extended = new sbyte[Map.cTileSize][]; 
4 

5 
for (int x = 0; x < Map.cTileSize; ++x) 
6 
{

7 
extended[x] = new sbyte[Map.cTileSize]; 
8 

9 
for (int y = 0; y < Map.cTileSize; ++y) 
10 
extended[x][y] = System.Convert.ToSByte(y < slope[x]); 
11 
}

12 

13 
return extended; 
14 
}

Nothing complicated here—if a particular position on the tile is solid, we set it to 1; if it's not, it's set to 0.
Now let's create our Init
function, which will eventually do all the caching work we need to have done on the slopes.
1 
public static void Init() 
2 
{

3 
}

Let's create the container arrays here.
1 
public static void Init() 
2 
{ 
3 
slopesHeights = new sbyte[(int)TileCollisionType.Count][]; 
4 
slopesExtended = new sbyte[(int)TileCollisionType.Count][][]; 
5 
} 
Now let's make every tile collision type point to the corresponding cached data.
1 
for (int i = 0; i < (int)TileCollisionType.Count; ++i) 
2 
{

3 
switch ((TileCollisionType)i) 
4 
{

5 
case TileCollisionType.Empty: 
6 
slopesHeights[i] = empty; 
7 
slopesExtended[i] = Extend(slopesHeights[i]); 
8 
break; 
9 

10 
case TileCollisionType.Full: 
11 
slopesHeights[i] = full; 
12 
slopesExtended[i] = Extend(slopesHeights[i]); 
13 
break; 
14 

15 
case TileCollisionType.Slope45: 
16 
slopesHeights[i] = slope45; 
17 
slopesExtended[i] = Extend(slopesHeights[i]); 
18 
break; 
19 
case TileCollisionType.Slope45FX: 
20 
case TileCollisionType.Slope45FY: 
21 
case TileCollisionType.Slope45FXY: 
22 
case TileCollisionType.Slope45F90: 
23 
case TileCollisionType.Slope45F90X: 
24 
case TileCollisionType.Slope45F90XY: 
25 
case TileCollisionType.Slope45F90Y: 
26 
slopesHeights[i] = slopesHeights[(int)TileCollisionType.Slope45]; 
27 
slopesExtended[i] = slopesExtended[(int)TileCollisionType.Slope45]; 
28 
break; 
29 

30 
case TileCollisionType.SlopeMid1: 
31 
slopesHeights[i] = slopeMid1; 
32 
slopesExtended[i] = Extend(slopesHeights[i]); 
33 
break; 
34 
case TileCollisionType.SlopeMid1FX: 
35 
case TileCollisionType.SlopeMid1FY: 
36 
case TileCollisionType.SlopeMid1FXY: 
37 
case TileCollisionType.SlopeMid1F90: 
38 
case TileCollisionType.SlopeMid1F90X: 
39 
case TileCollisionType.SlopeMid1F90XY: 
40 
case TileCollisionType.SlopeMid1F90Y: 
41 
slopesHeights[i] = slopesHeights[(int)TileCollisionType.SlopeMid1]; 
42 
slopesExtended[i] = slopesExtended[(int)TileCollisionType.SlopeMid1]; 
43 
break; 
44 
}

45 
}

Offset Structure
Now we can define our offset structure.
1 
public struct SlopeOffsetSB 
2 
{

3 
public sbyte freeLeft, freeRight, freeDown, freeUp, collidingLeft, collidingRight, collidingBottom, collidingTop; 
4 

5 
public SlopeOffsetSB(sbyte _freeLeft, sbyte _freeRight, sbyte _freeDown, sbyte _freeUp, sbyte _collidingLeft, sbyte _collidingRight, sbyte _collidingBottom, sbyte _collidingTop) 
6 
{

7 
freeLeft = _freeLeft; 
8 
freeRight = _freeRight; 
9 
freeDown = _freeDown; 
10 
freeUp = _freeUp; 
11 

12 
collidingLeft = _collidingLeft; 
13 
collidingRight = _collidingRight; 
14 
collidingBottom = _collidingBottom; 
15 
collidingTop = _collidingTop; 
16 
}

17 
}

As explained before, the freeLeft
, freeRight
, freeDown
, and freeUp
variables correspond to the offset that needs to be applied so the object is no longer colliding with the slope, while the collidingLeft
, collidingRight
, collidingTop
, and collidingBottom
are the distance that the object needs to be shifted to touch the slope while not overlapping it.
It's time to create our heavyduty caching function, but just before we do it, let's create a container which will hold all that data.
1 
public static sbyte[][] slopesHeights; 
2 
public static sbyte[][][] slopesExtended; 
3 
public static SlopeOffsetSB[][][][][] slopeOffsets; 
And create the array in the Init
function.
1 
slopesHeights = new sbyte[(int)TileCollisionType.Count][]; 
2 
slopesExtended = new sbyte[(int)TileCollisionType.Count][][]; 
3 
slopeOffsets = new SlopeOffsetSB[(int)TileCollisionType.Count][][][][]; 
Memory Issues
As you can see, this array has plenty of dimensions, and each new tile type will actually require quite a lot of memory. For every X position in the tile, for every Y position in the tile, for every possible Width in the tile and for every possible Height, there will be a separate offset value calculation.
Since the tiles we are using are 16x16, this means that the amount of data needed for each tile type will be 16*16*16*16*8 bytes, which equals 512 kB. This is a lot of data, but still manageable, and of course if caching this amount of information is unfeasible, we'll need to either switch to calculating the offsets in real time, probably using a more efficient method than the one we're using for caching, or optimize our data.
Right now, if the tile size in our game was bigger, say 32x32, each tile type would occupy 8 MB, and if we used 64x64, then it would be 128MB. These amounts seem way too big to be useful, especially if we want to have quite a few slope types in the game. A sensible solution to this seems to be splitting the big collision tiles into smaller ones. Note that it is just each newly defined slope that requires more space—the transformations use the same data.
Checking Collisions Within a Tile
Before we start calculating the offsets, we need to know if an object at a particular position will collide with the solid parts of the tile. Let's create this function first.
1 
public static bool Collides(sbyte[][] slopeExtended, sbyte posX, sbyte posY, sbyte w, sbyte h) 
2 
{

3 
for (int x = posX; x <= posX + w && x < Map.cTileSize; ++x) 
4 
{

5 
for (int y = posY; y <= posY + h && y < Map.cTileSize; ++y) 
6 
{

7 
if (slopeExtended[x][y] == 1) 
8 
return true; 
9 
}

10 
}

11 

12 
return false; 
13 
}

The function takes the collision bitmap, the position of the overlap, and the overlap size. The position is the bottom left pixel of the object, and the size is the 0based width and height. By 0based, I mean that width of 0 means that the object is actually 1 pixel wide, and width equal to 15 means that the object is 16 pixels wide. The function is very simple—if any of the object's pixels overlap with a slope, then we return true, otherwise we return false.
Calculate the Offsets
Now let's start calculating the offsets.
1 
public static SlopeOffsetSB GetOffset(sbyte[][] slopeExtended, sbyte posX, sbyte posY, sbyte w, sbyte h) 
2 
{

3 
}

Again, to calculate the offset, we'll need the collision bitmap, position and size of the overlap. Let's start by declaring the offset values.
1 
public static SlopeOffsetSB GetOffset(sbyte[][] slopeExtended, sbyte posX, sbyte posY, sbyte w, sbyte h) 
2 
{

3 
sbyte freeUp = 0, freeDown = 0, collidingTop = 0, collidingBottom = 0; 
4 
sbyte freeLeft = 0, freeRight = 0, collidingLeft = 0, collidingRight = 0; 
5 
}

Now let's calculate how much we need to move the object to make it not collide with the slope. To do that, while the object is colliding with the slope we need to keep moving it up and checking for collision until there's no overlap with the solid parts of the tile.
Above is the illustration of how we calculate the offset. In the first case, since the object is touching the top bound of the tile, instead of just moving it up we also need to decrease its height. That's because if any part of the AABB moves outside the tile bounds, we are no longer interested in it. Similarly, offsets are calculated for all other directions, so for the above example the offsets would be:
 4 for the up offset
 4 for the left offset
 16 for the down offset—that's the maximum distance because basically if we move the object down, we need to move it all the way out of the bounds of the tile to stop colliding with the slope
 16 for the right offset
Let's start by declaring the temporary variable for the height of the object. As mentioned above, this will change depending on how high we'll be moving the object.
1 
sbyte movH = h; 
Now it's time for the main condition. As long as the object hasn't moved out of the tile bounds and it collides with the solid parts of the tile, we need to increase the offsetUp
.
1 
sbyte movH = h; 
2 
while (movH >= 0 && posY + freeUp < Map.cTileSize && Collides(slopeExtended, posX, (sbyte)(posY + freeUp), w, movH)) 
3 
{

4 
++freeUp; 
5 
}

Finally, let's adjust the size of the objecttile overlapping area if the object moves outside the bounds of the tile.
1 
sbyte movH = h; 
2 
while (movH >= 0 && posY + freeUp < Map.cTileSize && Collides(slopeExtended, posX, (sbyte)(posY + freeUp), w, movH)) 
3 
{

4 
if (posY + freeUp == Map.cTileSize) 
5 
movH; 
6 

7 
++freeUp; 
8 
}

Now let's do the same thing for the left offset. Note that when we're moving the object left and the object is being moved out of the tile bounds, we don't really need to alter the position; instead, we just change the width of the overlap. This is illustrated on the right side of the animation illustrating the offset calculation.
1 
movW = w; 
2 
while (movW >= 0 && posX + freeLeft >= 0 && Collides(slopeExtended, (sbyte)(posX + freeLeft), posY, movW, h)) 
3 
{

4 
if (posX + freeLeft == 0) 
5 
movW; 
6 
else

7 
freeLeft; 
8 
}

But here, since we weren't moving the freeLeft
offset along the way as we were decreasing the width, we need to convert the reduced size into the offset.
1 
movW = w; 
2 
while (movW >= 0 && posX + freeLeft >= 0 && Collides(slopeExtended, (sbyte)(posX + freeLeft), posY, movW, h)) 
3 
{

4 
if (posX + freeLeft == 0) 
5 
movW; 
6 
else

7 
freeLeft; 
8 
}

9 
freeLeft = (sbyte)(w  movW); 
Now let's do the same thing for the down and right offsets.
1 
movH = h; 
2 
while (movH >= 0 && posY + freeDown >= 0 && Collides(slopeExtended, posX, (sbyte)(posY + freeDown), w, movH)) 
3 
{

4 
if (posY + freeDown == 0) 
5 
movH; 
6 
else

7 
freeDown; 
8 
}

9 

10 
freeDown = (sbyte)(h  movH); 
11 

12 
sbyte movW = w; 
13 
while (movW >= 0 && posY + freeRight < Map.cTileSize && Collides(slopeExtended, (sbyte)(posX + freeRight), posY, movW, h)) 
14 
{

15 
if (posX + freeRight == Map.cTileSize) 
16 
movW; 
17 

18 
++freeRight; 
19 
}

Alright, we've calculated the first part of the offset—that is how much we should move the object for it to stop colliding with the slope. Now it's time to figure out the offsets which are supposed to move the object right next to the solid parts of the tile.
Notice that if we need to move the object out of collision, we're already doing that, because we stop right after the collision is no more.
In the case on the right, the up offset is 4, but it is also the offset that we need to move the object for its bottom edge to sit on a solid pixel. The same goes for the other sides.
1 
if (freeUp == 0) 
2 
{

3 

4 
}

5 
else

6 
collidingBottom = freeUp; 
Now the case on the left is where we need to find the offsets ourselves. If we want to find the collidingBottom
offset there, we need to move the object 3 pixels down. The calculations needed here are similar to previous ones, but instead this time we'll be looking for when the object will collide with the slope, and then moving while reducing the offset by one, so it barely touches the solid pixels instead of overlapping them.
1 
if (freeUp == 0) 
2 
{

3 
while ( posY + collidingBottom >= 0 && !Collides(slopeExtended, posX, (sbyte)(posY + collidingBottom), w, h)) 
4 
collidingBottom; 
5 

6 
collidingBottom += 1; 
7 
}

8 
else

9 
{

10 
collidingBottom = freeUp; 
11 
}

If freeUp
is equal the 0, free down must be equal to 0 as well, so we can throw in the calculations for collidingTop
under the same brackets. Again, these calculations are analogous to what we've been doing so far.
1 
if (freeUp == 0) 
2 
{

3 
while (posY + h + collidingTop < Map.cTileSize && !Collides(slopeExtended, posX, (sbyte)(posY + collidingTop), w, h)) 
4 
++collidingTop; 
5 

6 
collidingTop = 1; 
7 

8 
while ( posY + collidingBottom >= 0 && !Collides(slopeExtended, posX, (sbyte)(posY + collidingBottom), w, h)) 
9 
collidingBottom; 
10 

11 
collidingBottom += 1; 
12 
}

13 
else

14 
{

15 
collidingBottom = freeUp; 
16 
collidingTop = freeDown; 
17 
}

Let's do the same for the left and right offsets.
1 
if (freeRight == 0) 
2 
{

3 
while (posX + w + collidingRight < Map.cTileSize && !Collides(slopeExtended, (sbyte)(posX + collidingRight), posY, w, h)) 
4 
++collidingRight; 
5 

6 
collidingRight = 1; 
7 

8 
while (posX + collidingLeft >= 0 && !Collides(slopeExtended, (sbyte)(posX + collidingLeft), posY , w, h)) 
9 
collidingLeft; 
10 

11 
collidingLeft += 1; 
12 
}

13 
else

14 
{

15 
collidingLeft = freeRight; 
16 
collidingRight = freeLeft; 
17 
}

Caching the Data
Now that all the offsets are calculated, we can return the offset for this particular data set.
1 
return new SlopeOffsetSB(freeLeft, freeRight, freeDown, freeUp, collidingLeft, collidingRight, collidingBottom, collidingTop); 
Let's create a container for all our cached data.
1 
public static sbyte[][] slopesHeights; 
2 
public static sbyte[][][] slopesExtended; 
3 
public static SlopeOffsetSB[][][][][] slopeOffsets; 
Initialize the array.
1 
slopesHeights = new sbyte[(int)TileCollisionType.Count][]; 
2 
slopesExtended = new sbyte[(int)TileCollisionType.Count][][]; 
3 
slopeOffsets = new SlopeOffsetSB[(int)TileCollisionType.Count][][][][]; 
And finally, create the caching function.
1 
public static SlopeOffsetSB[][][][] CacheSlopeOffsets(sbyte[][] slopeExtended) 
2 
{

3 
var offsetCache = new SlopeOffsetSB[Map.cTileSize][][][]; 
4 

5 
for (int x = 0; x < Map.cTileSize; ++x) 
6 
{

7 
offsetCache[x] = new SlopeOffsetSB[Map.cTileSize][][]; 
8 

9 
for (int y = 0; y < Map.cTileSize; ++y) 
10 
{

11 
offsetCache[x][y] = new SlopeOffsetSB[Map.cTileSize][]; 
12 

13 
for (int w = 0; w < Map.cTileSize; ++w) 
14 
{

15 
offsetCache[x][y][w] = new SlopeOffsetSB[Map.cTileSize]; 
16 

17 
for (int h = 0; h < Map.cTileSize; ++h) 
18 
{

19 
offsetCache[x][y][w][h] = GetOffset(slopeExtended, (sbyte)x, (sbyte)y, (sbyte)w, (sbyte)h); 
20 
}

21 
}

22 
}

23 
}

24 

25 
return offsetCache; 
26 
}

The function itself is very simple, so it's very easy to see how much data it caches to satisfy our requirements!
Now make sure to cache the offsets for each tile collision type.
1 
case TileCollisionType.Slope45: 
2 
slopesHeights[i] = slope45; 
3 
slopesExtended[i] = Extend(slopesHeights[i]); 
4 
slopeOffsets[i] = CacheSlopeOffsets(slopesExtended[i]); 
5 
break; 
6 
case TileCollisionType.Slope45FX: 
7 
case TileCollisionType.Slope45FY: 
8 
case TileCollisionType.Slope45FXY: 
9 
case TileCollisionType.Slope45F90: 
10 
case TileCollisionType.Slope45F90X: 
11 
case TileCollisionType.Slope45F90XY: 
12 
case TileCollisionType.Slope45F90Y: 
13 
slopesHeights[i] = slopesHeights[(int)TileCollisionType.Slope45]; 
14 
slopesExtended[i] = slopesExtended[(int)TileCollisionType.Slope45]; 
15 
slopeOffsets[i] = slopeOffsets[(int)TileCollisionType.Slope45]; 
16 
break; 
And that's it, our main caching function is finished!
Calculating the World Space Offset
Now let's use the cached data to make a function which will return an offset for a character which exists in a world space.
1 
public static SlopeOffsetI GetOffset(Vector2 tileCenter, float leftX, float rightX, float bottomY, float topY, TileCollisionType tileCollisionType) 
2 
{

3 
}

The offset that we'll be returning is not the same struct we used for the cached data, since the world space offsets can end up being bigger than the limits of the single byte. The structure is basically the same thing, but using integers.
The parameters are as follows:
 the world space center of the tile
 the left, right, bottom and top edges of the AABB we want to receive the offset for
 the type of tile we want to receive the offset for
First, we need to figure out how the AABB overlaps with a slope tile. We need to know where the overlap starts (the bottom left corner), and also how much the overlap extends over the tile.
To calculate this, let's first declare the variables.
1 
public static SlopeOffsetI GetOffset(Vector2 tileCenter, float leftX, float rightX, float bottomY, float topY, TileCollisionType tileCollisionType) 
2 
{

3 
int posX, posY, sizeX, sizeY; 
4 
SlopeOffsetI offset; 
5 
}

Now let's calculate the edges of the tile in the world space.
1 
float leftTileEdge = tileCenter.x  Map.cTileSize / 2; 
2 
float rightTileEdge = leftTileEdge + Map.cTileSize; 
3 
float bottomTileEdge = tileCenter.y  Map.cTileSize / 2; 
4 
float topTileEdge = bottomTileEdge + Map.cTileSize; 
Now this should be quite easy. There are two main categories of cases we can find here. First is that the overlap is within the tile bounds.
The dark blue pixel is the position of the overlap, and the height and width are marked with the blue tiles. Here things are pretty straightforward, so calculating the position and the size of the overlap doesn't require any additional actions.
The second category of cases looks as follows, and in the game we'll mostly be dealing with those cases:
Let's look at an example situation pictured above. As you can see, the AABB extends well beyond the tile, but what we need to figure out is the position and size of the overlap within the tile itself, so we can retrieve our cached offset value. Right now we don't really care about anything that lies beyond the tile bounds. This will require us to clamp the overlap position and size to the tile's bounds.
Position x is equal to the offset between the left edge of the AABB and the left edge of the tile. If AABB is to the left of the tile's left edge, the position needs to be clamped to 0. To get the overlap width, we need to subtract the AABB's right edge from the overlap's x position, which we already calculated.
The values for the Y axis are calculated in the same way.
1 
posX = (int)Mathf.Clamp(leftX  leftTileEdge, 0.0f, Map.cTileSize  1); 
2 
sizeX = (int)Mathf.Clamp(rightX  (leftTileEdge + posX), 0.0f, Map.cTileSize  1); 
3 

4 
posY = (int)Mathf.Clamp(bottomY  bottomTileEdge, 0.0f, Map.cTileSize  1); 
5 
sizeY = (int)Mathf.Clamp(topY  (bottomTileEdge + posY), 0.0f, Map.cTileSize  1); 
Now we can retrieve the cached offsets for the overlap.
1 
offset = new SlopeOffsetI(slopeOffsets[(int)tileCollisionType][posX][posY][sizeX][sizeY]); 
Adjust the Offset
Before we return the offset, we might need to adjust it. Consider the following situation.
Let's see how our cached offset for such an overlap would look. When caching, we were only concerned about the overlap within the tile bounds, so in this case, the up offset would be equal to 9. You can see that if we moved the overlap area within the tile bounds 9 pixels up, it would cease to collide with the slope, but if we move the whole AABB, then the area which is below the tile bounds will move into the collision.
Basically, what we need to do here is adjust the up offset by the number of pixels the AABB extends below the tile bounds.
1 
if (bottomTileEdge > bottomY) 
2 
{

3 
if (offset.freeUp > 0) 
4 
offset.freeUp += (int)bottomTileEdge  (int)bottomY; 
5 
offset.collidingBottom = offset.freeUp; 
6 
}

The same thing needs to be done for all of the other offsets—left, right, and down—except that for now we'll skip handling the left and right offsets in this manner since it is not necessary to do so.
1 
if (topTileEdge < topY) 
2 
{

3 
if (offset.freeDown < 0) 
4 
offset.freeDown = (int)(topY  topTileEdge); 
5 
offset.collidingTop = offset.freeDown; 
6 
}

7 
if (bottomTileEdge > bottomY) 
8 
{

9 
if (offset.freeUp > 0) 
10 
offset.freeUp += (int)bottomTileEdge  (int)bottomY; 
11 
offset.collidingBottom = offset.freeUp; 
12 
}

Once we're done, we can return the adjusted offset. The finished function should look like this.
1 
public static SlopeOffsetI GetOffset(Vector2 tileCenter, float leftX, float rightX, float bottomY, float topY, TileCollisionType tileCollisionType) 
2 
{

3 
int posX, posY, sizeX, sizeY; 
4 

5 
float leftTileEdge = tileCenter.x  Map.cTileSize / 2; 
6 
float rightTileEdge = leftTileEdge + Map.cTileSize; 
7 
float bottomTileEdge = tileCenter.y  Map.cTileSize / 2; 
8 
float topTileEdge = bottomTileEdge + Map.cTileSize; 
9 
SlopeOffsetI offset; 
10 

11 
posX = (int)Mathf.Clamp(leftX  leftTileEdge, 0.0f, Map.cTileSize  1); 
12 
sizeX = (int)Mathf.Clamp(rightX  (leftTileEdge + posX), 0.0f, Map.cTileSize  1); 
13 

14 
posY = (int)Mathf.Clamp(bottomY  bottomTileEdge, 0.0f, Map.cTileSize  1); 
15 
sizeY = (int)Mathf.Clamp(topY  (bottomTileEdge + posY), 0.0f, Map.cTileSize  1); 
16 

17 
offset = new SlopeOffsetI(slopeOffsets[(int)tileCollisionType][posX][posY][sizeX][sizeY]); 
18 

19 
if (topTileEdge < topY) 
20 
{

21 
if (offset.freeDown < 0) 
22 
offset.freeDown = (int)(topY  topTileEdge); 
23 
offset.collidingTop = offset.freeDown; 
24 
}

25 
if (bottomTileEdge > bottomY) 
26 
{

27 
if (offset.freeUp > 0) 
28 
offset.freeUp += Mathf.RoundToInt(bottomTileEdge  bottomY); 
29 
offset.collidingBottom = offset.freeUp; 
30 
}

31 

32 
return offset; 
33 
}

Of course, it's not completely done yet. Later on we'll also be handling the tile transformations here, so the offset is returned appropriately depending on whether the tile has been flipped on the XY axes or rotated 90 degrees. For now, though, we'll be playing only with the nontransformed tiles.
Implementing OnePixel Step Physics
Overview
Moving the objects by one pixel will make it quite easy to handle a lot of things, especially collisions against slopes for fast objects, but even though we're going to check for collision each pixel we move, we should move in a specific pattern to ensure accuracy. This pattern will be dependent on the object's speed.
On the picture above, you can see that if we blindly move the object first all the pixels it needs to move horizontally, and after that vertically, the arrow would end up colliding with a solid block that's not really on its course. The order of movement should be based on the ratio of the vertical to horizontal speed; this way we'll know how many pixels we need to move vertically for each pixel moved horizontally.
Define the Data
Let's move to our moving object class and define a few new variables.
First of all, our main mPosition
variable will be holding only the integer numbers, and we'll be keeping another variable called mRemainder
to keep the value after the floating point.
1 
public Vector2 mPosition; 
2 
public Vector2 mRemainder; 
Next, we'll add a few new position status variables to indicate whether the character is currently on the slope. At this point, it will be good if we pack all the position status into a single structure.
1 
[Serializable] 
2 
public struct PositionState 
3 
{

4 
public bool pushesRight; 
5 
public bool pushesLeft; 
6 
public bool pushesBottom; 
7 
public bool pushesTop; 
8 

9 
public bool pushedTop; 
10 
public bool pushedBottom; 
11 
public bool pushedRight; 
12 
public bool pushedLeft; 
13 

14 
public bool pushedLeftObject; 
15 
public bool pushedRightObject; 
16 
public bool pushedBottomObject; 
17 
public bool pushedTopObject; 
18 

19 
public bool pushesLeftObject; 
20 
public bool pushesRightObject; 
21 
public bool pushesBottomObject; 
22 
public bool pushesTopObject; 
23 

24 
public bool pushedLeftTile; 
25 
public bool pushedRightTile; 
26 
public bool pushedBottomTile; 
27 
public bool pushedTopTile; 
28 

29 
public bool pushesLeftTile; 
30 
public bool pushesRightTile; 
31 
public bool pushesBottomTile; 
32 
public bool pushesTopTile; 
33 

34 
public bool onOneWayPlatform; 
35 

36 
public Vector2i leftTile; 
37 
public Vector2i rightTile; 
38 
public Vector2i topTile; 
39 
public Vector2i bottomTile; 
40 

41 
public void Reset() 
42 
{

43 
leftTile = rightTile = topTile = bottomTile = new Vector2i(1, 1); 
44 

45 
pushesRight = false; 
46 
pushesLeft = false; 
47 
pushesBottom = false; 
48 
pushesTop = false; 
49 

50 
pushedTop = false; 
51 
pushedBottom = false; 
52 
pushedRight = false; 
53 
pushedLeft = false; 
54 

55 
pushedLeftObject = false; 
56 
pushedRightObject = false; 
57 
pushedBottomObject = false; 
58 
pushedTopObject = false; 
59 

60 
pushesLeftObject = false; 
61 
pushesRightObject = false; 
62 
pushesBottomObject = false; 
63 
pushesTopObject = false; 
64 

65 
pushedLeftTile = false; 
66 
pushedRightTile = false; 
67 
pushedBottomTile = false; 
68 
pushedTopTile = false; 
69 

70 
pushesLeftTile = false; 
71 
pushesRightTile = false; 
72 
pushesBottomTile = false; 
73 
pushesTopTile = false; 
74 

75 
onOneWayPlatform = false; 
76 
}

77 
}

Now let's declare an instance of the struct for the object.
1 
public PositionState mPS; 
Another variable that we'll need is slope sticking.
1 
public bool mSticksToSlope; 
Basic Implementation
Let's start by creating the basic collision checking functions; these will not handle the slopes yet.
Collision Checks
Let's start with the right side.
1 
public bool CollidesWithTileRight(ref Vector2 position, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState stat) 
2 
{

3 
}

The parameters used here are the current position of the object, its top right and bottom left corners, and the position state. First of all, let's calculate the top right and top left tile for our object.
1 
Vector2i topRightTile = mMap.GetMapTileAtPoint(new Vector2(topRight.x + 0.5f, topRight.y  0.5f)); 
2 
Vector2i bottomLeftTile = mMap.GetMapTileAtPoint(new Vector2(bottomLeft.x + 0.5f, bottomLeft.y + 0.5f)); 
Now let's iterate through all the tiles along the object's right edge.
1 
for (int y = bottomLeftTile.y; y <= topRightTile.y; ++y) 
2 
{

3 
var tileCollisionType = mMap.GetCollisionType(topRightTile.x, y); 
4 
}

Now, depending on the collision tile, we react appropriately.
1 
switch (tileCollisionType) 
2 
{

3 
default://slope 
4 
break; 
5 
case TileCollisionType.Empty: 
6 
break; 
7 
case TileCollisionType.Full: 
8 
state.pushesRightTile = true; 
9 
state.rightTile = new Vector2i(topRightTile.x, y); 
10 
return true; 
11 
}

As you can see, for now we'll skip handling the slopes; we just want to get the basic setup done before we delve into that.
Overall, the function for now should look like this:
1 
public bool CollidesWithTileRight(ref Vector2 position, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState stat) 
2 
{

3 
Vector2i topRightTile = mMap.GetMapTileAtPoint(new Vector2(topRight.x + 0.5f, topRight.y  0.5f)); 
4 
Vector2i bottomLeftTile = mMap.GetMapTileAtPoint(new Vector2(bottomLeft.x + 0.5f, bottomLeft.y + 0.5f)); 
5 

6 
for (int y = bottomLeftTile.y; y <= topRightTile.y; ++y) 
7 
{

8 
var tileCollisionType = mMap.GetCollisionType(topRightTile.x, y); 
9 

10 
switch (tileCollisionType) 
11 
{

12 
default://slope 
13 
break; 
14 
case TileCollisionType.Empty: 
15 
break; 
16 
case TileCollisionType.Full: 
17 
state.pushesRightTile = true; 
18 
state.rightTile = new Vector2i(topRightTile.x, y); 
19 
return true; 
20 
}

21 
}

22 

23 
return false; 
24 
}

We'll do the same for all the other three directions: left, up, and down.
1 
public bool CollidesWithTileLeft(ref Vector2 position, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState stat) 
2 
{

3 
Vector2i topRightTile = mMap.GetMapTileAtPoint(new Vector2(topRight.x  0.5f, topRight.y  0.5f)); 
4 
Vector2i bottomLeftTile = mMap.GetMapTileAtPoint(new Vector2(bottomLeft.x  0.5f, bottomLeft.y + 0.5f)); 
5 

6 
for (int y = bottomLeftTile.y; y <= topRightTile.y; ++y) 
7 
{

8 
var tileCollisionType = mMap.GetCollisionType(bottomLeftTile.x, y); 
9 

10 
switch (tileCollisionType) 
11 
{

12 
default://slope 
13 
break; 
14 
case TileCollisionType.Empty: 
15 
break; 
16 
case TileCollisionType.Full: 
17 
state.pushesLeftTile = true; 
18 
state.leftTile = new Vector2i(bottomLeftTile.x, y); 
19 
return true; 
20 
}

21 
}

22 

23 
return false; 
24 
}

25 

26 
public bool CollidesWithTileTop(ref Vector2 position, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState stat) 
27 
{

28 
Vector2i topRightTile = mMap.GetMapTileAtPoint(new Vector2(topRight.x  0.5f, topRight.y + 0.5f)); 
29 
Vector2i bottomleftTile = mMap.GetMapTileAtPoint(new Vector2(bottomLeft.x + 0.5f, bottomLeft.y + 0.5f)); 
30 

31 
for (int x = bottomleftTile.x; x <= topRightTile.x; ++x) 
32 
{

33 
var tileCollisionType = mMap.GetCollisionType(x, topRightTile.y); 
34 

35 
switch (tileCollisionType) 
36 
{

37 
default://slope 
38 
break; 
39 
case TileCollisionType.Empty: 
40 
break; 
41 
case TileCollisionType.Full: 
42 
state.pushesTopTile = true; 
43 
state.topTile = new Vector2i(x, topRightTile.y); 
44 
return true; 
45 
}

46 
}

47 

48 
return false; 
49 
}

50 

51 
public bool CollidesWithTileBottom(ref Vector2 position, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState stat) 
52 
{

53 
Vector2i topRightTile = mMap.GetMapTileAtPoint(new Vector2(topRight.x  0.5f, topRight.y  0.5f)); 
54 
Vector2i bottomleftTile = mMap.GetMapTileAtPoint(new Vector2(bottomLeft.x + 0.5f, bottomLeft.y  0.5f)); 
55 

56 
for (int x = bottomleftTile.x; x <= topRightTile.x; ++x) 
57 
{

58 
var tileCollisionType = mMap.GetCollisionType(x, bottomleftTile.y); 
59 

60 
switch (tileCollisionType) 
61 
{

62 
default://slope 
63 
break; 
64 
case TileCollisionType.Empty: 
65 
break; 
66 
case TileCollisionType.Full: 
67 
state.onOneWayPlatform = false; 
68 
state.pushesBottomTile = true; 
69 
state.bottomTile = new Vector2i(x, bottomleftTile.y); 
70 
return true; 
71 
}

72 
}

73 

74 
return false; 
75 
}

Moving Functions
Now that we have this covered, we can start creating two functions responsible for movement. One will handle the movement horizontally, and another will handle the vertical movement.
1 
public void MoveX(ref Vector2 position, ref bool foundObstacleX, float offset, float step, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState state) 
2 
{

3 
}

The arguments we'll use in this function are the current position, a boolean which indicates whether we find an obstacle along the way or not, an offset which defines how much we need to move, a step which is a value we move the object with each iteration, the AABB's lower left and upper right vertices, and finally the position state.
Basically, what we want to do here is move the object by a step so many times, so that the steps sum up to the offset. Of course, if we meet an obstacle, we need to stop moving as well.
1 
while (!foundObstacleX && offset != 0.0f) 
2 
{

3 
}

With each iteration, we subtract the step from the offset, so the offset eventually becomes zero, and we know we moved as many pixels as we needed to.
1 
while (!foundObstacleX && offset != 0.0f) 
2 
{

3 
offset = step; 
4 
}

With each step, we want to check whether we collide with a tile. If we're moving right, we want to check if we collide with a wall on the right; if we're moving left, we want to check for obstacles on the left.
1 
while (!foundObstacleX && offset != 0.0f) 
2 
{

3 
offset = step; 
4 

5 
if (step > 0.0f) 
6 
foundObstacleX = CollidesWithTileRight(ref position, ref topRight, ref bottomLeft, ref state, true); 
7 
else

8 
foundObstacleX = CollidesWithTileLeft(ref position, ref topRight, ref bottomLeft, ref state, true); 
9 
}

If we didn't find an obstacle, we can move the object.
1 
while (!foundObstacleX && offset != 0.0f) 
2 
{

3 
offset = step; 
4 

5 
if (step > 0.0f) 
6 
foundObstacleX = CollidesWithTileRight(ref position, ref topRight, ref bottomLeft, ref state, true); 
7 
else

8 
foundObstacleX = CollidesWithTileLeft(ref position, ref topRight, ref bottomLeft, ref state, true); 
9 

10 
if (!foundObstacleX) 
11 
{

12 
position.x += step; 
13 
topRight.x += step; 
14 
bottomLeft.x += step; 
15 
}

16 
}

Finally, after we move, we check for collisions up and down, because we could slide right under or above a block. This is just to update the position state to be accurate.
1 
public void MoveX(ref Vector2 position, ref bool foundObstacleX, float offset, float step, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState state) 
2 
{

3 
while (!foundObstacleX && offset != 0.0f) 
4 
{

5 
offset = step; 
6 

7 
if (step > 0.0f) 
8 
foundObstacleX = CollidesWithTileRight(ref position, ref topRight, ref bottomLeft, ref state, true); 
9 
else

10 
foundObstacleX = CollidesWithTileLeft(ref position, ref topRight, ref bottomLeft, ref state, true); 
11 

12 
if (!foundObstacleX) 
13 
{

14 
position.x += step; 
15 
topRight.x += step; 
16 
bottomLeft.x += step; 
17 

18 
CollidesWithTileTop(ref position, ref topRight, ref bottomLeft, ref state); 
19 
CollidesWithTileBottom(ref position, ref topRight, ref bottomLeft, ref state); 
20 
}

21 
}

22 
}

The MoveY
function works similarly.
1 
public void MoveY(ref Vector2 position, ref bool foundObstacleY, float offset, float step, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState state) 
2 
{

3 
while (!foundObstacleY && offset != 0.0f) 
4 
{

5 
offset = step; 
6 

7 
if (step > 0.0f) 
8 
foundObstacleY = CollidesWithTileTop(ref position, ref topRight, ref bottomLeft, ref state); 
9 
else

10 
foundObstacleY = CollidesWithTileBottom(ref position, ref topRight, ref bottomLeft, ref state); 
11 

12 
if (!foundObstacleY) 
13 
{

14 
position.y += step; 
15 
topRight.y += step; 
16 
bottomLeft.y += step; 
17 

18 
CollidesWithTileLeft(ref position, ref topRight, ref bottomLeft, ref state); 
19 
CollidesWithTileRight(ref position, ref topRight, ref bottomLeft, ref state); 
20 
}

21 
}

22 
}

Consolidate the Movement
Now that we have functions responsible for vertical and horizontal movement, we can create the main function responsible for movement.
1 
public void Move(Vector2 offset, Vector2 speed, ref Vector2 position, ref Vector2 remainder, AABB aabb, ref PositionState state) 
2 
{

3 
}

The function takes the value of how much to move the object, the object's current speed, its current position together with the floating point remainder, the object's AABB, and the position state.
The first thing we'll do here is add the offset to the remainder, so that in the remainder we have the full value of how much our character should move.
1 
remainder += offset; 
Since we'll be calling the MoveX
and MoveY
functions from this one, we'll need to pass the top right and bottom left corners of the AABB, so let's calculate them now.
1 
Vector2 topRight = aabb.Max(); 
2 
Vector2 bottomLeft = aabb.Min(); 
We also need to get the step vector. It will be used as a direction in which we'll be moving our object.
1 
var step = new Vector2(Mathf.Sign(offset.x), Mathf.Sign(offset.y)); 
Now let's see how many pixels we actually need to move. We need to round the remainder, because we are always going to move by an integer number, and then we need to subtract that value from the remainder.
1 
var move = new Vector2(Mathf.Round(remainder.x), Mathf.Round(remainder.y)); 
2 
remainder = move; 
Now let's split the movement into four cases, depending on our move vector values. If the move vector's x and y values are equal to 0, there is no movement to be made, so we can just return.
1 
if (move.x == 0.0f && move.y == 0.0f) 
2 
return; 
If only the y value is 0, we're going to move only horizontally.
1 
if (move.x == 0.0f && move.y == 0.0f) 
2 
return; 
3 
else if (move.x != 0.0f && move.y == 0.0f) 
4 
MoveX(ref position, ref foundObstacleX, move.x, step.x, ref topRight, ref bottomLeft, ref state); 
If only the x value is 0, we're going to move only vertically.
1 
if (move.x == 0.0f && move.y == 0.0f) 
2 
return; 
3 
else if (move.x != 0.0f && move.y == 0.0f) 
4 
MoveX(ref position, ref foundObstacleX, move.x, step.x, ref topRight, ref bottomLeft, ref state); 
5 
else if (move.y != 0.0f && move.x == 0.0f) 
6 
MoveY(ref position, ref foundObstacleY, move.y, step.y, ref topRight, ref bottomLeft, ref state); 
7 
else

8 
{

9 
}

If we need to move both on the x and y axes, we need to move in a pattern that was described earlier. First off, let's calculate the speed ratio.
1 
float speedRatio = Mathf.Abs(speed.y) / Mathf.Abs(speed.x); 
Let's also declare the vertical accumulator which will hold how many pixels we need to move vertically with each loop.
1 
float speedRatio = Mathf.Abs(speed.y) / Mathf.Abs(speed.x); 
2 
float vertAccum = 0.0f; 
The condition to stop moving will be that we either met an obstacle on any of the axes or the object was moved by the whole move vector.
1 
float speedRatio = Mathf.Abs(speed.y) / Mathf.Abs(speed.x); 
2 
float vertAccum = 0.0f; 
3 

4 
while (!foundObstacleX && !foundObstacleY && (move.x != 0.0f  move.y != 0.0f)) 
5 
{

6 
}

Now let's calculate how many pixels vertically we should move the object.
1 
while (!foundObstacleX && !foundObstacleY && (move.x != 0.0f  move.y != 0.0f)) 
2 
{

3 
vertAccum += Mathf.Sign(step.y) * speedRatio; 
4 
}

For the movement, we first move one step horizontally.
1 
while (!foundObstacleX && !foundObstacleY && (move.x != 0.0f  move.y != 0.0f)) 
2 
{

3 
vertAccum += Mathf.Sign(step.y) * speedRatio; 
4 

5 
MoveX(ref position, ref foundObstacleX, step.x, step.x, ref topRight, ref bottomLeft, ref state); 
6 
move.x = step.x; 
7 
}

8 
And after this we can move vertically. Here we know that we need to move the object by the value contained in the vertAccum
, but in case of any inaccuracies, if we moved all the way on the X axis, we also need to move all the way on the Y axis.
1 
while (!foundObstacleX && !foundObstacleY && (move.x != 0.0f  move.y != 0.0f)) 
2 
{

3 
vertAccum += Mathf.Sign(step.y) * speedRatio; 
4 

5 
MoveX(ref position, ref foundObstacleX, step.x, step.x, ref topRight, ref bottomLeft, ref state); 
6 
move.x = step.x; 
7 

8 
while (!foundObstacleY && move.y != 0.0f && (Mathf.Abs(vertAccum) >= 1.0f  move.x == 0.0f)) 
9 
{

10 
move.y = step.y; 
11 
vertAccum = step.y; 
12 

13 
MoveY(ref position, ref foundObstacleX, step.y, step.y, ref topRight, ref bottomLeft, ref state); 
14 
}

15 
}

16 
All in all, the function should look like this:
1 
public void Move(Vector2 offset, Vector2 speed, ref Vector2 position, ref Vector2 remainder, AABB aabb, ref PositionState state) 
2 
{

3 
remainder += offset; 
4 

5 
Vector2 topRight = aabb.Max(); 
6 
Vector2 bottomLeft = aabb.Min(); 
7 

8 
bool foundObstacleX = false, foundObstacleY = false; 
9 

10 
var step = new Vector2(Mathf.Sign(offset.x), Mathf.Sign(offset.y)); 
11 
var move = new Vector2(Mathf.Round(remainder.x), Mathf.Round(remainder.y)); 
12 
remainder = move; 
13 

14 
if (move.x == 0.0f && move.y == 0.0f) 
15 
return; 
16 
else if (move.x != 0.0f && move.y == 0.0f) 
17 
MoveX(ref position, ref foundObstacleX, move.x, step.x, ref topRight, ref bottomLeft, ref state); 
18 
else if (move.y != 0.0f && move.x == 0.0f) 
19 
MoveY(ref position, ref foundObstacleY, move.y, step.y, ref topRight, ref bottomLeft, ref state); 
20 
else

21 
{

22 
float speedRatio = Mathf.Abs(speed.y) / Mathf.Abs(speed.x); 
23 
float vertAccum = 0.0f; 
24 

25 
while (!foundObstacleX && !foundObstacleY && (move.x != 0.0f  move.y != 0.0f)) 
26 
{

27 
vertAccum += Mathf.Sign(step.y) * speedRatio; 
28 

29 
MoveX(ref position, ref foundObstacleX, step.x, step.x, ref topRight, ref bottomLeft, ref state); 
30 
move.x = step.x; 
31 

32 
while (!foundObstacleY && move.y != 0.0f && (Mathf.Abs(vertAccum) >= 1.0f  move.x == 0.0f)) 
33 
{

34 
move.y = step.y; 
35 
vertAccum = step.y; 
36 

37 
MoveY(ref position, ref foundObstacleX, step.y, step.y, ref topRight, ref bottomLeft, ref state); 
38 
}

39 
}

40 
}

41 
}

Now we can use the functions we've built to compose our main UpdatePhysics
function.
Build the Physics Update Function
First of all, we want to update the position state, so all of the previous frame's data goes to the adequate variables, and the current frame's data is reset.
1 
public void UpdatePhysics() 
2 
{

3 
mPS.pushedBottom = mPS.pushesBottom; 
4 
mPS.pushedRight = mPS.pushesRight; 
5 
mPS.pushedLeft = mPS.pushesLeft; 
6 
mPS.pushedTop = mPS.pushesTop; 
7 

8 
mPS.pushedBottomTile = mPS.pushesBottomTile; 
9 
mPS.pushedLeftTile = mPS.pushesLeftTile; 
10 
mPS.pushedRightTile = mPS.pushesRightTile; 
11 
mPS.pushedTopTile = mPS.pushesTopTile; 
12 

13 
mPS.pushesBottom = mPS.pushesLeft = mPS.pushesRight = mPS.pushesTop = 
14 
mPS.pushesBottomTile = mPS.pushesLeftTile = mPS.pushesRightTile = mPS.pushesTopTile = 
15 
mPS.pushesBottomObject = mPS.pushesLeftObject = mPS.pushesRightObject = mPS.pushesTopObject = mPS.onOneWay = false; 
16 
}

Now let's update the collision state of our object. We do this so that before we move our object, we have updated data on whether it's on the ground or is pushing any other tiles. Normally the previous frame's data would still be up to date if the terrain was unmodifiable and other objects wouldn't be able to move this one, but here we assume that any of this could happen.
1 
Vector2 topRight = mAABB.Max(); 
2 
Vector2 bottomLeft = mAABB.Min(); 
3 

4 
CollidesWithTiles(ref mPosition, ref topRight, ref bottomLeft, ref mPS); 
CollidesWithTiles
simply calls all the collision functions we've written.
1 
public void CollidesWithTiles(ref Vector2 position, ref Vector2 topRight, ref Vector2 bottomLeft, ref PositionState state) 
2 
{

3 
CollidesWithTileTop(ref position, ref topRight, ref bottomLeft, ref state); 
4 
CollidesWithTileBottom(ref position, ref topRight, ref bottomLeft, ref state); 
5 
CollidesWithTileLeft(ref position, ref topRight, ref bottomLeft, ref state); 
6 
CollidesWithTileRight(ref position, ref topRight, ref bottomLeft, ref state); 
7 
}

Then update the speed.
1 
mOldSpeed = mSpeed; 
2 

3 
if (mPS.pushesBottomTile) 
4 
mSpeed.y = Mathf.Max(0.0f, mSpeed.y); 
5 
if (mPS.pushesTopTile) 
6 
mSpeed.y = Mathf.Min(0.0f, mSpeed.y); 
7 
if (mPS.pushesLeftTile) 
8 
mSpeed.x = Mathf.Max(0.0f, mSpeed.x); 
9 
if (mPS.pushesRightTile) 
10 
mSpeed.x = Mathf.Min(0.0f, mSpeed.x); 
And update the position. First off, let's save the old one.
1 
mOldPosition = mPosition; 
Calculate the new one.
1 
Vector2 newPosition = mPosition + mSpeed * Time.deltaTime; 
Calculate the offset between the two.
1 
Vector2 offset = newPosition  mPosition; 
Now, in case the offset is nonzero, we can call our Move function.
1 
if (offset != Vector2.zero) 
2 
Move(offset, mSpeed, ref mPosition, ref mRemainder, mAABB, ref mPS); 
Finally, update the object's AABB and the position state.
1 
mAABB.Center = mPosition; 
2 

3 
mPS.pushesBottom = mPS.pushesBottomTile; 
4 
mPS.pushesRight = mPS.pushesRightTile; 
5 
mPS.pushesLeft = mPS.pushesLeftTile; 
6 
mPS.pushesTop = mPS.pushesTopTile; 
That's it! This system now replaces the older one, the results should be the same, although the way we do it is quite a bit different.
Summary
That's it for laying down the groundwork for the slopes, so what's left is to fill up those gaps in our collision checks! We've done most of our caching work here and eliminated a lot of geometrical complexities by implementing the onepixel movement integration.
This will make the slope implementation a breeze, compared to what we'd need to do otherwise. We'll be finishing the job in the next part of the series.
Thanks for reading!