Hi everyone,

I have been messing around with methods for doing basic collision detection recently. I was colliding with trees at the time, so I decided to use a simple radius test - i.e. if the camera comes within a distance 'rad' of any of the tree trunks in the scene, a collision has occurred. I initially started by simply reverting the camera back to its previous position before the collision occurred, thereby stopping the player in their tracks until their camera movement no longer intersects with the tree. This worked for a simple proof of the idea, but it was pretty ugly as a final implementation (being stopped in your tracks when you simply clip a tree lightly is not a fun experience after a few minutes).

I wanted to make a collision model which 'slides' you round the tree as you try to walk through it, rather than stopping you in your tracks as soon as you touch it. So, after some thinking, I came up with an easy way to do this. It's probably something people have seen before, but I'm going to post it here anyway in case anyone's interested in criticising it and discussing it. I'll try and describe the collision detection method assuming there's only one tree in the scene which we have to worry about. It can easily be adapted to handle more than one tree using things like for loops, etc... (I'm using it for 30 trees at the moment).

So, here's the method:

1. Check for collision. This is the simple part - every time you move your camera (or render a frame), check the distance of the camera from the tree's centre. Since the camera and tree are on the same level (the tree trunk extends out of the ground and above your head in the +y direction), we only need to worry about the x and z components of position:

D3DXVECTOR3 camTreeDist = D3DXVECTOR3(cam.x-tree.x, 0, cam.z-tree.z); //Vector describing the distance to the centre of the tree if (D3DXVec3Length(&camTreeDist) < rad){collision = TRUE;} //Check for collision

This code uses the built-in DirectX function D3DXVec3Length() to determine whether the distance between the tree's centre and the camera is less than a certain value (rad). If you aren't using DirectX, then it's simple to find the length of a vector using a different function, or your own code.

2. So you now know that your camera has collided with the tree and is inside its bounding radius. At this point, we could simply revert the position of the camera back to its previous position before the tree collision, and the camera would stand still until the player moves in a direction which doesn't intersect with the tree. This isn't a very fun experience, for the aforementioned reasons. So how can we make the player 'slide' around the tree like in most first-person video games? With vectors! This picture describes the problem:

3. We have walked into the tree's trunk to the location marked as 'position' in the diagram. We actually want to 'slide' around the edge of the tree instead of walking through it. The place where we actually want to be is marked as 'desired position' in the diagram. How do we get to this desired location? From the diagram, it is easy to see that we need to extend our vector 'camTreeDist' to our desired location in order to put us on the outside of the tree trunk, but also to avoid the problem of simply reverting back to the camera's old position and preventing fluid movement. How do we do this? By adding a constant amount of 'camTreeDist' to our camera's position to put us outside the tree (denoted by the dotted line). This ensures that we have moved around the tree, but we are still outside its collision radius, such that:

Rearranging this formula means that λ can be calculated using the following formula:

Remember that the modulus of a vector can be calculated in Direct3D using D3DXVec3Length(). λ here represents the amount of 'camTreeDist' that we must add to our position inside the tree in order to put us in the equivalent location if we were outside the tree (the dotted line in the diagram). So, now all that remains is to actually translate our camera into the desired position:

cam += ((rad/D3DXVec3Length(&camTreeDist))-1)*camTreeDist;

...and that's it! These simple formulae will ensure that your camera slides smoothly around obstacles rather than awkwardly stopping when a collision is detected. Of course, this will only work for 'radial' objects where you can model their boundaries as a simple radius. It could also be applied to objects like sprites rather than a first person camera.