# Picking and ray / plain intersection [solved]

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

## Recommended Posts

Setting up a system that simply reports the X,Z cords that the mouse is pointing at a plain who's normal is 0,1,0. I started from the picking and math primers in Luna's intro to 3d programing book, and http://www.geocities.com/SiliconValley/2151/math3d.html(just a 3d math page). But my code is generating the wrong output entirely. My x,z results are simply too small and have some different relationship to my mouse pointer then what I intended. Anyway I hope some one here has implemented something like this before and has a better grasp of the math involved in picking / 3d math then I.
DxEngine::Ray DxEngine::GetRay(int iX, int iY)
{
float fPX = 0.0f;
float fPY = 0.0f;
fPX = (((2.0f*iX) / d3dVP.Width) - 1.0f) / proj(0,0);
fPY = (((-2.0f*iY) / d3dVP.Height) +1.0f) / proj(1,1);
DxEngine::Ray rRay;
rRay.origin = D3DXVECTOR3(0.0f,0.0f,0.0f);
rRay.direction = D3DXVECTOR3(fPX, fPY, 1.0f);
return rRay;
}
bool DxEngine::GetMouseAtZeroY(float* fXout , float* fZout)
{
*fXout = 0.0f;
*fZout = 0.0f;
DxEngine::Ray rRay=GetRay(iMouseX, iMouseY);
DxEngine::Ray* rpRay = &rRay;

D3DXMATRIX T;
pd3dDevice->GetTransform(D3DTS_VIEW, &T);

D3DXVec3TransformCoord(
&rpRay->origin,
&rpRay->origin,
&T);
D3DXVec3TransformNormal(
&rpRay->direction,
&rpRay->direction,
&T);

D3DXVec3Normalize(&rpRay->direction,&rpRay->direction);

D3DXVECTOR3 vecPlainN(0.0f, 1.0f, 0.0f); //normal of our plain with 0,0,0 lying on it.

float fDom= D3DXVec3Dot(&vecPlainN, &rpRay->direction);
if (abs(fDom) < 0.0001f)
return false;
else
{

float fScaler=(D3DXVec3Dot(&vecPlainN, &rpRay->origin))/fDom;
D3DXVECTOR3 vecResult=(rpRay->origin+(rpRay->direction * fScaler));
*fXout = vecResult.x;
*fZout = vecResult.y;
return true;
}
}

[Edited by - vs322 on September 30, 2007 8:10:20 AM]

##### Share on other sites
D3DXMatrixInverse(&T,0,&T);
after the
pd3dDevice->GetTransform(D3DTS_VIEW, &T);
line but my results are still not right.

It seems that they are flipped across the Z axis, but simply trying the negative doesn't work.

Edit; Got it!

float fScaler=(D3DXVec3Dot(&vecPlainN, &rpRay->origin))/fDom;
D3DXVECTOR3 vecResult=(rpRay->origin+(rpRay->direction * -fScaler));

needed to have my fScaler be negative.... Well hope someone can use my (now working) code.

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009843
• ### Who's Online (See full list)

There are no registered users currently online

×