In 3d geometry, a plane is defined as a flat surface with no thickness: it's the 3-dimensional analog to a line. Imagine the top of a table. The top surface of the table is a plane. Any objects lying on the table would be said to be "in the plane" of the tabletop.
A particular plane can be specified in several ways; in UnrealScript, this is done with a vector and a float:
- the vector is the plane's normal vector: a unit vector perpendicular to it. This gives the orientation of the plane. Any one of an infinite number of parallel planes has this normal vector.
- the float is the distance from the plane to the origin. This picks out one particular plane.
The Plane struct extends vector in UnrealScript, so in theory all operations using vectors as parameters can be used with planes as well. In practice it has been found that native functions like dot
crash UT2003 when used with a plane – this could be a bug, but the dot function wouldn't make much sense on planes anyway. Other such peculiarities with the use of planes may also exist.
The plane struct can be used to describe other types of things for which a vector + a number makes sense:
- sphere (position vector of the centre + radius)
Pingz: We need a more technical description maybe. How about some examples of uses for planes and a nice graphic of one.
Wormbo: The plane
struct can also be used to describe spheres, so maybe some functions for that would be a nice addition.
Foxpaw: I'm confused. How can you specify a plane as only a distance from the origin and a normal describing the facing of the plane? How does it know which direction the distance from the origin is? Is the plane in these snippets required to be facing directly away from the origin?
Tarquin: the direction of the distance from the origin will be given by the normal vector – you'll get positive or negative. I can write some more but I'm, not sure how technical I can get
Foxpaw: So in other words the plane will always face directly toward/directly away from the origin? Hmm, actually when I think about it I guess every plane has to face the origin/face directly away from it at at least one point. I guess that would work after all.
Contents
Built-In Plane Functions
There are no native plane operators or functions in UT, UT2003, Unreal, or Unreal 2.
Other Useful Functions
Plane Creation From Point And Normal
This function constructs a plane from a normal vector and a point which lies on the desired plane. If the normal is of unit length then the resulting plane with be normalized.
static final function plane PlaneFromPointAndNormal( vector normal, vector point ) { local plane result; result.x = normal.x; result.y = normal.y; result.z = normal.z; result.w = normal dot point; return result; }
Normalize Plane
This function will normalize a plane which was not created from a unit normal. This allows for the calculation of exact distance to a point.
static final function plane PlaneNormalize( plane p ) { local float inverse_length; // vsize(p) returns the length of the vector // part of the plane ( the normal ), i.e. it // completetly ignores the W component of // the plane. inverse_length = 1.0f / vsize( p ); p.x *= inverse_length; p.y *= inverse_length; p.z *= inverse_length; p.w *= inverse_length; return p; }
Distance To Point
This will return the distance and the side which a point lies in relation to a plane. If the distance is 0 the point lies exactly on the plane. If the distance is positive the point lies in front of the plane. If the distance is negative the point is behind the plane. Note that to get the correct distance the plane must be normalized, else only the side will be accurate.
static final function float PlaneDistanceToPoint( plane p, vector point ) { local vector n; // Note: We have to use a temp here to hold // the normal part of the plane because calling // 'plane dot point', though the compiler // allows it, actually crashes UT2K3. n = p; return ( n dot point ) - p.w; }
Another technical note:
Here n = p
works in a similar way like assigning e.g. a Pawn to an Actor variable, except that a struct assignment copies the actual values while an object assignment only copies references, not the objects themselves.
Related Topics
Related Links
Foxpaw: Nifty. The PlaneDistancetoPoint function is exactly what I've been looking for. Been scratching my head to try to remember that stuff from high school. :P