DirectX Camera to follow based on 3D Model's world Matrix?

后端 未结 2 1181
时光说笑
时光说笑 2021-01-26 10:06

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

相关标签:
2条回答
  • 2021-01-26 10:37

    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.

    0 讨论(0)
  • 2021-01-26 10:51

    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.

    0 讨论(0)
提交回复
热议问题