Jump to content
  • Advertisement
Sign in to follow this  

Picking Accuracy Problem

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

What I am trying to do is output some code when I mouse over one of my meshes on the screen. It seems to be very accurate when I have a mesh close to my camera. As my camera moves away from the mesh it becomes less and less accurate. I am moving away in the z direction both the camera position and it's look at position. Sounds like to me it has something to do with my projection matrix, but I can't see the problem. Or is this inaccuracy common when moving away from meshes? Here is the function. I pass in the mouse X/Y pos, get the ray, then see if it intersects with any of my meshes.
D3DXVECTOR3 * World::checkForMeshHit(long mouseX, long mouseY){
	D3DXMATRIX matProjection, matView, matWorld, matInverse;
	LPDIRECT3DDEVICE9 * d3ddev = d3d->getD3ddev();
	(*d3ddev)->GetTransform(D3DTS_PROJECTION, &matProjection);
	(*d3ddev)->GetTransform(D3DTS_VIEW, &matView);
	float xAngle = (((2.0f * mouseX) / d3d->getScreenWidth()) - 1.0f) / matProjection._11;
	float yAngle  = (((-2.0f * mouseY) / d3d->getScreenHeight()) + 1.0f) / matProjection._22;
	D3DXVECTOR3 * direction = new D3DXVECTOR3(xAngle, yAngle, 1.0f);
	for(int i = 0; i < units.size(); i++){
                BOOL hit = false;
		direction = new D3DXVECTOR3(xAngle, yAngle, 1.0f);
		D3DXVECTOR3 origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
		EnemyUnit * unit = units;
		matWorld = (*unit->getMeshModel()->getWorldMat());
		D3DXMatrixInverse(&matInverse, NULL, &(matWorld*matView));
		D3DXVec3TransformCoord(&origin, &origin, &matInverse);
		D3DXVec3TransformNormal(direction, direction, &matInverse);
		D3DXVec3Normalize(direction, direction);
		D3DXIntersect(unit->getMeshModel()->getMesh(), &origin, direction, &hit, NULL, NULL, NULL, NULL, NULL, NULL);
			cout << "says its mousing over the object " << endl;
	return NULL;  

Here is initial camera code if that matters.
void D3DCreator::setupCamera(void){
	D3DXMATRIX matView;    // the view transform matrix
    &D3DXVECTOR3 (cameraPosX, cameraPosY, cameraPosZ),    // the camera position
    &D3DXVECTOR3 (cameraLookAtX, cameraLookAtY, cameraLookAtZ), // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView 

    // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                               1.0f,   // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

Share this post

Link to post
Share on other sites
float yAngle  = (((-2.0f * mouseY) / d3d->getScreenHeight()) + 1.0f) / matProjection._22;

Shouldn't that be -1.0f on the last float? commented code does help a lot as well.

Also what is causing your problem is that D3DXIntersect intersects a model positioned at (0,0,0) with no rotation, you should adjust your ray origin and ray direction accordingly.

Share this post

Link to post
Share on other sites
One thing I encountered in the past is the case when the mouse coords aren't what you think. That may be the case if you're using a window and getting the mouse coords from the outer frame and not the inner one, so the borders don't get counted. If you're using a window, I'd suggest you check that (for example, draw something where your program thinks the mouse is, see if there's an offset). If you're doing full screen, that won't happen, so it's another problem.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!