Part 1: Sphere/Plane Intersection
Collision Detection Tutorial
Collision detection is one of those required features for every 3d game engine.
At this point I assume that you've read the previous tutorials and understand them completely.
Simple as possible
This tutorial is meant to be as simple as possible, therefore you can't really do something useful with it. The 2nd part of this tutorial will teach you how the "real-thing" really works.
this tutorial is a bit useless because (as you know..) planes are infinite, and last time I checked I didn't see any infinite walls in quake :-)
Not letting the sphere get too close
The idea is not to let the sphere (player) get too close to the plane (wall).
Before doing this, you should make sure that every plane you've got has it's own normal, and D value (from the plane equation).
We use what was thought in the last tutorial, calculating a relative position of a vertex near a sphere.
The distance between a vertex (the sphere's center point) and the plane is calculated like that:
distance = dot product ( plane.normal , sphere.pos )
This distance is the blue line in the figure. You should already know that the numeric value of this distance can either be positive or negative.
If the blue line's length will be zero, then the sphere will be on the plane (which is not good.. ). That's the point where the yellow highlighted area comes into action -- If the blue line, is longer then the sphere's radius, then the yellow line exists. If the yellow line exists, the sphere hasn't reached the plane yet.
What if the sphere moves too fast?
If the sphere moves too fast it will cross the plane, but still be far enough from it, so the above check will fail!
As you might have guessed, there is a tricky solution for that!
We know that the distance to a plane can either be positive or negative.
On one side of the plane all distances are positive, on the other - they are negative.
If the sphere passed to the other side of the plane, the distance's numeric sign will change.
To check this, you need to calculate the distance to the plane in two cases:
- before you move the sphere.
- after you move the sphere.
If the sign changes then the sphere must have collided with the plane.
The attached demo
The attached demo shows you how to implement what's said above. Download It Here!
Part 2 of the Tutorial can be found here.
Collision Detection Tutorial - Part 2: Ray/Plane intersection