realbasic-games
[Top] [All Lists]

Re: line segment/triangle intersection in 3D

To: <realbasic-games at lists dot realsoftware dot com>
Subject: Re: line segment/triangle intersection in 3D
From: Thomas Cunningham <mauitom at maui dot net>
Date: Fri, 19 Aug 2005 12:37:02 -1000
Delivered-to: realbasic-games at lists dot realsoftware dot com
Ugh, sorry for the noise, first response had an attachment.

This is for a line and a box from code by David Eberly of Magic Software and
his #D Game engine book. Sorry, but a casual search for his web site is
coming up empty.

Function LineBoxIntersection(segment As LineSegment3D, box As Box3D) As
Boolean
   // return value is 'true' if and only if objects intersect
   // Clipping of a linear component 'origin'+t*'direction' against an
   // axis-aligned box [-e0,e0]x[-e1,e1]x[-e2,e2] where 'extent'=(e0,e1,e2).
   // The values of t0 and t1 must be set by the caller.
   // If the component is a segment, set t0 = 0 and t1 = 1.
   // The values are (possibly) modified by the clipper.
   
   Dim AWdu(2),ADdu(2),AWxDdu(2), Rhs As Double
   Dim sDirection, sCenter, Diff As Vector3D
   
   sDirection = New Vector3D()
   sDirection = segment.direction.Times(0.50)
   sCenter = New Vector3D
   sCenter = segment.origin.Plus(sDirection)
   Diff = New Vector3D
   Diff = sCenter.Minus(box.center)              // tests requires a change
in the coord. system
   
   // x
   AWdu(0) = ABS(sDirection.Dot(box.axis(0)))
   ADdu(0) = ABS(Diff.Dot(box.axis(0)))
   Rhs = box.extent(0) + AWdu(0)
   
   If ADdu(0) > Rhs Then
      Return False
   End
   // y
   AWdu(1) = ABS(sDirection.Dot(box.axis(1)))
   ADdu(1) = ABS(Diff.Dot(box.axis(1)))
   Rhs = box.extent(1) + AWdu(1)
   
   If ADdu(1) > Rhs Then
      Return False
   End
   // z
   AWdu(2) = ABS(sDirection.Dot(box.axis(2)))
   ADdu(2) = ABS(Diff.Dot(box.axis(2)))
   Rhs = box.extent(2) + AWdu(2)
   
   If ADdu(2) > Rhs Then
      Return False
   End
   
   Dim WxD As Vector3D
   WxD = sDirection.Cross(Diff)
   //
   AWxDdu(0) = ABS(WxD.Dot(box.axis(0)))
   Rhs = box.extent(1) * AWdu(2) + box.extent(2) * AWdu(1)
   
   if AWxDdu(0) > Rhs Then
      Return False
   End
   //
   AWxDdu(1) = ABS(WxD.Dot(box.axis(1)))
   Rhs = box.extent(0) * AWdu(2) + box.extent(2) * AWdu(0)
   
   if AWxDdu(1) > Rhs Then
      Return False
   End
   //
   AWxDdu(2) = ABS(WxD.Dot(box.axis(2)))
   Rhs = box.extent(0) * AWdu(1) + box.extent(1) * AWdu(0)
   
   if AWxDdu(2) > Rhs Then
      Return False
   End
   
   //
   Return True

End Function

LineSegment3D is a
direction As Vector3D
origin As Vector3D

Box3D is a
axis(2) As Vector3D
center As Vector3D
extent(2) As Double

Perhaps that would help you.
Thomas C.





_______________________________________________
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>