Jump to content
  • Advertisement
Sign in to follow this  
vs322

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.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!