Advertisement Jump to content
Sign in to follow this  

Ray Picking and Heightmap terrain

This topic is 4968 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

im making a rts and successfuly got my terrain engine to work. It uses a heightmap. my question is, if i click on a point somewhere on the terrain, i want to know how to check which triangle the user clicked. I know how to do this using D3DXIntersect . I don't know how to convert my terrain data, into a mesh to use this technique. I don't really want an optimized code, just something that is easy and will work. any help or guidence is greatly appreciated.

Share this post

Link to post
Share on other sites
How is your terrain built? Using a binary triangle tree? If so, I would go down through the tree using a ray-plane intersect test (D3DXPlaneIntersectLine) at each level until you get to the leaf level which will then give you the intersect point and distance. You will need to un-project the screen co-ordinates first to get a point and vector for the intersection test. I did this using...

D3DVIEWPORT9 viewport;
D3DXMATRIX projectionMatrix, viewMatrix, worldMatrix;
D3DXVECTOR3 screenNear((float)in_screenX, (float)in_screenY, 0.0f);
D3DXVECTOR3 screenFar((float)in_screenX, (float)in_screenY, 1.0f);
D3DXVECTOR3 intersectPoint, nearPoint, farPoint, direction;
FLOAT dist=0.0f;

// convert to 3d
IDirect3DDevice9 *pDevice=in_engine.m_d3dDevice;
pDevice->GetTransform(D3DTS_PROJECTION, &projectionMatrix);
pDevice->GetTransform(D3DTS_VIEW, &viewMatrix);
pDevice->GetTransform(D3DTS_WORLD, &worldMatrix);
D3DXVec3Unproject(&nearPoint, &screenNear, &viewport, &projectionMatrix, &viewMatrix, &worldMatrix);
D3DXVec3Unproject(&farPoint, &screenFar, &viewport, &projectionMatrix, &viewMatrix, &worldMatrix);
D3DXVec3Normalize(&direction, &direction);

There may be a quicker way of doing it, but this would be my first stop. Obviously if you have a quadtree or octtree for your tiles you would traverse this first.

Share this post

Link to post
Share on other sites
I used quadtree to optimize my ray picking routine. It is very simple and fast to implement. If your terrain is huge, ray picking will slow down the process of computation. I suggest you incorporate quad tree implementation to get you computations faster. Inplementing quad trees will also allow you to do rendering optimizations.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!