# How to normalize the direction to check if the player is able to move?

I have a room with 4 walls along the x and z axis respectively. My player, who is in first person (therefore the camera), should have collision detection with these walls. I'm relatively new to this so please bare with me. I believe the way to do this is to calculate the direction and distance to the wall from the camera and then normalize the directions. However, I can only get this far before I don't know what to do. I think you should work out the angle and direction your facing?

Here _dx and _dz is the small buffer in front of the camera.

float CalcDirection(float Cam_x, float Cam_z, float Wall_x, float Wall_z)
{
//Calculate direction and distance to obstacle.
float ob_dirx = Cam_x + _dx - Wall_x;
float ob_dirz = Cam_z + _dz - Wall_z;
float ob_dist = sqrt(ob_dirx*ob_dirx + ob_dirz*ob_dirz);

//Normalise directions
float ob_norm = sqrt(ob_dirx*ob_dirx + ob_dirz*ob_dirz);
ob_dirx = (ob_dirx)/ob_norm;
ob_dirz = (ob_dirz)/ob_norm;


Can anyone explain in layman's terms how I work out the angle?

• Are you aware of raycasting? – nathan Nov 7 '12 at 11:20
• do you mean, from the camera position you have, say a bounding box to help when you hit the wall? – SD1990 Nov 7 '12 at 11:27
• No. Take a starting point, for example, the head of your character and cast rays around his field of view. Each time a ray intersect something, store the distance from the starting point and the intersection point. However i'm not sure this is the best way to handle collision of a moving character in 3D (in fact i'm sure it's not a good way to go) but it looks like what you are trying to do. – nathan Nov 7 '12 at 11:34
• Also what are you trying to do? Because i'm wondering why are you trying to detect collision this way? It's for a real 3D game or are your trying to do something similar to Doom or Wolfenstein (using raycasting to draw a 3D representation of a 2D world) ? – nathan Nov 7 '12 at 11:38
• it could be said to be similar to doom, what im looking for is collision detection with the walls around the maze, which im sure has to be done by having say a bounding rectangle in direction of the camera and seeing if this touches a wall within a certain parameter and therefore i can not go any further. – SD1990 Nov 7 '12 at 11:41

This is actually a well documented problem. What you want isn't the distance from the camera to a specific point on the wall (Wall_x,Wall_z). Rather, you can find the point on a wall which is closest to the camera, at any time. This article should tell you all about the math involved, even though it's for ActionScript.