Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Ray - Tri intersection? - Check out this

This topic is 6534 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Ok, so you need a routine which checks if a ray intersects a poly. That''s a bit more difficult, but here is the one I used for my lightmap generator. The source is in delphi. function TRay.TriIntersect(v0, v1, v2: TVector): boolean; const EPSILON = 0.0001; var u, v: Single; edge1, edge2, tvec, pvec, qvec, d, o: TVector; det, inv_det: Double; t: single; ints: TVector; p: TPlane; begin Result := false; p := MakePlane(v0, v1, v2); o := MakeVector(px, py, pz); d := MakeVector(dx, dy, dz); // Find 2 edges sharing v0 edge1 := VecSubtract(v1, v0); edge2 := VecSubtract(v2, v0); // Calculate determinant pvec := CrossProduct(d, edge2); // If it is near 0, the ray is lying in the tri''s plane det := DotProduct(edge1, pvec); if (det > -EPSILON) and (det < EPSILON) then exit; inv_det := 1/det; // Calculate distance from v0 to ray-origin tvec := VecSubtract(o, v0); // Calculate u-param and check u := DotProduct(tvec, pvec) * inv_det; if (u < 0) or (u >= 1.0) then exit; // Prepare for testing v-param qvec := CrossProduct(tvec, edge1); // Calculate v-param and check v := DotProduct(d, qvec) * inv_det; if (v < 0) or (u + v > 1) then exit; t := DotProduct(edge2, qvec) * inv_det; if (t >= -EPSILON) and (t < len - EPSILON) then exit; // Get coordinates to intersection ints.x := px + dx * t; ints.y := py + dy * t; ints.z := pz + dz * t; if len < VecLength(VecSubtract(o, ints)) then exit; Result := true; end; I wasn''t the one to create this algorithm, I just adapted it a bit. But it should work. cu

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!