Tencent senior programmer puzzle game engine development, the proposal must learn math

For many boys playing a game, the game industry, there will be worthy of the love of work. But after the line know to play the game and develop the game are two different things!

 

Bring joy to countless people in the game, difficult to develop it? The entry barrier is high?

Numerous gaming industry vision of people have this question.

 

We start the engine and game development is closely related to talking about it.

 

The game engine technology developed very rapidly in a foreign country, but the domestic game industry started relatively late, coupled with practitioners sometimes anxious, talent accumulated engine is far less than foreign.

 

The game engine in the end what is it?

 

In the game the player view, the game screen, the better the performance of the force, the greater the degree of shocking scenes of the game, the more realistic gaming experience, the underlying game engine, the more powerful it may be.

(Source: Jedi escape)

 

From a professional point of view, the game engine means that some of the already-written editable computer game system, or some interactive real-time image of the application core components. These systems provide the tools they need to write games for game designers, its purpose is to allow game designers to easily and quickly without having to write a game program from scratch.

 

No matter what form of the game (a role-playing game, real-time strategy games, adventure puzzle games or action shooting game), even if it is only a 1 trillion games, also need game engine technology.

 

The game engine is equivalent frame, rear frame lay, level designers, modelers, animators can fill the inside content, it is the top priority for the development of the game.

 

 

Of course, the courage to grasp the nettle and many people, driven by interest, learning game engine technology development is not a distant thing. Well, today we give a stumbling block to talk about the first game engine to develop the road - a mathematical algorithm .

 

Basic Math

 

Speaking from VSMath this file

 

This document encapsulates the C language commonly used mathematical functions, as well as a large number of macro definitions.

Following macro definition is used to determine the accuracy, in particular determined two floating point numbers are equal, because the absolute is not equal, an error must be taken into account.

 
#define VSFRONT 0 #define VSBACK 1 #define VSON 2 #define VSCLIPPED 3 #define VSCULLED 4 #define VSVISIBLE 5 #define VSINTERSECT 3 #define VSOUT 4 #define VSIN 5 #define VSNOINTERSECT 6 //弧度和角度转换函数 inline VSREAL RadianToAngle(VSREAL Radian){  return ( Radian * 180.0f ) / VSPI ; } inline VSREAL AngleToRadian(VSREAL Angle){  return ( Angle * VSPI ) / 180.0f; } //判断是否为 2 N inline bool IsTwoPower(unsigned int uiN){  return !(uiN & (uiN - 1)); } inline unsigned short FloatToHalf(VSREAL Value) inline VSREAL HalfToFloat(unsigned short Value) inline unsigned int CompressUnitFloat(VSREAL f, unsigned int Bit = 16) inline unsigned int CompressFloat(VSREAL f, VSREAL Max , VSREAL Min ,  unsigned int Bit = 16) inline VSREAL DecompressUnitFloat(unsigned int quantized,unsigned int Bit = 16) inline VSREAL DecompressFloat(unsigned int quantized,VSREAL Max ,  VSREAL Min ,unsigned int Bit = 16) //计算正弦和余弦查找表,加快正弦和余弦计算速度 for (unsigned int i = 0 ; i <= 360 ; i++){  VSREAL iRadian = AngleToRadian(VSREAL(i));  FastSin[i] = SIN(iRadian);  FastCos[i] = COS(iRadian); } inline VSREAL VSMATH_API GetFastSin(unsigned int i); inline VSREAL VSMATH_API GetFastCos(unsigned int i); VSREAL GetFastSin(unsigned int i){  return FastSin[i]; } VSREAL GetFastCos(unsigned int i){  return FastCos[i]; }

 

The following two functions to calculate a hash index by a given length of data, 32-bit hash value is returned.

If a large amount of data provided, there may be a conflict, that is, two different data return the same value.

For the engine, the relevant data is not much, so the conflict is 0.

 
void VSInitCRCTable() unsigned int CRC32Compute( const void *pData, unsigned int uiDataSize )

 

Finally, SSE (Streaming SIMD Extensions, wherein SIMD Single Instruction Multiple Data is the abbreviation represents a Single Instruction Multiple Data) instructions for accelerated math library.

The book uses two versions of the library, is a compilation version, the other is a "high-level language", Premium Edition convenient than assembly language version to use.

 

VSFastFunction file which is used in the compilation SSE library, used in VSVector3, VSMatrix3X3, VSMatrix4X4 file-level language version of SSE library, easier to understand.

