Jump to content
  • Advertisement
Sign in to follow this  
Anddos

Stuck with d3d9 picking with 2 transformed models

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

Basically i am stuck with picking 2 transformed models, 1 model gets rotated and 1 model gets transformed x,y,z etc

so here is the code in rendering scene, what i understand everytime you call SetTransform , you Call GetTransform the same amount of times to get to the right matrix or am i wrong?


D3DXMATRIX matRotateY,matTrans;
D3DXMatrixRotationY(&matRotateY, index);
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY));
// draw the teapot
meshTeapot->DrawSubset(0);


D3DXMatrixTranslation(&matTrans,1.0f,0.0f,0.0f);
d3ddev->SetTransform(D3DTS_WORLD, &(matTrans));
//draw the box
meshBox->DrawSubset(0);


so now in my picking functions i have this


// get the current transform matrices
D3DXMATRIX matProjection, matView, matWorld,matWorld2, matInverse,matInverse2;
d3ddev->GetTransform(D3DTS_PROJECTION, &matProjection);
d3ddev->GetTransform(D3DTS_VIEW, &matView); //Inverse of Viewspace is World
d3ddev->GetTransform(D3DTS_WORLD, &matWorld); //rotation of teapot
d3ddev->GetTransform(D3DTS_WORLD, &matWorld2); //translation of box

// use the mouse coordinates to get the mouse angle
GetCursorPos(&MousePos);
float xAngle = (((2.0f * MousePos.x) / SCREEN_WIDTH) - 1.0f) / matProjection(0, 0);
float yAngle = (((-2.0f * MousePos.y) / SCREEN_HEIGHT) + 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 on rotated teapot
BOOL hit;
D3DXIntersect(meshTeapot, &origin, &direction, &hit, NULL, NULL, NULL, NULL, NULL, NULL);
if(hit)
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
else
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);


//Pick the Box
// find the inverse matrix
D3DXMatrixInverse(&matInverse2, NULL, &(matWorld2 * matView));

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


// detect picking on transformed box
D3DXIntersect(meshBox, &origin, &direction, &hit, NULL, NULL, NULL, NULL, NULL, NULL);
if(hit)
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
else
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);


}

Share this post


Link to post
Share on other sites
Advertisement
Hi.

In the famous words of Hunter "IT WORKS FOR ME"

so to does this


[color="#008000"][color="#008000"]//check for onclicks

D3DXMATRIX matWorld = *world;

[color="#008000"][color="#008000"]//frame->matCombined;

[color="#008000"][color="#008000"] D3DXMATRIX mWorldView = matWorld * matView;

D3DXMATRIX m;

D3DXMatrixInverse( &m, NULL, &mWorldView );



[color="#008000"][color="#008000"]// Transform the screen space pick ray into 3D space

[color="#008000"][color="#008000"] rayDir.x = v.x * m._11 + v.y * m._21 + v.z * m._31;

rayDir.y = v.x * m._12 + v.y * m._22 + v.z * m._32;

rayDir.z = v.x * m._13 + v.y * m._23 + v.z * m._33;

rayOrigin.x = m._41;

rayOrigin.y = m._42;

rayOrigin.z = m._43;



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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!