Game Development Community

Please: What does the PlaneF intersect function return?!

by Josh Albrecht · in Torque Game Engine · 09/06/2001 (6:04 pm) · 7 replies

I dont really understand how to use the value returned by this function. It should be very easy for someone that knows anything, so please take a second to help. :) With this complete, I will have completely functional hitboxes (no joke this time... :)

Here is the function (it is really short):

inline F32 PlaneF::intersect(const Point3F &p1, const Point3F &p2) const
{
F32 den = mDot(p2 - p1, *this);
if(den == 0)
return PARALLEL_PLANE;
return -distToPlane(p1) / den;
}

PARRALLEL_PLANE is defined as:

#define PARALLEL_PLANE 1e20f

And the distToPlane function is defined as:

inline F32 PlaneF::distToPlane( const Point3F& cp ) const
{
// return mDot(*this,cp) + d;
return (x * cp.x + y * cp.y + z * cp.z) + d;
}

So how do I use the number returned? What does it represent? What the heck is 1e20f?

#1
09/06/2001 (7:27 pm)
I think I know what this is doing
First of all, p2 and p1 are two Point3F variables, representing any two points in a 3-dimensional space...
Analysis of F32 den = mDot(p2 - p1, *this):
By subtracting p1 from p2, you are creating a vector pointing from p2 to p1 (or is it the other way around? Ah well, its one of those)... and then taking the dot product of that vector and *this (in this case, *this is the vector representing your plane, and to represent a plane by a vector you use the normal of the plane)
If the dotproduct is 0, it means the two vectors are perpendicular... however since the *this was the normal of the plane, if the normal dotted into another vector equals 0, it means the plane and the vector are parallel.. it returns 1e20f (or 100000000000000000000.0) which is what they use as an "infinite" value since parallel lines/planes never intersect and 1e20 is a pretty big number =)

If it's not parallel, however, it returns the distance from P1 to the point where the line between P1 and P2 intersected the plane... to explain
_b_
      |  o <-P1
     a|@/c
 _____|/____
/_____+____/   @ is an angle
     /
    /
   o<-P2
Another way to determine the dot product of two vectors is to find the cosine of the angle between them...
In this case, den = cos(@)

Using trig, we see cos(@) = a/c thus c=a/cos(@)
side a is equal to the distance from P1 to the plane, which is why we have the distToPlane(p1)

The negative sign is there, just cause that's the way it worked out =) I never claimed my explanation would be perfect

So, intersect(p1,p2) returns the distance from point1 to the plane along a line between point1 and point2...

Hope that helps, and I hope it's right =)
-nohbdy
#2
09/07/2001 (12:07 pm)
That is what I suspected, but I dont want to build code and just cause that it is right. :)

What if it doesnt intersect between the start and finish? What then?
#3
09/07/2001 (2:01 pm)
Haven't checked, but from the code it looks like the intersection doesn't have to be between p1 and p2.

That is, p1 and p2 define a line (infinitly long), not a line segment (which only stretches from p1 to p2).
X.intersect(p1,p2) returns the distance from p1 to the intersection of plane X with the line extrapolated through p1 and p2, regardless of the length of the (p1-p2) vector.

-- Bob
#4
09/07/2001 (4:14 pm)
What Bob said
#5
09/07/2001 (5:17 pm)
Ok, I think I can find the length of the vector easily enough. But is a negative number or a positive one going to lie on the line?

(Excuse my retarded grammar before, I posted in a hurry. I mean assume instead of cause I think. :)
#6
09/07/2001 (5:39 pm)
Again, I haven't checked this, but it looks right from the code:

Say you have the result:

Q=X.intersect(p1,p2);

Imagine the line going through p1 and p2 as:

-----A-------(p1)-----B-------(p2)-------C-------


If Q is negative, the intersection occured in region A i.e. in the direction opposite to that of p2, from p1.

If Q is positive, the intersection is in region B or C.

If Q>0 and Q<((p2-p1).len()) then the intersection is in region B, between p1 and p2.

[ (p2-p1).len() returns the length of the line from p1 to p2, by the way. ]

-- Bob
#7
09/07/2001 (5:41 pm)
Thank you. Now I understand it. This has to be one of the fastest replies ever. (2 minutes or so I think. :)

My hitboxes are complete! :)