Conventional add instruction can only complete one addition, addition and SSE instructions in a library can be done up to four addition operations. Below are given SSE assembler version library and high-level language version.

 
//汇编版 SSE 库 void VSFastAdd(const VSMatrix3X3W & InM1,const VSMatrix3X3W & InM2,  VSMatrix3X3W & OutM) {  //VS 支持内嵌汇编  __asm  {  mov eax, [InM2];  mov ecx, [InM1];  movups xmm4, [eax];  movups xmm5, [eax+16];  movups xmm6, [eax+32];  movups xmm7, [eax+48];  mov eax, [OutM];  movups xmm0, [ecx];  movups xmm1, [ecx+16];  movups xmm2, [ecx+32];  movups xmm3, [ecx+48];  addps xmm0, xmm4;  movups [eax], xmm0;  addps xmm1, xmm5;  movups [eax+16], xmm1;  addps xmm2, xmm6;  movups [eax+32], xmm2;  addps xmm3, xmm7;  movups [eax+48], xmm3;  } } //高级语言版 SSE 库 void VSMatrix3X3W::operator -=(VSREAL f) {  __m128 _v1 = _mm_set_ps(m[0],m[1],m[2],m[3]);  __m128 _v2 = _mm_set_ps(m[4],m[5],m[6],m[7]);  __m128 _v3 = _mm_set_ps(m[8],m[9],m[10],m[11]);  __m128 _v4 = _mm_set_ps(m[12],m[13],m[14],m[15]);  __m128 _f = _mm_set_ps(f,f,f,f);  __m128 _r1 = _mm_sub_ps(_v1,_f);  __m128 _r2 = _mm_sub_ps(_v2,_f);  __m128 _r3 = _mm_sub_ps(_v3,_f);  __m128 _r4 = _mm_sub_ps(_v4,_f);  M[0][0] = _r1.m128_f32[3]; M[0][1] = _r1.m128_f32[2];  M[0][2] = _r1.m128_f32[1]; M[0][3] = _r1.m128_f32[0];  M[1][0] = _r2.m128_f32[3]; M[1][1] = _r2.m128_f32[2];  M[1][2] = _r2.m128_f32[1]; M[1][3] = _r2.m128_f32[0];  M[2][0] = _r3.m128_f32[3]; M[2][1] = _r3.m128_f32[2];  M[2][2] = _r3.m128_f32[1]; M[2][3] = _r3.m128_f32[0];  M[3][0] = _r4.m128_f32[3]; M[3][1] = _r4.m128_f32[2];  M[3][2] = _r4.m128_f32[1]; M[3][3] = _r4.m128_f32[0]; }

 

Basic Math Unit

 

Three-dimensional vector

 

VSVector3 represents three-dimensional vector, this type can represent a 3D vector can represent one point.

So this class provides 3D vector should have a function as a point in space and should have a function.

 
class VSMATH_API VSVector3 { public:  union  {  VSREAL m[3];  struct  {  VSREAL x, y, z;  };  }; }

 

Matrices and vector class, defines a union type, you can access the class attribute vectors by way of an array index or mode.

VSVector3 class correlation function as follows.

 
//长度 inline VSREAL GetLength(void)const; //长度的平方 inline VSREAL GetSqrLength(void) const; //乘以-1 inline void Negate(void); //单位化 inline void Normalize(void); //叉积 inline void Cross(const VSVector3 &v1,const VSVector3 &v2); //点积 VSREAL operator * (const VSVector3 &v)const; //两个向量的夹角(弧度) VSREAL AngleWith( VSVector3 &v); //用四元数旋转向量 VSQuat operator * (const VSQuat &q)const; //3×3 矩阵变换向量 VSVector3 operator * (const VSMatrix3X3 &m)const; //4×4 矩阵变换向量 VSVector3 operator * (const VSMatrix3X3W &m)const; //向量加减 void operator += (const VSVector3 &v); void operator -= (const VSVector3 &v); VSVector3 operator + (const VSVector3 &v)const; VSVector3 operator - (const VSVector3 &v)const; //向量和常量加减 void operator *= (VSREAL f); void operator /= (VSREAL f); void operator += (VSREAL f); void operator -= (VSREAL f); bool operator ==(const VSVector3 &v)const; VSVector3 operator * (VSREAL f)const; VSVector3 operator / (VSREAL f)const; VSVector3 operator + (VSREAL f)const; VSVector3 operator - (VSREAL f)const;

 

The reader should realize and fully understand the meaning of the above functions, in particular, as well as dot product and cross product of the matrix transformation.

 

Four-dimensional vector

 

It represents VSVector3W dimensional vector, w component class is added in VSVector3, mainly in order to facilitate the 4 × 4 matrix operation. This non-spatial transformation in a case where the component w to play a significant role. Another application scenario is used as a color.

 
class VSMATH_API VSVector3W typedef class VSVector3W VSColorRGBA;

 

