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?

  • \$\begingroup\$ Are you aware of raycasting? \$\endgroup\$ – nathan Nov 7 '12 at 11:20
  • \$\begingroup\$ do you mean, from the camera position you have, say a bounding box to help when you hit the wall? \$\endgroup\$ – SD1990 Nov 7 '12 at 11:27
  • \$\begingroup\$ 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. \$\endgroup\$ – nathan Nov 7 '12 at 11:34
  • \$\begingroup\$ 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) ? \$\endgroup\$ – nathan Nov 7 '12 at 11:38
  • \$\begingroup\$ 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. \$\endgroup\$ – 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.

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).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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