简体   繁体   中英

Error control of directx camera rotation?

I use the mouse to control camera rotation in my program(using Directx 9.0c). Mouse X controls the camera to rotate around the Up Vector and Mouse Y controls the rotation around the Right Vector. Rotation caculation is as below: void Camera::RotateCameraUp(float angle) { D3DXMATRIX RoMatrix; D3DXMatrixRotationAxis(&RoMatrix, &vUp, angle); D3DXVec3TransformCoord(&vLook, &vLook, &RoMatrix); D3DXVec3TransformCoord(&vRight, &vRight, &RoMatrix); } void Camera::RotateCameraRight(float angle) { D3DXMATRIX RoMatrix; D3DXMatrixRotationAxis(&RoMatrix, &vRight, angle); D3DXVec3TransformCoord(&vLook, &vLook, &RoMatrix); D3DXVec3TransformCoord(&vUp, &vUp, &RoMatrix); } void Camera::RotateCameraUp(float angle) { D3DXMATRIX RoMatrix; D3DXMatrixRotationAxis(&RoMatrix, &vUp, angle); D3DXVec3TransformCoord(&vLook, &vLook, &RoMatrix); D3DXVec3TransformCoord(&vRight, &vRight, &RoMatrix); } void Camera::RotateCameraRight(float angle) { D3DXMATRIX RoMatrix; D3DXMatrixRotationAxis(&RoMatrix, &vRight, angle); D3DXVec3TransformCoord(&vLook, &vLook, &RoMatrix); D3DXVec3TransformCoord(&vUp, &vUp, &RoMatrix); } void Camera::RotateCameraUp(float angle) { D3DXMATRIX RoMatrix; D3DXMatrixRotationAxis(&RoMatrix, &vUp, angle); D3DXVec3TransformCoord(&vLook, &vLook, &RoMatrix); D3DXVec3TransformCoord(&vRight, &vRight, &RoMatrix); } void Camera::RotateCameraRight(float angle) { D3DXMATRIX RoMatrix; D3DXMatrixRotationAxis(&RoMatrix, &vRight, angle); D3DXVec3TransformCoord(&vLook, &vLook, &RoMatrix); D3DXVec3TransformCoord(&vUp, &vUp, &RoMatrix); } It is supposed that rotation around Up or Right vector should not leads to rotation around the "LookAt" vector, but if I circle my mouse for a while and stop it at the starting point, rotation around the "LookAt" vector has happened. I think it's because of the error while caculating, but I don't know how to eliminate it or control it. Any idea?

This is a common problem. You apply many rotations, and over time, the rounding errors sum up. After a while, the three vectors vUp , vLook and vRight are not normalized and orthogonal anymore. I would use one of two options:

1. Don't store vLook and vRight ; instead, just store 2 angles. Assuming x is right, y is top, z is back, store a) the angle between your view axis and the xz-Plane, and b) the angle between the projection of your view axis on the xz-Plane and the z-Axis or x-Axis. Update these angles according to mouse move and calculate vLook and vRight from them.

2. Set the y-component of vRight to 0, as vRight should be in the xz-Plane. Then re-orthonormalize the vectors (you know the vectors should be perpendicular to each other and have length 1). So after calculating the new vLook and vRight , apply these corrections:

vRight.y = 0
vRight = Normalize(vRight)
vUp = Normalize(Cross(vLook, vRight))
vLook = Normalize(Cross(vRight, vUp))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM