Author : Andreas Seidel
Page : << Previous 3
Go to page :

}

To move your player around in your level you have to modify his position. Your player always walks into one direction. That can be the player-angle if he walks forward, or any other angle, if he walks e.g. to the left or the right.

The first thing you have to do is to find out about the angle the player is moving. After that you need to calculate the x-unit and the y-unit of his movement. To get the x-unit you take the cosine of the angle, for the y-unit you take the sine. Once you have those basic units you multiply them by a number which represents the length of the player's movement.

If you want to move your player by 256 map-units, just multiply the x-unit and the y-unit by 256. That's all you have to do.

void move_player(int length, int direction)
{
int x_unit, y_unit;

x_unit = cos(direction) * length;
y_unit = sin(direction) * length;

playerx += x_unit;
playery += y_unit;
}

Well, your player is moving now. But you will notice that he can walk to whereever he wants to! He can just leave a sector and you won't even know about it!

Blocking the player
If you don't want the player to walk through your walls you need to test how far the player is away from the next wall. Just calculate the distance to the wall a ray hits with the angle that is equal to the players moving direction. If the distance is smaller than a certain value leave the move_player-function and stop him from walking through the wall!

The code for that is as follows:

void move_player(int length, int direction)
{
int x_unit, y_unit;
long distance;

distance = get_distance(direction);   // get the distance to a wall hit by the ray with angle 'direction'

if (distance < 256) return;           // leave the function if distance is too small

x_unit = cos(direction) * length;
y_unit = sin(direction) * length;

playerx += x_unit;
playery += y_unit;
}

Well, you should not try to block the player if you hit a transparent wall! Just test wether the wall you hit is transparent and decide to return afterwards.

Sector-transition
Ok, your player can now move around in one sector and can't walk through your walls. But if you'd allow him to just walk through a transparent wall, you'll get a black screen! Remember: The player is always in one sector the 'player_sector'. You check all the walls within that player_sector for possible intersection with your rays. But you don't test the walls of all the other sectors! So if you move the player outside the player_sector WITHOUT changing the player_sector, you'll get no correct intersections any more and you'll just see a black screen!

The only thing to do, is to test which sector the player is running into and change the value player_sector to the new sector-number. That's all. If you organize your level-data in the right way, you'll save the number of the next sector with every transparent wall, because it's easy then to get the needed information fast.

Conclusion
Well, this is the first part of my tutorial on advanced raycasting. I hope that you understood the main concept and that you're able now to program your own simply sector-based raycaster. In the next parts I will tell you how to implement floor- and ceiling-textures, lighting, doors, jumping, looking up and down and so on. I'll try to give you all the information you need to create an engine like REX3D which is the name of my 3D-engine. If you want to look at it to see how much you can get out of raycasting, please visit my homepage at

http://www.acs-home.de/rex3d
You can get the source-code of an older version of REX3D, if you mail me. My address is

rex3d@acs-home.de
So if you have any suggestions, questions or whatever, just tell me.

Reprinted with permission!

Page : << Previous 3