Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Simple Plane Intersection Question

Started by cpcollis Dec 20, 2004 at 7:17 PM 4 replies 1.5k views
Original Post
cpcollis
cpcollis
Hi, I'm using Plane.IntersectLine to find the point where a line intersects a plane in C# and Managed DirectX. I was wondering how I can retrieve the distance from the origin of my ray to the hit point on the plane. Heres an example:

static void Main(string[] args) 
{ 
   // Ignore the values, they're just for show

   Plane test = new Plane( 0.0f, 1.0f, 0.0f, 0.0f ); 
   Vector3 origin = new Vector3( 5.0f, 10.0f, 5.0f ); 
   Vector3 end = new Vector3( 10.0f, -10.0f, 0.0f ); 

   Vector3 hitPoint = Plane.IntersectLine( test, origin, end ); 
}


I think I'm being really slow with this, is it simply:

float distance = Vector3.Length(hitPoint - origin);

// but I also remember this:
float distance = Vector3.Dot(hitPoint - origin, direction);

// Where "direction" is the direction the ray is travelling (end - origin).


Please confirm the answer, I having "one of those moments". Many Thanks, Chris
JohnBolton
JohnBolton
    float distance = Vector3.Length(hitPoint - origin); 
This is the distance between two points.
    float distance = Vector3.Dot(hitPoint - origin, direction); 
This is the distance to from a point to a plane (kind of).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Drilian
Drilian
Well, if you know the length of the ray's direction vector (D, in my example), then given:

(Using * for scalar product and . for dot product)Plane =>   PlaneNorm.x*x + PlaneNorm.y*y + PlaneNorm.z*z + PlaneDist = 0     (alternately, PlaneNorm . Pt + PlaneDist = 0)Ray = RayOrig + t * RayDir


You can get the distance to the plane by first plugging the ray equation into the plane equation and solving for t:
          PlaneNorm . RayOrig + PlaneDistt =  - ( --------------------------------- )             PlaneNorm . RayDir

(Note that if PlaneNorm . RayDir == 0, the plane and the ray do not intersect)

With t given, and you know the length of the line, the distance to the plane is:

dist = t * Length(RayDir)


So if you initialize your ray such that its direction vector's length is 1, then the distance to the plane is just t. Regardless, you can get the endpoint by:

Pt = RayOrig + RayDir * t


Also note that if t is negative, there is no intersection (well, the intersection is behind the origin of the ray...if you're raytracing, that's usually a bad thing).

In summary pseudo-code:

float Divisor = PlaneNorm . RayDirif Divisor == 0 return NO_INTERSECTfloat t = -( PlaneNorm . RayOrigin + PlaneDist ) / Divisorif( t < 0 ) return NO_INTERSECTfloat dist = t * RayDir.Length  // or just float dist = t when RayDir.Length == 1Vector intersectPoint = RayOrig + RayDir * t;return INTERSECT


Hopefully that helps a bit!


Josh
cpcollis
cpcollis
Eek! I'm so totally confused now! But thanks for your replies.

I'll try and clarify:

If the ray starts at "start", travelling in direction "dir", hits the plane at point "hit", then why isn't the distance to the plane, from the "start" not just the distance between the "start" and the "hit" point? This is where I am confused.

I say this because I'm using the Plane.IntersectLine function in MDX (why re-invent the wheel), the function returns a Vector3 of the point at which the ray hits the plane. I need both the point which the ray hits the plane (which is returned) and the distance from the origin of the ray to point it hits. In that sitatuation, is it correct to simply take Vector3.Length(hit - start);?

Thanks again
Drilian
Drilian
Sorry, I should have clarified my reasons for posting what I did. I think I misread your question in that by "slow" I thought you meant "non-optimized." Thus, I was giving you a slightly faster alternative to do the same thing.

The math behind what I posted is the same math (most likely) that's inside of Plane.IntersectLine. The trick is that you can get the length of the vector by looking at the "t" variable. Thus, you don't need the square root calculation that's necessary in the Length function.

If you don't need to eliminate that square-root call, then, yes,

distance = Vector3.Length(hitPoint - origin)
.
cpcollis
cpcollis
hehe, oh right! I thought that was a pretty complex answer to a simple question ;) very interesting anyway! Thanks for confirming, I'm willing to sacrifice a sq root for the sake of simplicity at this stage.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.