The following is a correlation function of the color of the operation, are used to implement the conversion with 32-bit DWORD type 4 type float.

 
DWORD GetDWARGB()const; DWORD GetDWRGBA()const; DWORD GetDWBGRA()const; DWORD GetDWABGR()const; void GetUCColor(unsigned char &R,unsigned char &G,unsigned char &B,  unsigned char &A)const; void CreateFromARGB(DWORD ARGB); void CreateFromBGRA(DWORD BGRA); void CreateFromRGBA(DWORD RGBA); void CreateFormABGR(DWORD ABGR);

 

The following function of the combination of several colors are used to perform mutual conversion VSColorRGBA type and type DWORD.

 
inline DWORD VSDWCOLORARGB(unsigned char a, unsigned char r,  unsigned char g,unsigned char b){  return (DWORD)  ((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff))); } inline DWORD VSDWCOLORBGRA(unsigned char a, unsigned char r,  unsigned char g,unsigned char b){  return (DWORD)  ((((b)&0xff)<<24)|(((g)&0xff)<<16)|(((r)&0xff)<<8)|((a)&0xff))); } inline DWORD VSDWCOLORRGBA(unsigned char a, unsigned char r,  unsigned char g,unsigned char b){  return (DWORD)  ((((r)&0xff)<<24)|(((g)&0xff)<<16)|(((b)&0xff)<<8)|((a)&0xff))); } inline DWORD VSDWCOLORABGR(unsigned char a, unsigned char r,  unsigned char g,unsigned char b){  return (DWORD)  ((((a)&0xff)<<24)|(((b)&0xff)<<16)|(((g)&0xff)<<8)|((r)&0xff))); } inline void VSDWCOLORGetARGB(DWORD ARGB,unsigned char &a,  unsigned char &r, unsigned char &g,unsigned char &b){  a = (ARGB>>24) & 0xff;  r = (ARGB>>16) & 0xff;  g = (ARGB>>8) & 0xff;  b = (ARGB) & 0xff; } inline void VSDWCOLORGetBGRA(DWORD BGRA,unsigned char &a,  unsigned char &r, unsigned char &g,unsigned char &b){  b = (BGRA>>24) & 0xff;  g = (BGRA>>16) & 0xff;  r = (BGRA>>8) & 0xff;  a = (BGRA) & 0xff; } inline void VSDWCOLORGetRGBA(DWORD RGBA,unsigned char &a,  unsigned char &r, unsigned char &g,unsigned char &b){  r = (RGBA>>24) & 0xff;  g = (RGBA>>16) & 0xff;  b = (RGBA>>8) & 0xff;  a = (RGBA) & 0xff; } inline void VSDWCOLORGetABGR(DWORD ABGR,unsigned char &a,  unsigned char &r, unsigned char &g,unsigned char &b){  a = (ABGR>>24) & 0xff;  b = (ABGR>>16) & 0xff;  g = (ABGR>>8) & 0xff;  r = (ABGR) & 0xff; }

 

When graphics rendering, color conversion required between unsigned char, DWORD and float, different colors of a range of different format.

 

3 × 3 matrix

 

VSMatrix3X3 showing 3 × 3 matrix, 3 × 3 matrix in the transformation matrix is ​​mainly achieved for rotating, scaling, or a combination of both.

 

Need to be reminded of is: we must distinguish between a left-handed or right-handed, matrix and vector multiplication is left or right multiplication, and matrix is ​​the main line matrix or in the columns of the matrix-based, and understand how the positive direction of rotation is defined.

 

Below are a few functions to create a rotation matrix.

 
//通过一个朝向创建旋转矩阵 void CreateFromDirection(VSVector3 & Direction , const VSVector3 &Up = VSVector3(0,1,0)); void CreateRotX(VSREAL a); // 绕 x 轴旋转 void CreateRotY(VSREAL a); // 绕 y 轴旋转 void CreateRotZ(VSREAL a); // 绕 z 轴旋转 //绕 z 轴、x 轴和 y 轴构建欧拉角 void CreateEluer(VSREAL Roll,VSREAL Pitch, VSREAL Yaw) void CreateAxisAngle(const VSVector3 &vAxis, VSREAL a);//绕 vAxis 旋转 a 弧度  //通过 3 个基向量创建旋转矩阵 void CreateRot(const VSVector3 &U,const VSVector3 &V,const VSVector3 & N);以下几个函数也要了解一下。

 

