MMORPG programming in Silverlight tutorial (8) A * Algorithm

Source: Internet
Author: User

From this chapter, I will introduce map engine, it refer to 2 aspects, as follows:

1) Implementation the map. Including map's splitting, composing and rendering style.

2) implementation the objects of the map. Including obstruction, mask and transportation.

Now let's begin with path finding.

At present, the most classic path finding algorithm is a *, there are using algorithms originate from it, for example, the Manhattan method. in this chapter, I don't plan to introduce a * algorithm's implementation. if you are interested, you can visit this article to study more:

Http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c12527/

This guy implemented this algorithm in C #, so my friend Xuan Qin, referred to his implementation and wrote higher performance algorithm base on it. He named it should. You can download it here: Submit

The picture below shows the high performance of this algorithm implementation. the brown grid stands for obstruction, the start point is the Left top corner, the end point is the red grid in the center of the canvas. the blue grid stands for the shortest path. the algorithms cost 0.0030 s to find the path, it is perfect.

 

A * algorithm is difficult to understand, but it doesn' t matter whether you master it completely, we only care about how to use it in our game engine.

1st, add reference to this DLL, and declare its namespace in the code behind:

UsingQxgame_silverlight3.astar;
 
 

2nd, init global variables:

 
// A matrix used for Finding PathPrivate byte[,] Matrix =New byte[1024,102 4];// The size of the unit gridPrivate intGridsize = 20;// The start and end point of the moving AnimationPrivatePointStart, end;

 

The variable gridsize is very important. as we know, the game map is so large that we can't define each grid is an obstruction or not, so we must scale down the coordinate, the gridsize is the ratio.

For example, the window size is 800*600, supposed a point (80,100) in the window, according to the ratio: gridsize = 20, the point is converted to (80/20, 100/20) = (4, 5 ). it is convenient for us to implement every effect in different scenarios. I will explain this advantage in the following chapters.

Attention, the matrix's uppound must be the POW of 2; otherwise, A * algorithm will throw an exception. in our demo, It is 1024. it is enough. it is adjusted dynamically, I will talk about it in the following chapters.

 

3rd, init matrix, as follows, we set its elements, default value is 1, and 0 stands for obstruction:

 Void Initmatrix (){ For ( Int Y = 0; y <matrix. getupperbound (1); y ++ ){ For ( Int X = 0; x <matrix. getupperbound (0); X ++ ){ // Default to 1, to stand for not obstraction Matrix [x, y] = 1 ;}} // Init obstruction  For ( Int I = 0; I <18; I ++ ){ // Set to 0, to stand for obstraction Matrix [I, 12] = 0; rect = New  Rectangle () {Fill =New  Solidcolorbrush ( Colors . Red), width = gridsize, Height = gridsize}; carrier. Children. Add (rect ); Canvas . Setleft (rect, I * gridsize ); Canvas . Settop (rect, 12 * gridsize );} // Init obstruction  For ( Int I = 12; I <17; I ++) {matrix [17, I] = 0; rect = New  Rectangle () {Fill =New  Solidcolorbrush ( Colors . Red), width = gridsize, Height = gridsize}; carrier. Children. Add (rect ); Canvas . Setleft (rect, 17 * gridsize ); Canvas . Settop (rect, I * gridsize );} // Init obstruction  For ( Int I = 3; I <18; I ++) {matrix [I, 16] = 0; rect = New  Rectangle () {Fill =New  Solidcolorbrush ( Colors . Red), width = gridsize, Height = gridsize}; carrier. Children. Add (rect ); Canvas . Setleft (rect, I * gridsize ); Canvas . Settop (rect, 16 * gridsize );}}

 
 

In the code snippet above, I init 3 obstruction regions, and add 18 + 5 + 15 = 38 rectangles in the canvas. they are all obstructions, filled in red. the effect is as follows:

The value of gridsize is not fixed. if the value is small, there will be so large small rectangle generated in the canvas; if the value is large, maybe it cannot describe the obstruction accurately. so you must adjust it in different scenarios.

 

4th, find path between start and end point.

The start point is fixed, I init its value as (1, 1 ).

In the method carrier_mouseleftbuttondown, we reset end point in the first 4 sentences.

 Private void Carrier_mouseleftbuttondown ( Object Sender, Mousebuttoneventargs E ){ // Set end point's coordinate Point P = E. getposition (carrier ); Int X = ( Int ) P. x/gridsize; Int Y = ( Int ) P. Y/gridsize; end = New  Point (X, y ); Ipathfinder Pathfinder = New  Pathfinderfast (Matrix); Pathfinder. formula = Heuristicformula . Manhattan; Pathfinder. searchlimit = 2000;// The step number is less than 2000 // Pathfinder. diagonals = false;  VaR Path = Pathfinder. findpath (START, end ); If (Path = Null ){ MessageBox . Show ( "Path is not existing" ); Return ;} For ( Int I = path. Count-1; I> = 0; I --) {rect = New  Rectangle () {Fill =New  Solidcolorbrush ( Colors . Green), width = gridsize, Height = gridsize}; carrier. Children. Add (rect ); Canvas . Setleft (rect, path [I]. x * gridsize ); Canvas . Settop (rect, path [I]. y * gridsize );}}

 

Next, we use path finder algorithm, take matrix as the first parameter, a * algorithm will return an path array if there is a path between start and end point. I mark each element of the path array in green and locate all of them to their original point (path [I]. x * gridsize, path [I]. y * gridsize ).

Now, press Ctrl + F5, and click on the canvas, we can see the effect as follows, A * algorithm find a green path for us, although it is not well optimized.

 

There is still a problem left for us. As you can see in the picture as follow, why not go straight from A to B? It is shorter than the path: A-> C-> B. I will resolve it in the following chapters.

 

Summary: This chapter introduce a * algorithm and how to use it in our games.

Next chapter, I will implement how to add keyframe dynamically, so you can change the animation even the object is still moving. Please focus on it.

Chinese friend, You can also visit this Chinese blog if you feel difficult to read English, http://www.cnblogs.com/alamiye010/archive/2009/06/17/1505346.html, part of my article is base on it.

Demo download: http://silverlightrpg.codeplex.com/releases/view/40978

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.