Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i have a 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 dont know what to do. I think you should work out the angle and direction your facing?

where _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 laymen's terms how i work out the angle?

share|improve this question
    
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
show 4 more comments

1 Answer

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.

Once you know the point which is closest, finding the distance and direction is trivial. Actually, you can look at the whole thing as collision detection between a circle (around the camera) and a line (a wall) in 2D (seen top-down, ignoring the y dimension).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.