The first person is controlled, just like Quake, with the mouse control direction, with the keyboard to control the left and right. The mouse and keyboard use DirectInput control input. First, we can know that there are three components of the D3D view matrix, which are three vectors: the eyes are in the point, the point of view, and the upward direction. So, we first define three vectors: D3DXVector3 vDot, Vatpoint, VUP; Give the initial value: vdot = D3DXVector3 (2.0F, 0.0F, 2.0F); vatpoint = d3dxvector3 (0.0F, 0.0F, 0.0F); VUP = D3DXVECTOR3 (0.0F, 1.0F, 0.0F); Enter to View Matrix Matview: Void SetView () {D3DXMAMATRIXLOOKATLH (& Matview, & VDOT, & VatPoint, & VUP);} You can start making keyboard control forward, back, left, right Houned: We can see that in these controls, the VUP vector is not changeable, otherwise there will be the effect of tightening, as long as it controls the location of VDOT and VATPOINT. From the above picture, A point is VDOT, the B point is VPoint, AC is VUP vector. First, we calculate the back of the back: The forward retreat is actually moving along the direction at the same point and B point: the first step, calculate the vector AB: D3DXVEC3SUBTRACT (& AB, & VATPOINT, & VDOT); the second step, calculate the direction of mobile And steps: D3DXVEC3NORMALIZE (& Pout2, & Ab); pout2.x * = u; pout2.y * = u; pout2.z * = u; third step, add moving position to A and B in two points, You can get a new front-rear position. Pout = vdot; D3DXVEC3ADD (& vdot, & pout, & pout); pout = vatpoint; d3dxvec3add (& vatpoint, & pout, & pout); // * / setView (); Next, we calculate left and right movement, actually along the ABC Facial N simultaneously moves A and B: First step, calculate the normal of the ABC surface Vector N: D3DXVEC3CROSS (& Pout, & AB, & AC); other steps: D3DXVEC3NORMALIZE (& Pout2, & pout); pout2.x * = u; Pout2.y * = u; pout2.z * = u; pout = vdot; d3dxvec3add (& vdot, & pout, & pout); pout = vatpoint; d3dxvec3add (& vatpoint, & pout, & pout); set2); setView (); Next mouse control direction It is not that simple, it involves a point around the spatial rotation space, simply here is fixed A, so that B is rotated around a point of the A axis: We divide the direction into horizontal rotation and vertical rotation, Other directions are superimposed in both directions. To rotate around the arbitrate, we must know that the rotating shaft is in a point (VDOT) and its direction (A, B, C) (note that the ABC here is different, this is the value rather than point), Ask the transformation matrix.
First, the horizontal direction is rotated, which is the result of VATPoint winding around VUP: The number of direction must be standardized, ie the length is 1.
D3DXVEC3NORMALIZE (& Pout, & Ac); a = pout.x; b = pout.y; c = pout.z; v = (float) SQRT (C * C B * b); remove VDOT to the origin: r = D3DXMatrix (1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -VDOT.X, -VDOT.Y, -VDOT.Z, 1); // D3DXMatrixmultiply (& R, & R2, & RT); R2 = R; Rotate the current AB to the ZXO surface, the original transformation matrix is like this: D3DXMatrix (1, 0, 0, 0, 0, Cos (J1), SIN (J1), 0, 0, -SIN (J1), COS (J1), 0, 0, 0, 0, 1); but because COS (J1) = C / V; SiN (J1) = B / V; Since become the following matrix Rt = D3Dxmatrix (1, 0, 0, 0, C / V, B / V, 0, 0, -B / V, C / V, 0, 0, 0, 0, 1); D3DXMatrixMultiply (& R, & R2, & RT); R2 = R; Similarly: COS (J2) = V / | OA | = V / 1 = V (OA has been standardized) SIN (J2) = - A / | OA | = a; so get it The following matrix: RT = D3DXMatrix (V, 0, A, 0, 0, 1, 0, 0, -A, 0, V, 0, 0, 0, 0, 1); D3DXMatrixMultiply (& R, & R2, & RT) R2 = R; At this time, we can make the VATPOINT rotate around the VUP to the z-axis rotation U angle (radians) in the new coordinate system (arc) = D3DxMatrix ((float) sin (U) SIN (U) SIN ), 0, 0, - (Float) sin (u), (Float) COS (U), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); D3DXMatrixmultiply (& R, & R2, & Rt); R2 = R; Next inversion; RT = D3DXMatrix (V, 0, -A, 0, 0, 1, 0, 0, A, 0, V, 0, 0, 0, 0, 1); D3DXMatrixmultiply (& R, & R2, & RT); R2 = R; RT = D3Dxmatrix (1, 0, 0, 0, 0, C / V, -B) / V, 0, 0, b / v, c / v, 0, 0, 0, 0, 1); D3DXMatrixmultiply (& R, & R2, & RT); R2 = R; RT = D3DXMatrix (1, 0, 0, 0 , 0, 1, 0, 0, 0, 0, 1, 0, vdot.x, vdot.y, vdot.z, 1); D3DXMatrixmultiply (& R, & R2, & RT); at this time R is Vatpoint Wind VUP Rotating transformation matrix D3DXVEC3TRANSFORM (& VTemp, & Vatpoint, & r); vatpoint.x = vTemp.x;