Advertisement

Ray - Tri intersection? - Check out this

Started by August 30, 2000 06:04 AM
-1 comments, last by LaBasX2 24 years, 2 months ago
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

This topic is closed to new replies.

Advertisement