Sometimes also obtain the row vector and column vector in the matrix of the engines, in particular it was before the rotation matrix from the direction of an object, the upper and right directions (three basis vectors U, V, N).

 
//按行获得向量 void GetRowVector(VSVector3 Row[3])const; //按行、按列获得向量 void GetColumnVector(VSVector3 Column[3])const; void GetRowVector(VSVector3 &Row0,VSVector3 &Row1,VSVector3 &Row2)const; void GetColumnVector(VSVector3 &Column0,VSVector3 &Column1,  VSVector3 &Column2)const; //获得基向量 void GetUVN(VSVector3 UVN[3])const; void GetUVN(VSVector3 & U,VSVector3 &V,VSVector3 &N)const;

 

Sometimes you need to create scaling matrix, most of the scaling engine according to origin.

 
//创建缩放矩阵,根据原点缩放 void CreateScale(VSREAL fX,VSREAL fY,VSREAL fZ); //根据轴缩放 void CreateScale(const VSVector3 & Axis,VSREAL fScale);

 

Amount of scaling and rotation amount acquisition from a matrix to be more commonly used.

 
void GetScale(VSVector3 & Scale)const; void GetScaleAndRotater(VSVector3 & Scale);

 

The following two functions are rarely used, this engine with two functions to seek point set OBB bounding boxes.

 
//构造一个行向量、一个列向量 inline void CreateFromTwoVector(const VSVector3 & v1,const VSVector3 & v2); //求特征值、特征向量 void GetEigenSystem(VSREAL EigenValue[3],VSVector3 Eigen[3])const;

 

And vector multiplication and matrix multiplication of the matrix to achieve the following functions.

 
inline VSMatrix3X3 operator * (const VSMatrix3X3 &Matrix)const; // 矩阵相乘 inline VSVector3 operator * (const VSVector3 &vc)const; // 矩阵和向量相乘

 

Multiplying the matrix only have practical significance, matrix multiplication can be regarded as a matrix of "addition."

Hearing this many readers might be confused, how is multiplied by the addition?

Learned "Discrete Mathematics" who have learned group theory, which is often referred to simply summing the addition of a specific range, different groups have their own additions.

 

For example: integer addition adder 1 is a conventional 1 + 1 = 2, 1 = 1 0 a subtraction, i.e. 1 + (- 1).

Adder matrix and is equal to M multiplied by M1 and M2, M1 is a matrix subtraction and multiplication of the inverse matrix M2, but this addition is not commutative.

Matrix "0" is the identity matrix, also known as E.

On the basis of this understanding, the concept of interpolation leads matrix.

Review the following code.

 
void VSMatrix3X3::LineInterpolation(VSREAL t,const VSMatrix3X3 & M1,  const VSMatrix3X3 &M2) {  *this = M1 * (1.0f - t) + M2 * t; } void VSMatrix3X3::Slerp(VSREAL t,const VSMatrix3X3 & M1,  const VSMatrix3X3 &M2) {  VSMatrix3X3 M1Transpose,Temp;  M1Transpose.TransposeOf(M1);  Temp = M1Transpose * M2;  VSREAL fAnagle;  VSVector3 Axis;  Temp.GetAxisAngle(Axis,fAngle);  Temp.CreateAxisAngle(Axis,fAngle * t);  *this = M1 * Temp; }

 

Essentially, the rotation matrix for the interpolation, the interpolation algorithm of a function is not correct, although the interpolation formula is M1 (1.0ft) + M2t, but the "+" represents matrix multiplication, the second function interpolation algorithm is correct.

In the second function, the interpolation formula is M1 (M1t) 1M2t, t here is not a simple matrix multiplication, the matrix becomes necessary axial and angular, angular and then multiplied by t.

 

4 × 4 matrix

 

VSMatrix3X3W indicates 4 × 4 matrix, the function is the most used in the spatial transform.

The following is a 4 × 4 matrix to create, scaling and rotation are achieved by VSMatrix3X3.

 
//用 3*3 矩阵创建 void CreateFrom3X3(const VSMatrix3X3 & Mat); //平移矩阵 void CreateTranslate(VSREAL dx, VSREAL dy, VSREAL dz); void CreateTranslate(const VSVector3 & V);

 

The following function creates a local object transformation matrix.

If two objects A, B are in the same space M, B to A is transformed into lower space, the object was to create a transformation matrix A.

Wherein, U, V, N basis vectors for the object A in the space M, Point A is the position in the M space.

 

As a simple example: if M is a world-space, all objects in world space to be transformed to camera space, the U, V, N is the camera in the three world space basis vectors (or axial), Point of the camera position in world space.

 
void CreateInWorldObject(const VSVector3 &U,const VSVector3 &V,const VSVector3 &  N,const VSVector3 &Point);

 

Bulletin board (Billboard) is a special patch, but introduce more here. Usually the bulletin board, there are two: one is for the complete camera, and more generally for particle; the other is only in the y-axis, the facing direction of the camera as possible.

 
//建立公告牌变换矩阵 void CreateFormBillboard(const VSVector3 &vcPos, //公告牌位置  const VSMatrix3X3 &CameraRotMatrix, //相机或其他矩阵  bool bAxisY); //是否只选择沿 y 轴旋转

 

Creating several matrix camera, perspective projection matrix orthogonal projection matrix, the matrix function of the viewport.

 
//构建相机矩阵(根据观察方向) bool CreateFromLookDir(const VSVector3 &vcPos, //相机位置  const VSVector3 &vcDir, //观察方向  const VSVector3 &vcWorldUp = VSVector3(0,1,0)); //构建相机矩阵(根据目标位置) bool CreateFromLookAt(const VSVector3 &vcPos, //相机位置  const VSVector3 &vcLookAt, //观察位置  const VSVector3 &vcWorldUp = VSVector3(0,1,0)); //上方向 //建立透视投影矩阵 bool CreatePerspective(VSREAL fFov , //x 方向的张角  VSREAL fAspect, //宽高比  VSREAL fZN , //近剪裁面  VSREAL fZF); //远剪裁面 //建立正交投影矩阵 bool CreateOrthogonal(VSREAL fW , //宽  VSREAL fH, //高  VSREAL fZN , //近剪裁面  VSREAL fZF); //远剪裁面 //建立视口矩阵 bool CreateViewPort(VSREAL fX,VSREAL fY,VSREAL fWidth,VSREAL fHeight,VSREAL fMinz,VSREAL fMaxz);

 

The following are some common functions, since the 3 × 3 matrix are obtained in function of the rotation and scaling components, it is not provided directly in the 4 × 4 matrix.

 
inline void Identity(void); //单位矩阵 inline void TransposeOf(const VSMatrix3X3W &Matrix); //转置 inline void InverseOf(const VSMatrix3X3W & Mat); //求逆 inline VSMatrix3X3W GetTranspose()const; //转置 inline VSMatrix3X3W GetInverse()const; //求逆 inline VSVector3 GetTranslation(void)const; //得到平移量 inline void Get3X3(VSMatrix3X3 & Mat)const; //得到 3*3 部分

 

Several commonly used are the following, but VSVector3 and 4 × 4 matrix multiplication with a 4 × 4 dot matrix multiplication, represents a spatial transformation point.

If you want to transform vector space should be used Apply3X3.

 
inline VSMatrix3X3W operator * (const VSMatrix3X3W &Matirx)const; // 矩阵相乘 inline VSVector3 operator * (const VSVector3 &vc)const; // 矩阵和向量乘 inline VSVector3W operator * (const VSVector3W &vc)const; // 矩阵和向量乘 //应用 3*3 的部分 inline VSVector3 Apply3X3(const VSVector3 &v)const; //应用平移 inline VSVector3 ApplyTranlate(const VSVector3 &Point)const;

 

Quaternion

 

VSQuat represents quaternion classes. The following function creates quaternions.

 
//通过旋转轴和旋转角构造四元数 void CreateAxisAngle(const VSVector3& Axis,VSREAL fAngle); //由欧拉角构造四元数 void CreateEule(VSREAL fRoll, VSREAL fPitch, VSREAL fYaw);

 

Get quaternion rotation matrix function written by a VSMatrix3X3 inside.

 
//得到欧拉角 void GetEulers(VSREAL &fRoll, VSREAL &fPitch, VSREAL &fYaw)const; //从四元数得到变换矩阵 void GetMatrix(VSMatrix3X3 &Matrix)const; //取得旋转轴和旋转角 void GetAxisAngle(VSVector3 & Axis , VSREAL & fAngle)const;

 

Quaternion some common functions as follows.

 
//单位化 void Normalize(); //求共轭 VSQuat GetConjugate()const; //得到长度 VSREAL GetLength(void)const; //求逆 VSQuat GetInverse()const; //求点积 VSREAL Dot(const VSQuat& q)const; //求共轭 VSQuat operator ~(void) const; //求幂 VSQuat Pow(VSREAL exp)const; //求以 e 为底的对数 VSQuat Ln()const; //求以 e 为底的指数 VSQuat Exp()const; void operator /= (VSREAL f); VSQuat operator / (VSREAL f)const; void operator *= (VSREAL f); VSQuat operator * (VSREAL f)const; VSQuat operator * (const VSVector3 &v) const; VSQuat operator * (const VSQuat &q) const; void operator *= (const VSQuat &q); void operator += (const VSQuat &q); VSQuat operator + (const VSQuat &q) const; void operator -= (const VSQuat &q); VSQuat operator - (const VSQuat &q) const; bool operator ==(const VSQuat &q)const;

 

