• Advertisement
Sign in to follow this  

Pick an object and get the hit face

This topic is 3289 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, First of all, I have an object on my scene. I want let the user click and pick this object. I think that for testing intersection of the user click I have to use the ray technique. Is it true ? If it is, I found how to convert screen point into a ray. But, now I have the ray, how can I know if the ray hit my object. Also, I would like to know on in which face it has hit and the impact point. My Datas : all vertex positions and all index to build triangles. I would like to enrich my own math library, so not to use directx or opengl function. In fact, I want to build my own D3DXIntersect (http://msdn.microsoft.com/en-us/library/bb172882(VS.85).aspx) and I'm searching the math tools and data containers for do it. Can you help me ? Thanks, Aurélien P.S.: And sorry for my bad english

Share this post


Link to post
Share on other sites
Advertisement
Search for 'line-polygon intersections'. A ray is just like a line but with only one endpoint.

In short, the process is this:
- Convert click into ray (I think you have this part)
- Find where that ray intersects the plane of each polygon, perhaps having done some spatial partitioning to remove regions of the map
- Find out if that point is inside the polygon
- If it is, you have a hit
- If multiple planes hit, pick the closest.

Share this post


Link to post
Share on other sites
Erf... I'm try to get a ray from mouse coords but...

Can you explain me how to do ? Because net samples use always OpenGL or DirectX functions such as UnProject...

I a a view matrix and a projection matrix.


Thanks for your response,

Aurélien

Share this post


Link to post
Share on other sites
Oh, I thought from your first post you had got that part. It is quite easy: your screen space can be thought of as (tan alpha) and (tan phi) instead of x and y (with 0,0 at the middle). So any screen point gives you a direction in camera space. Multiply that direction by the camera projection matrix and subtract the camera's position and that will give you the direction vector for the line in world space.

Share this post


Link to post
Share on other sites
AFAIK the implementations of unprojecting in DirectX and OpenGL are openly available. That's what happens:


GR::tVector& XBasicRenderer::Unproject( GR::tVector& vect,
const XViewport& viewPort,
const math::matrix4& matProjection,
const math::matrix4& matView,
const math::matrix4& matWorld )
{

GR::tVector vectTemp;

vectTemp.x = ( ( vect.x - viewPort.m_dwX ) * 2.0f / viewPort.m_dwWidth - 1.0f );
vectTemp.y = 1.0f - ( vect.y - viewPort.m_dwY ) * 2.0f / viewPort.m_dwHeight;
vectTemp.z = 2 * vect.z - 1.0f;

math::matrix4 matComplete;

matComplete = matWorld * matView * matProjection;

matComplete.Inverse();

matComplete.TransformCoord( vectTemp );

vect = vectTemp;
return vect;

}

Share this post


Link to post
Share on other sites
So,

I've begin to implement this, but... what is the parameter vect ? It seems to be a vector with 3 components, so where are 2D coords ?


Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement