Download presentation
Presentation is loading. Please wait.
Published byBaby Meaders Modified over 9 years ago
1
1 3D Vector & Matrix Chapter 2
2
2 Vector Definition: Vector is a line segment that has the direction. The length of the line segment is called the magnitude or size of the vector. If two vectors have the same magnitude and direction, then they are considered as the same vectors
3
3 Vector3(float, float, float) 3D DirectX has class public Vector3 ( float valueX, float valueY, float valueZ ) If we set the beginning point of a vector at origin, then the ending point of the vector is uniquely defined. Therefore we can use vector to define a point, like vertex.
4
4 Addition of Vector3 We can directly use + to add two vectors.
5
5 Subtraction of Vector3 We can directly use to subtract two vectors.
6
6 Dot Product of Vector3 static float Dot ( Vector3 left, Vector3 right ) If Vector3 v1 = new Vector3(a1, b1, c1); And Vector3 v2 = new Vector3(a2, b2, c2); Then v1 o v2 = a1×a2 + b1×b2 + c1×c2
7
7 Cross Product of Vector3 static Vector3 Cross( Vector3 left, Vector3 right )
8
8 Other methods of Vector3 float Length( ) Return the length of the vector Vector3 Normalize( ) Return the vector that has the same direction and length=1 Vector3 Multiply(float ) We can directly use to multiply a vector by a float number
9
9 3D Matrix An array of floats that represent a 4 x 4 matrix, where i is the row number and j is the column number. For example, M34 means the component in the third row and fourth column struct Matrix{ float M11, M12, M13, M14; float M21, M22, M23, M24; float M31, M32, M33, M34; float M41, M42, M43, M44; };
10
10 Matrix Properties Call Matrix.Identity to get an identity matrix. float Determinant { get; } static Matrix Identity { get; } static Matrix Zero{ get; } Call Matrix.Zero to get an empty matrix.
11
11 Matrix Rotation Methods Call Matrix.RotationX to get a matrix that represents the rotation about x-axis by angle static Matrix RotationX (float ) static Matrix RotationY (float ) static Matrix RotationZ (float ) static Matrix RotationAxis(Vector3 axis, float ) Get a rotation matrix about any axis
12
12 Actually,
13
13 Matrix Translation Methods This is the operation (x, y, z) (x+a, y+b, z+c) The matrix is static Matrix Translation( float a,float b,float c) static Matrix Translation(Vector3 v) Because that
14
14 Matrix Transpose Method If we have matrix static Matrix Transpose(Matrix m) Then its transpose matrix is
15
15 Matrix Inverse Method Matrix Invert() static Matrix Invert(Matrix m ) Let If A*B = I (identity matrix), then B is call the inverse matrix of A. Also A is the inverse matrix of B.
16
16 Matrix operator Method Matrix has +, , * operators static Matrix operator + ( Matrix m1, Matrix m2) Which means they can directly do operations static Matrix operator - ( Matrix m1, Matrix m2) static Matrix operator * ( Matrix m1, Matrix m2) Matrix m1, m2; Matrix m_sum = m1 + m2; Matrix m_minus = m1 - m2; Matrix m_product = m1* m2;
17
17 World Coordinate Every Matrix object is a transformation of the 3D space. The following is the code to set this transformation. In the above, the world coordinate of Figure 1 could also under transformation by Matrix m1; Matrix m1, m2; m_device.Transform.World = Matrix.Identity(); Draw Figure 1; // under transformation m1 m_device.Transform.World = m1; Draw Figure 2; // under transformation m1 Draw Figure 3; // under transformation m1 m_device.Transform.World = m2; Draw Figure 4; // under transformation m2 only
18
18 Matrix combination We have two transformations whose matrices are m1 and m2. If applying those two transformations consecutively to our 3D world, we need to do Matrix multiplication. In 3D program the left matrix takes action first. (In Mathematics, the right one takes action first.) Matrix matRotation, matTranslation; m_device.Transform.World = matTranslation* matRotation ; Draw Figure; // first translation then rotation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.