The sample code below quaternion rotation.

 
//求 q2 绕 q1 旋转后的四元数 void Rotate(const VSQuat &q1, const VSQuat &q2); //旋转一个向量 VSVector3 Rotate(const VSVector3 &v)const;

 

Quaternion interpolation to achieve refer to the relevant "3D mathematical basis: Graphics and Game Development," a book.

 
//插值 void Slerp(VSREAL t,const VSQuat & q1,const VSQuat & q2); //三角形二维球型插值 void TriangleSlerp(VSREAL t1,VSREAL t2, const VSQuat & q1,const VSQuat & q2,  const VSQuat & q3); //四元数样条插值 void Slerp(VSREAL t,const VSQuat & q1,const VSQuat & q2,const VSQuat & s1,  const VSQuat & s2); void SlerpSValueOf(const VSQuat & q1,const VSQuat & q2,const VSQuat & q3);

 

The basic graphic element

 

This section describes the basic engine commonly used primitives camera crop, ray detector, collision with an object so closely associated with them, each entity is a class, class attributes are defined according to the spatial geometry of the package, class the method is also very easy to classify, mainly with the positional relationship of the other elements, or it is determined ( "computer graphics algorithm tool geometry Explanation" book detailed description of the algorithm) and the other elements of the distance.

The following figure shows the inheritance primitive class.

 

 

point

 

VSVector3 is represented by the class.

 

Line, ray, line segment

 

VSLine3 denotes straight line defined as a point and a direction, differs ray rectilinear direction may be a negative direction, that direction must be unitized.

 
P = P0 + tDir

The above equations are linear parametric equations, t is a parameter.

Given t, can be calculated P; given P, then can be calculated t.

 
//给定点 P,求 t bool GetParameter(const VSVector3 &Point,VSREAL &fLineParameter )const; //构建直线 inline void Set(const VSVector3 & Orig,const VSVector3 &Dir); //得到 P0 和 dir inline const VSVector3 & GetOrig()const; inline const VSVector3 & GetDir()const; //给定 t,求 P inline VSVector3 GetParameterPoint(VSREAL fLineParameter)const;

 

The following function for determining the positional relationship between the line and other elements.

 
//判断直线与三角形的位置关系。bCull 为是否为背面剪裁,是否考虑朝向,t 返回相交长度 //VSNOINTERSECT VSNTERSECT int RelationWith(const VSTriangle3 & Triangle, bool bCull,VSREAL &fLineParameter,  VSREAL fTriangleParameter[3])const; //判断直线与平面的位置关系 //VSNOINTERSECT VSNTERSECT VSON VSBACK VSFRONT int RelationWith(const VSPlane3 &Plane, bool bCull,VSREAL &fLineParameter)const; //判断直线与矩形的位置关系 //VSNOINTERSECT VSNTERSECT int RelationWith(const VSRectangle3 &Rectangle,bool bCull,VSREAL &fLineParameter,  VSREAL fRectangleParameter[2])const; //判断直线与球的位置关系 //VSNOINTERSECT VSNTERSECT int RelationWith(const VSSphere3 &sphere, unsigned int &Quantity,VSREAL &tNear,  VSREAL &tFar)const; //判断直线与 OBB 的位置关系 //VSNOINTERSECT VSNTERSECT int RelationWith(const VSOBB3 &OBB, unsigned int &Quantity,VSREAL &tNear,VSREAL  &tFar)const; //判断直线与 AABB 的位置关系 //VSNOINTERSECT VSNTERSECT int RelationWith(const VSAABB3 &AABB, unsigned int &Quantity,VSREAL &tNear,VSREAL &  tFar)const; //判断直线与多边形的位置关系 //VSNOINTERSECT VSNTERSECT int RelationWith(const VSPolygon3 &Polygon,VSREAL &fLineParameter, bool bCull,int &  iIndexTriangle,VSREAL fTriangleParameter[3])const;

 

