DirectX Camera to follow based on 3D Model's w

2019-03-06 00:23发布

I have multiple objects moving about in a 3D space and am looking for ways to, on button press, have the camera snap and follow the object chosen.

Is there a way to make use of each object's worldMatrix? (Below is an example of one object. This one is a planet that spins & orbits)

//set up matrices for rendering
D3DXMATRIX worldMatrixMer, viewMatrixMer, projectionMatrixMer;
m_Camera->GetViewMatrix(viewMatrixMer);
m_D3D->GetWorldMatrix(worldMatrixMer);
m_D3D->GetProjectionMatrix(projectionMatrixMer);
D3DXMatrixRotationX( &matRotateX, rx/65.0f);
//Rotate about Y axis
D3DXMatrixRotationY( &matRotateY, rotation * 15.0f);
D3DXMatrixRotationZ( &matRotateZ, rz/65.0f); 
//Collate Rot Matrices
D3DXMATRIX rotMatrixMer = matRotateX * matRotateY * matRotateZ;
D3DXVECTOR3 newVecDirMer;
D3DXVec3TransformCoord(&newVecDirMer, &initVecDirMer, &rotMatrixMer);
D3DXVec3Normalize( &currentVecDirMer, &newVecDirMer );
//Create the size of the object
D3DXMATRIX matScaleMer;
D3DXMatrixScaling(&matScaleMer, 0.1f, 0.1f, 0.1f);
//Starting position of object
D3DXMatrixTranslation(&matTranslateMer, 0.0f, 0.0f, 3.5983f * 3);
//Rotate about it's own axis
D3DXMatrixRotationY(&worldMatrixMer, rotation);
worldMatrixMer *= rotMatrixMer * matScaleMer * matTranslateMer;
//'Orbit'
D3DXMatrixRotationY( &matOrbit, (-1000.0f * rotation) / 88);
worldMatrixMer *= matOrbit;

I'm really trying to find an elegant way of making this happen, so any suggestions would be greatly appreciated.

Thanks

2条回答
不美不萌又怎样
2楼-- · 2019-03-06 00:38

I would approach this by setting the camera's position to the same as the object to be followed. If your objects are held in a vector this will make it easier to change what object to follow by simply incrementing your index in the vector to the next object. After applying the object's position to the camera's world position you should call a MatrixTranslation by the offset you want for your camera. These would be the values to play around with to get a good effect. The camera's lookat element should be equal to the object's position so that the camera is looking directly at it. And if you don't want the camera to look directly at the object you can translate that as well. Finally the up vector on the camera should point up of course.

Hope this helps.

查看更多
smile是对你的礼貌
3楼-- · 2019-03-06 00:56

Is there a way to make use of each object's worldMatrix?

Object matrix can be represented this way:

objx.x     objx.y     objx.z  0 //m[0][0]..m[0][3] or _11, _12, _13, _14
objy.x     objy.y     objy.z  0 //m[1][0]..m[1][3] or _21, _22, _23, _24
objz.x     objz.y     objz.z  0 //m[2][0]..m[2][3] or _31, _32, _33, _34
objpos.x   objpos.y   objpos.z  1 //m[3][0]..m[3][3] or _41, _42, _43, _44

Where m[][] and _11.._44 are corresponding elements of D3DMATRIX, objpos - object position vector, objx - object x ('local x" transformed to world space) vector, etc.

So as long as the last column (m[0..3][3]) is 0, 0, 0, 1 you can extract object position and its "x", "y", "z" vectors ("side", "up", "front" - which is which depends on application) from matrix. If last column is not "0, 0, 0, 1", then it is projection matrix and you can't extract object data from it this easily.

From there you could do anything you want with your camera.

查看更多
登录 后发表回答