The 3D Story
Your guide to the third dimension.

Hiding Surfaces

How do we hide a surface? As I mentioned earlier each surface will bo given a direction which it is facing. When it is facing away from the camera it will be invisible and therefore not drawn.
The main probles here are to determine what direction the surface is facing and the angle between the surface's facing direction and the viewpoint.
This can be solved through some vector geometry. First, how do we find the angle in which the surface is facing, we just find the normal vector, n.


The two vectors A and B define the triangle while the n vector defines the facing direection. The n vector can be retrieved through the "cross product". Don't panic and we'll solve this in a nice and easy way.
I've prepared a formula in advance (with some help from my friend, Robert, thanks!). What we do first is to find the vectors A and B, they are :

ax=x2-x1
ay=y2-y1
az=z2-z1
bx=x3-x1
by=y3-y1
bz=z3-z1

The triangle is defined between the points (x1,y1,z1) (p1), (x2,y2,z2) (p2) and (x3,y3,z3) (p3).

Now, all we have to do is to calculate the vector n.

nx=ay*bz-az*by
ny=bx*az-ax*bz
nz=ax*by-bx*ay

Ok, you've got to admit that cross product sounds more dangerous than it actually is. Well, that done, we now will have to be able to calculate the angle between two vectors in 3D space. This is also rather easy, we just use the simple function "dot product". As I did with the cross product I'll take you through it nice and easy. Just relax and read on...


What we are looking for is the angle v between the vectors A and B. The formulas are (thanks again Robert!) :

|A|=sqrt(ax2+ay2+az2)
|B|=sqrt(bx2+by2+bz2)

cos(v)=(ax*bx+ay*by+az*bz)/(|A|*|B|)


Instead of transforming the v from a cos value to a value in degrees we can use it directly to check if the surface is visible.
The A vector will be the n vector of the surface in question and the B vector will be our viewing direction. This means that the B vector will be equal to (0,0,1) as we look into the scene along the Z axis.
As long as cos(v) is positive (>0) the surface is invisible. This makes it rather easy to show or hide surfaces.
to optimize even further, we can make some modifications to the formulas :

|A|=sqrt(ax2+ay2+az2)
|B|=1

cos(v)=(az)/(|A|)

By doing this optimisation we can get the angle alot faster (we save more than half the time it takes to calculate if the surface is visible or not (!)).



[ Previous | Main Page | Next ]
Copyright©1997 Johan E. Thelin