# Is there a way in which I could alta my cheap terrain picking code to consider the terrains height at the projected mouse points with out the need to

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

## Recommended Posts

Is there a way in which I could alta my cheap terrain picking code to consider the terrains height at the projected mouse points with out the need to lock the mesh

Hi there.
I'm trying to get the world location on the terrain based on the current mouse position with out locking the mesh.
The function below works well for when the map is flat but as soon as a unit walks up a platform the wold
position is off by heaps.

could I some how use the height map to do what I want but I will need a x , z value any way.

//code
[source]

//----------------------------------------------------------------------------------------------------
//this will return a x y z of the point clicked on the map in world coord
//returns 0,0,0 for no map selection
//-----------------------------------------------------------------------------------------------------
D3DXVECTOR3 Terrain::GetMapPoint(POINT &ptCursor,//the x y screen pos you click
D3DXMATRIX *pmproj,//the projection matrix
int width,
int height)//screen width and height
{

D3DXVECTOR3 RT = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

// Compute the vector of the pick ray in world space
//D3DXVECTOR3 v;
// Get the view matrix
D3DXMATRIX matView;
getViewMatrix(&matView);

D3D10_VIEWPORT vp;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
vp.Width = width;
vp.Height = height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;

D3DXMATRIX Identityworld;
D3DXMatrixIdentity( &Identityworld );

// Put mouse coordinates in screen space
D3DXVECTOR3 mnear = D3DXVECTOR3(ptCursor.x, ptCursor.y, 0);
D3DXVECTOR3 mfar = D3DXVECTOR3(ptCursor.x, ptCursor.y, 1);
// Transform points to world space

D3DXVec3Unproject(&mnear,
&mnear,
&vp,
pmproj,
&matView,
&Identityworld);

D3DXVec3Unproject(&mfar,
&mfar,
&vp,
pmproj,
&matView,
&Identityworld);

// Find Y Intercept
D3DXVECTOR3 direction = mfar - mnear;
if (mfar.y < 0) // if your mouse ray ends below XZ Plane
{
float yFactor = -mnear.y / direction.y;
RT = mnear + direction * yFactor; // zeroWorldPoint.X and zeroWorldPoint.Z contain XZ Plane Intercept
}

//I could now maybe some how use the heigh map to get the height value ????? but where do I get the run from to project the height

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[/source]

##### Share on other sites
Not sure how feasible this is, but have you considered somehow moving along the pick ray until you either meet the terrain or leave the map (I think this would work since you already know the mouse location in world co-ordinates)

On a unrelated note, you should probably use a more consise thread title.

##### Share on other sites
It looks like moving along the ray is the way to go.

this is what I have so far I start at the end of the ray.

float heighmapheight = 0.0f;
heighmapheight = getHeight(RT.x, RT.z);

//RT.y = heighmapheight;
D3DXVec3Normalize(&direction, &direction);
while(RT.y - heighmapheight < 10)
{
RT -= direction * 40;//step size may be smaller or larger not sure works so far set to this value
heighmapheight = getHeight(RT.x, RT.z);
}

Does this code snipt look right or should I try a different way. Or should I start at the start and work my way to the ground I'll try this now.

##### Share on other sites
Well, if RT begins on the same side of the terrain as the mouse is, I don't see any problems with the form of that code snippet. For the step size, you should probably have it considerably smaller than the heightfield tile size so that your code doesn't skip past the terrain (as could happen if your step size is too large and you try to click on a ridge, but you get a point behind the ridge instead)

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 24
• 44
• 75