# 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.

## 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 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->GetViewport(&viewport);	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);	direction=farPoint-nearPoint;	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 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 12
• 10
• 11
• 13
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015480
×