XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

How do I get the point in the triangle intersected with a ray

Last post 08-15-2008 2:09 AM by Wessam Bahnassi. 3 replies.
  • 08-12-2008 3:18 PM

    How do I get the point in the triangle intersected with a ray

    How do I get the point in the triangle intersected with a ray?
    The dist is the distance between the intersected point in trainagle and the origin of the ray?

    D3DXIntersectTri(&v_tri[0],&v_tri[1],&v_tri[2],&originW,&dirW,&U,&V,&dist);

  • 08-12-2008 4:12 PM In reply to

    Re: How do I get the point in the triangle intersected with a ray

    The output U and V parameters describe the barycentric coordinates of the hit point. To convert that value back to 3D cartesian space, do:

    1 float w = 1 - (u+v);  
    2 p.x = (v0.x * u) + (v1.x * v) + (v2.x * w);   
    3 p.y = (v0.y * u) + (v1.y * v) + (v2.y * w);   
    4 p.z = (v0.z * u) + (v1.z * v) + (v2.z * w); 

    where v0,v1,v2 are your triangle's vertices, and u,v are returned from D3DXIntersectTri.

    Wessam Bahnassi
    Microsoft DirectX MVP
  • 08-14-2008 1:34 PM In reply to

    Re: How do I get the point in the triangle intersected with a ray

    1 D3DXIntersectTri(&v1,&v2,&v3,&originW,&dirW,&U,&V,&dist);  
    2 D3DXVECTOR3 vec1=originW+dist*dirW;  
    3  
    4 float W=1-(U+V);  
    5 D3DXVECTOR3 vec2;//intersected point in triangle  
    6 vec2.x=(v1.x*W)+(v2.x*U)+(v3.x*V);  
    7 vec2.y=(v1.y*W)+(v2.y*U)+(v3.y*V);  
    8 vec2.z=(v1.z*W)+(v2.z*U)+(v3.z*V); 

    Is the vec1 equal to vec2?

  • 08-15-2008 2:09 AM In reply to

    Re: How do I get the point in the triangle intersected with a ray

    Yes they should be equal. In fact, calculating the hit position using distance is faster. Barycentric coordinates allow you to get other vertex properties at the hit location as well (e.g., normal, color, texcoords).
    Wessam Bahnassi
    Microsoft DirectX MVP
Page 1 of 1 (4 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG