Server uses recast navigation

Source: Internet
Author: User
Tags fread

In 3D MMO or other types of games, usually need to do pathfinding, map for the road to find a variety of solutions, such as lattice, convex polygon, this article introduces a more commonly used navigation mesh way to carry out the road. However, the whole Navmesh algorithm is more complex, no in-depth research cannot be written out, we use online open source solutions, Google's recast solution. The development environment we use is Win7 + vs2013.

I. Installation and use of server-side recast

1. Download the source code of recast from GitHub first

2. After the source code is downloaded, we need to compile it ourselves, recast using the precompiled tool Premake, need to Premake.lua. Download Lua First, then download Premake5 separately, otherwise Premake5.lua will not be available. Placing the downloaded Premake.exe in the same directory as the Premake5.lua and then running Premake.exe vs2013 (depending on your IDE version number) in the console will generate the vs2013 solution sln file.

2. Recast test procedures, the use of the relevant SDL library, this is relatively simple, download a sdl-devel library can be, and then look at Recastdemo inside the configuration directory, copy in can be compiled through. Intermediate compile time, possibly because of the vs2013 version, will prompt Min,max these functions are not defined, as long as include the corresponding STL library header file <algorithm> can be

3. Successfully compiled recast related libraries, detour,recast and so on, we start with how to use recast may not have a concept, but recast has taken this into account, the project has a Recastdemo program, is used to sample how to use the recast.

4. Using Recastdemo, we can generate Navmesh based on the scene's. obj file, and import our own generated Navmesh, but this is all generated by the recast open Source tool. In the actual game, what we need is the Navmesh generated from the client and then imported into the server for parsing.

Second, Navmesh generation of Unity3d Client

Unity3d is a self-navmesh agent, you can generate Navmesh, and export Navmesh, but its export navmesh, I check down the need to write their own code, and Navmesh agent is modified export Navmesh file format , which means we can't use recast C + + code directly. Then I searched for the recast for unity plugin, which is a source code, but if you need $ $ for a normal purchase.

For this plug-in at that time studied a relatively long period, the first to export its Navmesh format, and then use the C++recast Import test, found that the format is definitely not the same. Then carefully study on both sides generated Navmesh when the format differences, for Tilemesh storage, the two sides are inconsistent, would like to directly modify C # code to achieve the same on both sides, but because the serialization of C # is not familiar enough, give up. But should be able to change directly to the same, this later time can be further studied.

Later, Kbengine modified Critterai was used, and the Navmesh file it produced was directly used in Kbengine. And then put our server in the resolution Navmesh format, change to and kbengine consistent on it. The code is still very simple and the code is as follows:

    BOOLNavmeshloader::load_cai (Const Char*path) {FILE* fp = fopen (Path,"RB"); if(!FP)return false; //Read header.Navmeshsetheader_cai Header; size_t Readlen= Fread (&header,sizeof(NAVMESHSETHEADER_CAI),1, FP); if(Readlen! =1) {fclose (FP); return false; }        if(Header.version! =navmeshset_version)            {fclose (FP); return false; } Dtnavmesh* Mesh =Dtallocnavmesh (); if(!mesh)            {fclose (FP); return false; } dtstatus Status= Mesh->init (&header.params); if(dtstatusfailed (status)) {fclose (FP); return false; }        //Read tiles.         for(inti =0; i < Header.tilecount; ++i) {Navmeshtileheader tileheader; Readlen= Fread (&tileheader,sizeof(Tileheader),1, FP); if(Readlen! =1) {fclose (FP); return false; }            if(!tileheader.tileref | |!tileheader.datasize) Break; unsignedChar* data = (unsignedChar*) Dtalloc (tileheader.datasize, dt_alloc_perm); if(!data) Break; memset (data,0, tileheader.datasize); Readlen= Fread (data, Tileheader.datasize,1, FP); if(Readlen! =1) {fclose (FP); return false; } Mesh->addtile (data, Tileheader.datasize, Dt_tile_free_data, Tileheader.tileref,0);        } fclose (FP); M_navmesh=mesh; M_navquery=Dtallocnavmeshquery (); Status= M_navquery->init (M_navmesh,2048); if(dtstatusfailed (status)) {return false; }        return true; }

Kbengine and Recastdemo in the resolution Navmesh is mainly Navmeshsetheader head structure is inconsistent, the others are the same, so it is relatively simple.

Can normally parse the Navmesh file, is the map to seek the road, where the main use of the Recastdemo in the path of code, the localization of the modification.

  

Third, the use of recast navigation problems encountered

1, which for a while, I for the client generated Navmesh, and server generated Navmesh in the coordinate system inconsistent, feel very confused, and even want to do not own the 3DMax and other tools to re-import the map model, and then modify the coordinate system, which now seems ridiculous, Mainly for the Navmesh generation principle is not very understanding. In fact, the Navmesh generated by the client is provided with its own coordinate system and relative coordinates. Before that confusion, because the server is Recastdemo generated in the Navmesh, the client generated their own, is basically two worlds, two kinds of coordinates, of course, inconsistent!!

2, in fact, the bottom of the Critterai is C + + write DLL, through unity Import into the project, and then generate Navmesh, as long as the unity using the DLL rules, we can actually use the recast source code, Then change to a DLL that unity can use to export Navmesh. You can try this later.

Server uses recast navigation

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.