The following function is used to calculate the distance of the straight line and the other elements.

 
//计算点到直线的距离 VSREAL SquaredDistance(const VSVector3 &Point,VSREAL &fLineParameter)const; //计算直线和直线的距离 VSREAL SquaredDistance(const VSLine3 &Line,VSREAL &fLine1Parameter,VSREAL &fLine2  Parameter)const; //计算直线和射线的距离 VSREAL SquaredDistance(const VSRay3 &Ray,VSREAL &fLineParameter,VSREAL &  fRayParameter)const; //计算直线和线段的距离 VSREAL SquaredDistance(const VSSegment3 & Segment,VSREAL &fLineParameter,VSREAL &  fSegmentParameter)const; //计算直线和三角形的距离 VSREAL SquaredDistance(const VSTriangle3& Triangle,VSREAL &fLineParameter,VSREAL  fTriangleParameter[3])const; //计算直线和矩形的距离 VSREAL SquaredDistance(const VSRectangle3& Rectangle,VSREAL &fLineParameter,VSREAL  fRectangleParameter[2])const; //计算直线和 OBB 的距离 VSREAL SquaredDistance(const VSOBB3 & OBB,VSREAL &fLineParameter,VSREAL  fOBBParameter[3])const; //计算直线和球的距离 VSREAL Distance(const VSSphere3 &Sphere,VSREAL &fLineParameter,VSVector3 &  SpherePoint)const; //计算直线和平面的距离 VSREAL Distance(const VSPlane3 &Plane,VSVector3 &LinePoint,VSVector3 &PlanePoint)  const; //计算直线和 AABB 的距离 VSREAL SquaredDistance(const VSAABB3 &AABB,VSREAL &fLineParameter, VSREAL  fAABBParameter[3])const; //计算直线和多边形的距离 VSREAL SquaredDistance(const VSPolygon3 & Polygon,VSREAL &fLineParameter,int&  IndexTriangle,VSREAL fTriangleParameter[3])const;

 

VSRay3 represents only difference rays, rays, and in that the line t may be negative.

VSSegment3 line represents, a straight line segment is different from, which has properties endpoints.

For P = P0 + tDir it, t is the range, it is defined in two ways: the first based on two points, based on the direction and the second length.

 
inline void Set(const VSVector3 &Orig,const VSVector3 &End); inline void Set(const VSVector3 &Orig,const VSVector3 &Dir,VSREAL fLen);

 

Flat, triangular, rectangular, polygonal,

 

VSPlane shows a plane, the plane equation is parameterized N (P0 - P1) = 0.

Normal to the plane perpendicular to the plane on all the lines, simplified NP0 + D = 0, D = NP1 is a constant; the point P on the plane satisfy all NP + D = 0.

Here are a few functions to create a plane.

 
//通过平面法向量和平面上一点确定一个平面 inline void Set(const VSVector3 &N, const VSVector3 &P); //通过平面法向量和 D 确定一个平面 inline void Set(const VSVector3 &N , VSREAL fD); //通过 3 个点确定一个平面 inline void Set(const VSVector3 &P0, const VSVector3 &P1, const VSVector3 &P2); inline void Set(const VSVector3 Point[3]);

 

The following function is used to calculate the distance from the plane of the other elements.

 
//计算点到平面的距离 VSREAL Distance(const VSVector3 &Point,VSVector3 &PlanePoint)const; //计算平面和球的距离 VSREAL Distance(const VSSphere3 &Sphere,VSVector3 & SpherePoint)const; //计算直线和平面的距离 VSREAL Distance(const VSLine3 &Line,VSVector3 &PlanePoint, VSVector3 &LinePoint)const; //计算射线和平面的距离 VSREAL Distance(const VSRay3 & Ray,VSVector3 &PlanePoint,VSVector3 &RayPoint)const; //计算线段和平面的距离 VSREAL Distance(const VSSegment3 & Segment,VSVector3 &PlanePoint, VSVector3 & Segment Point)const; //计算平面和平面的距离 VSREAL Distance(const VSPlane3 &Plane,VSVector3 &Plane1Point,VSVector3 & Plane2Point) const; //计算平面和三角形的距离 VSREAL Distance(const VSTriangle3 &Triangle,VSVector3 &PlanePoint, VSVector3 & Triangle Point)const; //计算矩形和平面的距离 VSREAL Distance(const VSRectangle3 &Rectangle,VSVector3 &PlanePoint,VSVector3 & Rectangle Point)const; //计算 OBB 和平面的距离 VSREAL Distance(const VSOBB3& OBB,VSVector3 &PlanePoint,VSVector3 & OBBPoint)const; //计算 AABB 和平面的距离 VSREAL Distance(const VSAABB3 &AABB,VSVector3 &PlanePoint,VSVector3 & AABBPoint)const; //计算平面和多边形的距离 VSREAL Distance(const VSPolygon3 &Polygon,VSVector3 &PlanePoint,int& IndexTriangle, VSVector3 &TrianglePoint)const;

 

