3.1. BASICS OF 2D MAPS - (VERY) SIMPLE PHYSICS
IntroOK, we start again with the BoF picture of the last lesson ^_-.
In the last lesson you saw how to make a very simple, plain map. But a map exists because entities (npcs, effects,...) are going to"live" inside it, the map is going to be their "world". And as any world, we need physics for that world. The physics can be has complicated as a "real" world, or as simple as a plain, 2D world. We will explain here how to make simple physics.
Movement - 2D basicsThe most important physical law in a game (forgetting line of sight :-P) is movement. Our entities are going to move in a world and they need to know whether they can move to a certain place or not. And if they are going to fall, or not. or if they can jump to a certain place, or not. Well, here we'll talk only of movement.
The most basic movement management in a 2D world is marking tiles as "impassable". Remembering the map of the previous lesson. We cannot pass through mountains and through the sea, but we can walk in the grass. Then, we can mark the mountain and sea tiles as "impassable".
There is two ways of marking a tile. First, we can embed this information in the tile. So any tile with the number "2" and "3" (mountain and sea) cannot be passed. Second, we can add another layer that stores the physical information, and we add a mark to any tile we want to make impassable.
Therefore, if somebody tries to pass over the sea or over the mountains... well, he/she will not pass :-P.
But... what happens if there is more than one layer? Remember last lesson again (e.g. Ground - Transparent - Roof). OK, it's simple: If the movement information is embedded in the tile, transparent layer will mark/unmark the "impassable" mark of the lower layers (e.g. the river tile). And if the information is stored in another layer... all remains the same.
Movement - 2D basics+OK, think now in the next example:
We have a player that wants to move through a wall. Well, that should be impossible... but with the techniques explaines in "Basics", there is no way we can avoid him to do it. Why?
There is two tiles, and none of them are impassable - the player can walk to any of these tiles. The problem comes when we want to avoid the player to go from one of those tiles to another (ey, there is a wall!). What we can do? Use flags.
We can use four flags to indicate whether a npc can (or not) pass from one tile to another. If the flag is OFF, he/she can pass. If the flag is ON, he/she canot pass.
But we still need the "impassable" mark, right? (the mountain...). Well, we can say that the impassable mark if when all the four flags are ON. Thus, a player cannot walk to a tile that has all the flags active.
This technique is used by the engine "RPG Maker 2000", developed by ASCII.
Line of SightAnother important issue inside the game world is the line of sight. In other terms: Whether an entity can see another one. Any entity must be aware of its surroundings, because it must interact with its enviroment.
A line of sight is basically done with a bresenham algorithm (see chapter LINKS). A bresenham algorithm traces a line between a point A (e.g. a npc) and a point B (e.g. a bad guy), and at every point in its way checks if it can see the next point. If all points can see each other, A can see B.
However, using the techniques exposed in previous sections (movement flags), we can calculate accurately the line of sight? No. There is a difference between walking to some place and seeing that place. We can see a man that is in the other side of the river, but we cannot walk to him.
Anyway, there is a simple technique for using movement flags in LoS. Any tile that you cannot pass through, but you can see through it, is marked as "Seeable". So, if in a test "A can see B" (being A adyacent of B) B is marked as seeable, the answer is inmediately yes.
We'll use this map as an example, where the Wolv "the cat" Rhine (upper-left corner) tries to see the human (lower part) and "bug" (upper-right corner):
Well, the rivers cannot be walked through, but anybody can see what is behind. So, the river tiles are marked as seeable. This way, the cat should see the human (can see above the river), but should not see "bug" (cannot see through the mountains).
Final WordsAll this lesson is oriented to tile-oriented movement, where one entity moves directly from one tile to another. In per-pixel movement, checks get a little bit more complicated.
Also, we didn't talk about gravity because there is no heights in the map. And we didn't talk about player collision (no npcs yet...). Well, there is a lot of things left here.
But remember: Physics are here very simple because we are in a 2D basic map. If we were working in a full-fledged 3D enviroment...
...we must care about lots of physics laws (gravity, speed, and so on [even wind :-P]) for making a real-like world.