3D server-side vector computing-based role Location Algorithm

Source: Internet
Author: User

3D server-side player walking processing is used by the server to calculate the current position of the Player Based on the client path, player walking time, and speed (including the variable speed. Because client walking is a path, the grid computing algorithm in 2D is not used, and the current position of the role cannot be efficiently obtained. The amount of walking paths, walking speed, and walking time used by players has been reached, calculate the current exact position of the player. Because the point in 3D games is the space point of xyz, the calculation of the link is a three-dimensional vector calculation. The formula for calculating the distance between two points in space is: coordinate x of the player on a line segment: coordinate y of the player on a line segment: coordinate z of the player on a line segment: Remarks: public file. rar code implementation: // spatial coordinate typedef struct tagCoordinate3D {tagCoordinate3D () {this-> x = 0; this-> y = 0; this-> z = 0; this-> dir = 0;} tagCoordinate3D (int x, int y, int z, short dir = 0) {www.2cto. comthis-> x = x; this-> y = y; this-> z = z; this-> dir = dir ;}~ TagCoordinate3D () {} int operator-(tagCoordinate3D coordinate3D) {double nTemp = (this-> x-coordinate3D. x) * (this-> x-coordinate3D. x) + (this-> y-coordinate3D. y) * (this-> y-coordinate3D. y) + (this-> z-coordinate3D. z) * (this-> z-coordinate3D. z); if (0 = nTemp) {return (int)-1;} return (int) sqrt (nTemp) ;}int x; // X coordinate int z; // zcoordinate int y; // Y coordinate (height) short dir; // direction (range: 0-359)} COORDINATE_3D, * PCOORDINATE_3D; Typedef struct Syntax: public tagCoordinate3D {tagCoordinate3DPath () {} tagCoordinate3DPath (int x, int y, int z, short dir = 0): tagCoordinate3D (x, y, z, dir) {} tagCoordinate3DPath (tagCoordinate3D coordinate3D): tagCoordinate3D (coordinate3D. x, coordinate3D. y, coordinate3D. z, coordinate3D. dir ){}~ TagCoordinate3DPath () {} int curDistance; // The current distance (the distance from the previous vertex) int allDistance; // The total distance (the distance from the first vertex and the sum of the distance from all vertices) int xDistance; int yDistance; int zDistance; double dFormula; // Location Calculation Formula} COORDINATE_3DPATH, * PCOORDINATE_3DPATH; get the current role coordinate: COORDINATE_3D CFightRole: GetPosition () {if (m_vtWalkPath.size () = 0) {return m_3d1_pathend;} COORDINATE_3D coordinate3D; DWORD dwNowTime = GetFrameTime (); int n1_time = dwNowTime-m_dwWa LkPathBeginTime; if (n0000time <0) {cout <"[severe error] the error should not occur. An error occurs." <endl; return m_3d1_pathend ;} double nTotalDistance = m_wSpeed * nWalkTime/(1000.0f); // The total distance of the player's walking distance is calculated, and the vector <COORDINATE_3DPATH >:: iterator itPath = m_vtWalkPath.begin (); for (; itPath! = M_vtWalkPath.end (); ++ itPath) {if (itPath-> allDistance> (int) nTotalDistance) {// calculate the current position of the role in the current path. bFind = true; double nCurDistance = nTotalDistance-(itPath-> allDistance-itPath-> curDistance); if (nCurDistance <0) {cout <"[Serious Error] Getting coordinates" <endl; return coordinate3D;} coordinate3D. x = (int) (itPath-> x + itPath-> dFormula * itPath-> xDistance * nCurDistance); coordinate3D. y = (int) (itPath-> y + itPath-> dF Ormula * itPath-> yDistance * nCurDistance); coordinate3D. z = (int) (itPath-> z + itPath-> dFormula * itPath-> zDistance * nCurDistance); coordinate3D. dir = itPath-> dir; if (coordinate3D. x = 1 & coordinate3D. y = 1 & coordinate3D. z = 1) {int I = 0;} return coordinate3D;} // clear the previous vertex path at the target point m_vtWalkPath.clear (); return m_3d1_pathend ;} some calculation formula initialization: COORDINATE_3D curCoordinate3D (tempCoordinate3d. x (), tempCoordinate 3d. y (), tempCoordinate3d. z (), tempCoordinate3d. dir (); // The path COORDINATE_3DPATH coordinate3DPath (prevCoordinate3D) in route n. // The distance from the current path to coordinate3DPath. curDistance = curCoordinate3D-prevCoordinate3D; // The total Path Distance from nAllPathDistance + = coordinate3DPath. curDistance; coordinate3DPath. allDistance = nAllPathDistance; // Location Calculation Formula double nValue = (curCoordinate3D. x-prevCoordinate3D.x) * (curCoordinate3D. x-prevCoordinate3D.x) + (CurCoordinate3D. y-prevCoordinate3D.y) * (curCoordinate3D. y-prevCoordinate3D.y) + (curCoordinate3D. z-prevCoordinate3D.z) * (curCoordinate3D. z-prevCoordinate3D.z); if (nValue <= 0) {cout <"root number is 0" <endl; return;} double dValue = sqrt (nValue ); if (0 = dValue) {cout <"error" <endl; return;} coordinate3DPath. xDistance = curCoordinate3D. x-prevCoordinate3D.x; coordinate3DPath. yDistance = curCoordinate3D. Y-prevCoordinate3D.y; coordinate3DPath. zDistance = curCoordinate3D. z-prevCoordinate3D.z; coordinate3DPath. dFormula = 1/dValue; // the point in the path segment is intermittently obtained to determine the walking point of the grid. VtWalkPath. push_back (coordinate3DPath)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.