YOUR FEEDBACK
Three RIA Platforms Compared: Adobe Flex, Google Web Toolkit, and OpenLaszlo
NN wrote: Yeah you are right GWT is poor man's Flex. After using GWT on two...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
MXDJ TOP LINKS YOU MUST CLICK ON !


One From the Vault: How I Rolled My Own Collision Detection
A Helpful Addition to Your Toolbox

Digg This!

Recently in a Shockwave3D project, I had some problems with the modelsUnderRay command. More precisely, modelsUnderRay seemed to have some problems of its own. Occasionally it just didn't give the correct result, it would miss a model somehow. In the particular application I was building (a landscape that let the user's drive a car around on the surface), this was dramatic because the when modelsUnderRay failed, the car fell through the world. At first I started to develop little stop-gap bits of code that ensured that the built-in functions would work. And then it occurred to me, why not just write my own?

So in my application, I was firing a ray 'down' from the position of the car, and intersecting the land, and then moving the car 'up' so that it would ride on the surface. I was also orienting the car to the normal of the triangle that it was on top of. This is shown in Figure 1.

So as you can see from the diagram, missing the landscape would have rather drastic consequences. Additionally, in doing it myself, I was able to get not only the normal from the original triangle, but the blended normal that weights the three disparate normal at the vertices of the triangle, which allows for a much smoother motion across the surface. The desired "smooth normal" is shown in Figure 2.

So I needed a way to fire a ray at the landscape, see where it hit, and get the resulting normal vector. The code for my collision detection routine is generalized to the code in Listing 1 (which unlike my terrain example uses ALL of the objects in the world, in the same way that modelsUnderRay does). Note also that to debug this I used my 'Visual Debug Lines' that I presented in a previous MXDJ column (http://mxdj.sys-con.com/read/47857.htm).

The true "workhorse" of this routine is the ghTriangleRayCollision routine, that performs the magic of ray/triangle collision detection. This routine is based on an algorithm that I found at the SoftSurfer graphics site [see References, item1#], and that has been published several times in various places. A competing algorithm that could also be used would be the Akenine-Moller algorithm, which is presented in [see References, item #2]. The code for ghTriangleRayCollision is presented in Listing 2.

Finally, I wanted a way to average the normals of the triangle I hit, and get the normal of the exact point, or texel, within the triangle, weighted from the three triangle vertices. So I modified the code in Listing 1 to return not only the texture coordinates, but the three normals for the triangle that I hit. This is simply a matter of reading them out of the mesh in the same way the texture coordinates are read. Once I had the normals, then I knew the following information: the position of the points of the triangle (aTriangleHit in the returned array), the point of collision (vHitPosition in the returned array), and the normals at the vertices of the triangle (aTriangleNormals). With these three pieces of information I used a technique called Barycentric Coordinates to average the normals across the triangle. The Barycentric Coordinates are computed and returned as a vector of weights in the ghGetBarycentricCoords3D function in Code Listing 3.

The final application of the weights to the original vectors occurs in the ghGetTexelValues handler, that takes an array of values (the normals) and a vector that represents the BaryCentric weights of the point. The code for GetTexelValues is shown in Listing 4.

Using these methods, I rolled my own collision detection that was far more accurate than the modelsUnderRay command that exists in the 3D Xtra. It will detect backfacing collisions, will not return a collision when the ray intersects a model's bounding sphere but not the model, and is generally more precise around the edges of individual faces. Additionally, by adding some averaging methods, I was able to more smoothly interpolate across the surface of the triangle, rather than have sharp transitions between the normals of two adjoining triangles. This made for a much smoother terrain-following algorithm, without the overhead of constantly interpolating between the last and current face. Hopefully you too will find these functions a helpful addition to your toolbox in developing your own 3D worlds.

References:

  • Sunday, Dan. "Intersection of Rays, Segments, Planes and Triangles in 3D." The SoftSurfer Graphics Algorithms Collection. Available Online: www.softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm
  • Akenine-Moller, Thomas, and Eric Haines. Real-Time Rendering. 2nd Edition. A. K. Peters. Natick, Massachusetts, 2002.
  • About Andrew M. Phelps
    Andrew M. Phelps, a member of the Editorial Board of Web Developer's & Designer's Journal, is in the Information Technology Department at the Rochester Institute of Technology in Rochester, NY (http://andysgi.rit.edu/).

    Marty Plumbo wrote: This is a great article, at least in substance. The problem is, as previously commented, the source listing is completely unusable. I was able to parse it and remove all of the bad line-breaks (what fun) but then got snarled in several mistyped variable names. Andrew, is there any chance you could repost the sample code or just provide a working demo DIR file? Thanks!
    read & respond »
    Rich Mayo wrote: The misplaced line breaks make the code impossible to compile. Can you post a corrected version - or even better, a sample file?
    read & respond »
    LATEST FLEX STORIES & POSTS
    A Runtime Integration Approach to Application Development
    This pattern is a hybrid of plug-in and event driven architecture to integrate individual plug-ins together with one another to come up with Plug-in Integrator pattern. This pattern leverages the benefits of both these well-known architectures to provide a optimal solution to build ent
    AJAX World - Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
    At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaFX Scri
    AJAX World - Xceed Launches Microsoft Silverlight 2 Control
    Xceed launched Xceed Upload for Silverlight, the commercial offering in support of Microsoft's promising new Silverlight technology. The product is available now for purchase or as a fully functional 45-day trial on Xceed's website. Xceed Upload for Silverlight lets developers add uplo
    Microsoft To Keynote 4th International Virtualization Conference & Expo
    Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec
    AJAX World - Skyway Software Announces RIA Developer Contest
    According to Sean Walsh, President and CEO of Skyway Software, 'Our Skyway Community is thriving and our members are very talented. We truly look forward to their RIAs submittals and Skyway Builder extensions and are excited that all of the contributions will benefit the entire Skyway
    "Virtualization Journal" Debuts This Week at JavaOne
    Founded in 2006, SYS-CON Media's 'Virtualization Journal' is the world's first magazine devoted exclusively to what Gartner has earmarked as the single highest-impact IT trend through 2012: virtualization. And now it will be available on newsstands worldwide, as SYS-CON Media seeks to
    SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
    SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

    SYS-CON FEATURED WHITEPAPERS

    ADS BY GOOGLE