New tile based platform engine – more theory
This post continues New tile based platform engine – theory behind the player and answers some questions made in these days.
In this part I’ll explain how to determine collisions between the player and the walls.
Once I know player position (easy to know thanks to _x and _y for AS2 or x and y for AS3), and its horizontal and vertical speed (determined in various ways according to keys pressed, terrain types, gravity and so on), I can forecast its future position.
Once I know player’s future position, I have to check all four corner spots (the ones marked with a yellow pixel in the picture shown at New tile based platform engine – theory behind the player and see if these points are in an empty, walkable area.
If they are in an empty area, I just update player position and restart the process.
If at least one of the four corners are in a unwalkable area, I have to move the player as close as I can to its future position while keeping it into a walkable area.
First, let’s see how do I check for player corners
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function get_edges() {
// right edge
right = Math.floor((x_pos+5)/tile_size);
// left edge
left = Math.floor((x_pos-6)/tile_size);
// bottom edge
bottom = Math.floor((y_pos+8)/tile_size);
// top edge
top = Math.floor((y_pos-9)/tile_size);
// adjacent tiles
top_right = level[top][right];
top_left = level[top][left];
bottom_left = level[bottom][left];
bottom_right = level[bottom][right];
} |
As you can see, I just operate on x_pos and y_pos (player’s future x and y centre positions) adding or subtracting the right amount of pixels to reach the four corners.
Once I have the left, right, top and bottom tiles, I just have to retrieve values in the level array, so the tile type at the top left corner will be level[top][left], the one at the bottom right will be level[bottom][right] and so on.
Divide et impera
Also known as divide and conquer, this rule will make you life easier.
Let’s look at this picture:
The green player is falling down and moving to the right, and we calculated next frame position (the transparent one).
As you can see, red circles show us the corners that would collide into the wall.

To determine the last possible position (the last one with all four corners outside the wall) we should determine player’s angle using trigonometry and trackback its movement until all corners are clean.
While possible and quite accurate, this process is not so easy to perform because of the approximation of trigonometry functions.
It’s much better to check collisions this way

In this picture I divided the down-right movement in two separate movements: a down one and a right one.
It’s not that important which one you will check first: in any case you will end, in case of collision, with an horizontal or vertical movement to backtrack, that is much more easy to do, while not so correct in a real world ruled by physic.
Anyway, this is not a physic engine but a platform/arcade one, so this is absolutely the best way to check for collisions.
Some bug fixes
I would like to thank superdean that explained and fixed the so-called “infamous Ladder Bug”.
You can find all details in this post and it will be officially included in the code in next update.
There was another bug that caused player to get stuck into the wall if jumping to the right and hitting a wall.
This bug appeared in part 6 and was fixed in part 7.
Just remove these two instructions
1 2 | x_pos = Math.round(x_pos); y_pos = Math.round(y_pos); |
And everything will work fine.
Next update… coins and spikes!!
They can be easily customized to meet the unique requirements of your project.
4 Responses to “New tile based platform engine – more theory”
Leave a Reply
Trackbacks
-
New tile based platform engine - part 9 - coins n’ spikes : Emanuele Feronato on
October 7th, 2008 1:31 pm
[...] New tile based platform engine – more theory I said superdean fixed the ladder bug… that’s not completely true because with his code the [...]
- Citrus Engine released for free for learning
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.74/5)
- Create a flash artillery game – step 2 (4.74/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 1 (4.71/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(7 votes, average: 4.57 out of 5)



Great explanation. I think the pictures with the guy moving downwards with the red arrow are the same thing, and you meant to use a different picture showing the vectors for the second one. Just a heads up. Keep up the great work!
thank you – fixed
Hi!
Just want to mention that the link in the following part is dead:
“You can find all details in ->this post<- and it will be officially included in the code in next update."
Actually I wanted to say this at the end, but because I write you now anyway I will say it now (,too):
Your Tutorials are awesome. I just began with Flash and Action Script 3 and generally with this kind of stuff and your Tutorials are the best help I could find!
I will keep my text short now, because I will write you again anyway :-)
Thanks!