Setting a new mouse location with mouse

Started by
5 comments, last by Anddos 12 years ago
What i want todo is set a new target to move to using the mouse, this is for the directx sdk sample , "multianimation" , when it picks a new location to move to it uses this function...


void CTiny::ChooseNewLocation( D3DXVECTOR3* pV )
{


pV->x = ( float )( rand() % 256 ) / 256.f;
pV->y = 0.f;
pV->z = ( float )( rand() % 256 ) / 256.f;

}


so instead of using this i want to use my own which is take the mouse 2d point and turn it in to a 3d vector position ...


void CTiny::SetNewClickTarge(D3DXVECTOR3* pV)
{
GetCursorPos(&point);
src.x = (int)point.x;
src.z = (int)point.y;
DXUTGetD3D9Device()->GetTransform(D3DTS_VIEW,&viw);
DXUTGetD3D9Device()->GetTransform(D3DTS_PROJECTION,&prj);
DXUTGetD3D9Device()->GetTransform(D3DTS_WORLD,&wld);


//D3DXMatrixIdentity(&wld);
DXUTGetD3D9Device()->GetViewport(&v9);


D3DXVec3Unproject(&r,&src,&v9,&prj,&viw,&wld);
pV->x = r.x;
pV->y = r.y;
pV->z = r.z;
}


when i run the sample and tick control , and move the mouse around nothing seems to happen?
:)
Advertisement
You cannot really convert a 2D point on the screen to a 3D point in the scene without additional specification, because a single 2D position on the screen corresponds to infinite number of points in 3D forming a line, or ray.

Try to google mouse picking. There also is an example in the DirectX SDK, called Pick. The example does a little bit more than what you need, it:
- builds the ray from the mouse position,
- casts the ray in the 3D space and tests which mesh it intersects with (and even which triangle of the mesh).

You need the first part (build the ray) and then I guess you'll probably want to find the intersection of this ray with a plane defined as y = 0 (the x-z plane). That will be your point. This step is just simple mathematics.

Few other notes:
- GetCursorPos returns the mouse position in screen coordinates. If your application runs in window, this coordinates will be completely unimportant and wrong for you. I would suggest using the windows messages, namely WM_MOUSEMOVE.
- It's better to avoid using the GetXXXX methods of the d3d9device. You should keep your own copies of the world, view, proj matrices. After all, it's you who created these matrices, so there really is no reason to set them first (by SetTransform) and then ask for them (by GetTransform).
I dont need to intersect with a mesh , just find out the 3d position from a 2d position mate.
:)
And are you sure you did read my WHOLE post, including the following parts?



You cannot really convert a 2D point on the screen to a 3D point in the scene without additional specification, because a single 2D position on the screen corresponds to infinite number of points in 3D forming a line, or ray.

....

You need the first part (build the ray) and then I guess you'll probably want to find the intersection of this ray with a plane defined as y = 0 (the x-z plane). That will be your point. This step is just simple mathematics.
You actually DO need to intersect with a mesh, in a manner of speaking. As Tom KQT state, you can not reverse-project a screen point to a single 3D point without additional information--namely, the depth of the point to project. Some reverse-projections will perform a read of the depth buffer to get the depth of the pixel at the mouse location, but if your purpose as stated is to find a target to move to, then this might not be optimal since the depth of the pixel might not lie on the movable area; it might be a piece of blocking geometry instead.

In your case, you need to compute the ray corresponding to the mouse location, then test intersection of that ray with any geometry forming the walkable or movable area, to find the point to move to. In the case that your walkable area is a simple flat plane, then this case is trivial and doesn't actually require testing against the geometry of your scene. Instead, you can just solve the case of the intersection of the ray against the ground plane.
right i see what you mean now
:)
what space does my ray need to be in then if the floor transform is like this


V( g_pEffect->SetTexture( "g_txScene", g_pTxFloor ) );
V( g_pEffect->SetMatrix( "g_mWorld", &g_mxFloor ) );


this is my pick code


void detect_picking(IDirect3DDevice9* pd3dDevice)
{
// get the current transform matrices
D3DXMATRIX matProjection, matView, matWorld, matInverse;
pd3dDevice->GetTransform(D3DTS_PROJECTION, &matProjection);
pd3dDevice->GetTransform(D3DTS_VIEW, &matView);
//pd3dDevice->GetTransform(D3DTS_WORLD, &matWorld);
g_pEffect->GetMatrix("g_mWorld",&matWorld);

// use the mouse coordinates to get the mouse angle
POINT MousePos;
GetCursorPos(&MousePos);
float xAngle = (((2.0f * MousePos.x) / 800) - 1.0f) / matProjection(0, 0);
float yAngle = (((-2.0f * MousePos.y) / 600) + 1.0f) / matProjection(1, 1);

D3DXVECTOR3 origin, direction;
origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
direction = D3DXVECTOR3(xAngle, yAngle, 1.0f);

// find the inverse matrix
D3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView));

// convert origin and direction into model space
D3DXVec3TransformCoord(&origin, &origin, &matInverse);
D3DXVec3TransformNormal(&direction, &direction, &matInverse);
D3DXVec3Normalize(&direction, &direction);

// detect picking
BOOL hit;
D3DXIntersect(g_pMeshFloor, &origin, &direction, &hit, NULL, NULL, NULL, NULL, NULL, NULL);
if(hit)
pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
else
pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
}


it seems to set the foor to wireframe no matter where the mouse is
:)

This topic is closed to new replies.

Advertisement