realbasic-games
[Top] [All Lists]

Re: line segment/triangle intersection in 3D

To: REALbasic Games <realbasic-games at lists dot realsoftware dot com>
Subject: Re: line segment/triangle intersection in 3D
From: Lo Saeteurn <realbasic at miensoftware dot com>
Date: Sat, 20 Aug 2005 09:21:46 -0700
Delivered-to: realbasic-games at lists dot realsoftware dot com
References: <a06200700bf2ac5f97ad1 at [66 dot 81 dot 214 dot 250]> <a06200715bf2b9c5dd723 at [10 dot 0 dot 1 dot 2]> <a06200700bf2c285d3d77 at [66 dot 81 dot 63 dot 195]>
I used this function that I translated from C. An intersection occurs when it returns 1 or 2. I is the point of intersection.

Function RayTriangleIntersect(P0 as vector3D, P1 as vector3D, v0 as vector3D, v1 as vector3D, v2 as vector3D, BYREF I as vector3D) as integer
  dim dir, w0, w,u,v,n as Vector3d // ray vectors
  dim r, a, b as Double // params to calc ray-plane intersect
  dim uu, uv, vv, wu, wv, D as Double
  dim s, t as Double
  const SMALL_NUM=0.00000001

  // get triangle edge vectors and plane normal
  u = V1.Minus(V0)
  v = V2.minus(V0)
  n = u.cross(v)        // cross product
  if (n.LenSquared=0) then        // triangle is degenerate
    return -1             // do not deal with this case
  end if

  dir = P1.minus(P0)         // ray direction vector
  w0 = P0.minus(V0)
  a = -n.dot(w0)
  b = n.dot(dir)
  if (abs(b) < SMALL_NUM) then // ray is parallel to triangle plane
    if (a = 0) then   // ray lies in triangle plane
      return 2
    else
      return 0
    end if         // ray disjoint from plane
  end if

  // get intersect point of ray with triangle plane
  r = a / b
  if (r < 0.0) then// ray goes away from triangle
    return 0              // => no intersect
  end if
  // for a segment, also test if (r > 1.0) => no intersect

  I = P0.plus(dir.Times(r))       // intersect point of ray and plane

  // is I inside T?

  uu = u.dot(u)
  uv = u.dot(v)
  vv = v.dot(v)
  w = I.minus(V0)
  wu = w.dot(u)
  wv = w.dot(v)
  D = uv * uv - uu * vv

  // get and test parametric coords
  s = (uv * wv - vv * wu) / D
  if (s < 0.0 or s > 1.0) then // I is outside T
    return 0
  end if
  t = (uv * wu - uu * wv) / D
  if (t < 0.0 or (s + t) > 1.0) then  // I is outside T
    return 0
  end if

  return 1                  // I is in T
End Function



On Aug 19, 2005, at 5:27 PM, Joe Raffanti wrote:

Thanks for all the feedback, everyone.
I think this is what you were referring to in Renegades:

Function PlaneIntersection(planeNormal As Vector3D, planeD As Double, p0 As Vector3D, p1 As Vector3D) As Vector3D // Find the intersection of the line from p0 to p1 with the given plane.
     // See Lengyel p. 85.
     Dim t As Double
   Dim V As Vector3D
     V = p1.Minus(p0)
   t = -(planeNormal.dot(p0) + planeD) / (planeNormal.Dot(V))
   return p0.Plus(V.Times(t))
  End Function

This looks like the right thing, but what does planeD refer to? ...I assume it is plane Depth or something like that, but then what do I measure the depth from? How would I set up the plane?

thanks,
Joe Raffanti


At 4:14 PM -0700 8/18/05, Joe Raffanti wrote:


Has anyone written a RB line segment / triangle intersection detection test?


I think there's one in Renegades, or in its precursor, the FPS demo described in RB Developer issue 1.3.


If so, could I use it?


Sure, help yourself. If that doesn't do what you need, then do a search for "Paul Bourke Geometry" -- Paul is an astronomer in Australia who has a ton of amazingly useful tutorials for this sort of thing.

Best,
- Joe

--
Joe Strout                          REAL Software, Inc.

Vote for REALbasic (twice!) in the LinuxWorld Reader's Choice Awards:
http://linux.sys-con.com/general/readerschoice.htm
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>




_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>