Sign in to follow this  

Picking and ray / plain intersection [solved]

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

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 this post


Link to post
Share on other sites
Ok, made some progress.... needed the inverse of the viewport, Added
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.

Share this post


Link to post
Share on other sites

This topic is 3733 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this