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 two-point distance of a space is as follows:

Coordinate x of a player on a line segment:

Coordinate y of a player on a line segment:

The coordinate z of a player on a line segment:

Analysis of the current role position calculation principle:

A role walk is a path, which is represented by a point sequence. The role starts to walk and record the current time TimeStart. To obtain the calculation of the current position, first obtain the current time TimeNow, based on the time difference between TimeNow-TimeStart and the walking speed of the role, obtain the distance that the role has traveled, calculate the distance between each two points, and determine the path of the role's current time, and between the two roles. Then, use the vector calculation formula to calculate the coordinates of the current role.

Class encapsulation implementation code for role location calculation:

# Pragma onceclass CCoordinatePath {public: CCoordinatePath (void );~ Synchronized (void); public: // update the walking path void UpdatePath (COORDINATE_3D paths); void UpdatePath (vector <COORDINATE_3DPATH> vtPath, COORDINATE_3D paths, float nCompletePath = 0 ); // start to walk (Mobile Computing) void StartMove (DWORD dwStartTime); // obtain the current space location COORDINATE_3D GetCoordinate (DWORD dwNowTime ); // obtain the walking path const vector <COORDINATE_3DPATH> * GetPath (); // obtain the stop position COORDINATE_3D GetStopCoordinate (); // obtain the total movement distance (the total distance from the start to the present) float GetCompletePath (DWORD dwNowTime); // update the player speed void UpdateSpeed (unsigned short wSpeed, DWORD dwNowTime ); // get the current speed unsigned short GetSpeed (); // whether the player is moving bool IsMoving (DWORD dwNowTime); private: // walking path vector <COORDINATE_3DPATH> m_vtPath; // stop point COORDINATE_3D m_stopCoordinate3D; // completed path float m_nCompletePath; // The current gamer speed unsigned short m_wCurSpeed; // The path start time DWORD m_dwStartMoveTime ;};

# Include "stdafx. H" # include "coordinatepath. H" ccoordinatepath: ccoordinatepath (void) {m_wcurspeed = 4;} ccoordinatepath ::~ Signature (void) {} void identifier: updatepath (coordinate_3d stopcoordinate3d) {m_vtpath.clear (); identifier = stopcoordinate3d; m_ncompletepath = 0;} void ccoordinatepath :: updatepath (vector <coordinate_3dpath> vtpath, coordinate_3d paths, float ncompletepath/* = 0 */) {m_vtpath = vtpath; Signature = signature; m_ncompletepath = ncompletepath;} void ccoordinate Path: startmove (DWORD dwstarttime) {m_dwstartmovetime = dwstarttime;} coordinate_3d variable: getcoordinate (DWORD dwnowtime) {If (m_vtpath.size () = 0) {return interval ;} float ntotaldistance = getcompletepath (dwnowtime); If (ntotaldistance <0) {cout <"gamer movement distance calculation error" <Endl; return m_stopcoordinate3d ;} // Ceshi // cout <"distance:" <ntotaldistance <"time" <(dwNowTime-m_dwStartMoveTime) <Endl; coordinate_3d coordinate3d; // The total distance between players is calculated. The following code calculates the player position vector <coordinate_3dpath >:: iterator itpath = m_vtpath.begin (); For (; itpath! = M_vtpath.end (); ++ itpath) {If (itpath-> alldistance> ntotaldistance) {// the current position of the role is in the current path, calculate the current position float ncurdistance = ntotaldistance-(itpath-> alldistance-itpath-> curdistance); If (ncurdistance <0) {cout <"[severe error] Getting coordinates" <Endl; return m_stopcoordinate3d;} coordinate3d. X = itpath-> X + itpath-> dformula * itpath-> xdistance * ncurdistance; coordinate3d. y = itpath-> Y + itpath-> dformula * itpath-> ydistance * ncurdistance; coordinate3d. z = itpath-> Z + itpath-> dformula * itpath-> zdistance * ncurdistance; coordinate3d. dir = itpath-> dir; If (coordinate3d. X = 1 & coordinate3d. y = 1 & coordinate3d. z = 1) {int I = 0;} // Yang // cout <"current moving coordinate: X:" <coordinate3d. x <", Y:" <coordinate3d. Y <", Z:" <coordinate3d. z <Endl; // yangreturn coordinate3d; }}// clear the previous vertex path at the target point m_vtpath.clear (); Return vertex;} const vector <coordinate_3dpath> * ccoordinatepath:: getpath () {return & m_vtpath;} coordinate_3d ccoordinatepath: getstopcoordinate () {return response;} float ccoordinatepath: getcompletepath (DWORD dwnowtime) {// shift-free distance calculation DWORD dwmovetime = dwNowTime-m_dwStartMoveTime; Return (m_ncompletepath + m_wcurspeed * dwmovetime/1000.0f);} void speed: updatespeed (unsigned short wspeed, DWORD dwnowtime) {// The calculated path m_ncompletepath + = getcompletepath (dwnowtime); m_dwstartmovetime = dwnowtime; m_wcurspeed = wspeed; // The current speed} unsigned short ccoordinatepath: getspeed () {return m_wcurspeed;} bool ccoordinatepath: ismoving (DWORD dwnowtime) {getcoordinate (dwnowtime); If (m_vtpath.size ()> 0) {return true;} else {return false ;}}

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.