The following function for determining the positional relationship with other elements of the plane.

 
//判断点和平面的位置关系 //VSFRONT VSBACK VSPLANAR int RelationWith(const VSVector3 &Point)const; //判断直线和平面的位置关系 /VSNOINTERSECT VSNTERSECT VSON VSBACK VSFRONT int RelationWith(const VSLine3 &Line, bool bCull,VSREAL &fLineParameter)const; //判断射线和平面的位置关系 //VSNOINTERSECT VSNTERSECT VSON VSBACK VSFRONT int RelationWith(const VSRay3 &Ray, bool bCull,VSREAL &fRayParameter)const; //判断线段和平面的位置关系 //VSNOINTERSECT VSNTERSECT VSON VSBACK VSFRONT int RelationWith(const VSSegment3 &Segment, bool bCull,VSREAL &fSegmentParameter) const; //判断平面和 OBB 的位置关系 //VSFRONT VSBACK VSINTERSECT int RelationWith(const VSOBB3 &OBB)const; //判断平面和 AABB 的位置关系 //VSFRONT VSBACK VSINTERSECT int RelationWith(const VSAABB3 &AABB)const; //判断平面和球的位置关系 //VSFRONT VSBACK VSINTERSECT int RelationWith(const VSSphere3 &Sphere)const; //判断平面和三角形的位置关系 //VSON VSFRONT VSBACK VSINTERSECT int RelationWith(const VSTriangle3 &Triangle)const; int RelationWith(const VSTriangle3 &Triangle ,VSSegment3 & Segment)const; //判断参数平面和平面的位置关系 //VSNOINTERSECT VSINTERSECT int RelationWith(const VSPlane3 &Plane)const; int RelationWith(const VSPlane3 &Plane,VSLine3 &Line)const; //判断平面和矩形的位置关系 //VSON VSFRONT VSBACK VSINTERSEC Tint RelationWith(const VSRectangle3 & Rectangle)const; int RelationWith(const VSRectangle3 &Rectangle,VSSegment3 &Segment)const; //判断平面和多边形的位置关系 //VSON VSFRONT VSBACK VSINTERSECTint RelationWith(const VSPolygon3 &Polygon)const; int RelationWith(const VSPolygon3 &Polygon,VSSegment3 & Segment)const; //判断平面和圆柱的位置关系 int RelationWith(const VSCylinder3 &Cylinder3)const;

 

VSTriangle3 denotes a triangle-like, derived from a triangle-like plane type, its structure is very simple, that is, 3 points.

P = P1U + P2V + P3 (1 UV) is triangular parametric equations.

Given a point P, the parameter U, V can be derived by the equation; given parameters U, V, P can be derived.

 
bool GetParameter(const VSVector3 &Point,VSREAL fTriangleParameter[3])const; inline VSVector3 GetParameterPoint(VSREAL fTriangleParameter[3])const;

 

VSRectangle3 rectangle representing the class, the class rectangle is derived from a plane, which is defined by two points, and a vertical component.

 

Sphere, there is the bounding box, cube

 

VSSphere3 represents spheres, VSOBB3 expressed the bounding box, VSAABB3 expressed cube. Because there is no physical realization engine, the cylinder, ellipsoid and the like, and the capsule body never implemented separately. In the engine inside a sphere and a cube with the most, they are used in the scene in management. In addition to these, there are some merging algorithm, the ball and the ball merger, cubes and cube merger.

 

Different game engine internal structure vary, but knowledge of the game engine very much involved, very few people can fully grasp every detail of knowledge.

 

At the same time, the game engine belonging to practical engineering, there must be compelling enough demos and code support, coupled with the authorization of commercial engines, the author is limited personal time and other various factors, leading to market books or generalities game engine talk, or to develop a far cry from a real game.

 

If you want to understand the basic principles and the role of the game engine, to understand the basic processes of large companies to develop engines to get stock of knowledge of good game engine, this "game engine Principles and Practice Volume 1 basic framework" recommend it to everyone, this book a special package of engine and example, you can learn to develop specific details of the game engine.

 

This book is Tencent game engine designer Cheng Dongzhe based on years of experience and accumulated masterpiece, detailed examples, the interpretation of the game engine production and development technology, Milo and other game industry-funded - deep expert full recommendation.

 

Concern "Asynchronous book" micro signal, replies: "51807", download the book supporting resources.

 

-END-

Published 545 original articles · won praise 298 · views 900 000 +

Guess you like

Origin blog.csdn.net/epubit17/article/details/104779524