Recently engaged in a game to meet the shortest path of the general game problem, happened to see the old colleague wrote the shortest path of the 3D computer room patrol line article, a qixing based on the HT for Web write a * algorithm of WEBGL 3D rendering, algorithm based on open source https://github.com/bgrins/ Javascript-astar JavaScript implementation, in fact, the author also has a good 2D example to achieve http://www.briangrinstead.com/files/astar/, but feel that all a * The visual implementation of the algorithm is not cool enough, there are a lot of parameters need to adjust the control, or it is worth a good full demo, first on the 2D and 3D examples of the contrast chart.
Implementing code is easier than more than 100 lines, But the algorithm core in Astar.js, the interface core in the ht.js inside, I just need to build grid information, just listen to the user click, and then call astar.js for the shortest path calculation, the result through the animation of the way to show the process of walking, all the code is as follows:
functioninit () {W= 40; m = 20; d = w * M/2; Gridrows= []; DM=Newht. Datamodel (); G3d=NewHt.graph3d.Graph3dView (DM); G3d.setgridvisible (true); G3d.setgridcolor (' #BBBBBB '); G3d.setgridsize (m); G3d.setgridgap (w); G3d.addtodom (); G3d.sm (). Setselectionmode (' None '); Anim= Startball = Endball =NULL; G3d.getview (). AddEventListener (HT. Default.istouchable? ' Touchstart ': ' MouseDown ',function(e) {if(!anim) { varp =g3d.gethitposition (e); varx = Math.floor ((p[0] + D)/W); vary = Math.floor ((p[2] + D)/W); varEndball = Dm.getdatabytag ("Cell_" + x + "_" +y); if(Endball && endball.s (' batch ')!== ' wall '){ if(STARTBALL.A (' x ') = = = x && startball.a (' y ') = = =y) { return; } varg =NewGraph (gridrows, {diagonal:formpane.v (' Diagonal ') }); varStart = G.GRID[STARTBALL.A (' x ')][startball.a (' Y '))]; varEnd =G.grid[x][y]; varresult =Astar.search (g, start, end, {closest:formpane.v (' Closest ') }); if(!result.length) { return; } x= Result[result.length-1].x; Y= Result[result.length-1].y; Endball= Dm.getdatabytag ("Cell_" + x + "_" +y); ENDBALL.S (' 3d.visible ',true); STARTBALL.S (' 3d.visible ',false); Formpane.setdisabled (true); Anim=ht. Default.startanim ({duration:700, Finishfunc:function(){ for(vari=0; i<result.length; i++){ varBall = Dm.getdatabytag ("Cell_" + result[i].x + "_" +result[i].y); Ball.s ({' 3d.visible ':false, ' Shape3d.opacity ': 1, ' Shape3d.transparent ':false }); STARTBALL.P3 (-D+W*X+W/2, W/2,-D+W*Y+W/2);STARTBALL.A ({x:x, y:y}); STARTBALL.S (' 3d.visible ',true); } anim=NULL; Formpane.setdisabled (false); }, Action:function(v) {varindex = Math.Round (v*result.length); for(vari=0; i<index; i++){ varBall = Dm.getdatabytag ("Cell_" + result[i].x + "_" +result[i].y); Ball.s ({' 3d.visible ':true, ' shape3d.opacity ': i/index*0.3 + 0.7, ' shape3d.transparent ':true }); } } }); } } }, false); Createformpane (); Creategrid (); } functionCreategrid () {dm.clear (); varBall ; Gridrows.length= 0; for(varx = 0; x < m; X + +) { varNoderow = []; Gridrows.push (Noderow); for(vary = 0; Y < m; y++) { varIswall = Math.floor (Math.random () * (1/FORMPANE.V (' frequency '));if(Iswall = = 0) {Noderow.push (0); CreateNode (x, y). S ({' Batch ': ' Wall ', ' All.color ': ' #9CA69D ' }); }Else{Noderow.push (1); Ball=CreateNode (x, y). S ({' Shape3d ': ' Sphere ', ' Shape3d.color ': ' #FF703F ', ' 3d.visible ':false }); } } } if(!Ball ) {Creategrid (); return; } Startball= CreateNode (BALL.A (' x '), BALL.A (' y '), ' start '). s ({' Shape3d ': ' Sphere ', ' Shape3d.color ': ' #FF703F ' }); Shape=Newht. Shape (); Shape.setpoints (Newht. List ([{x:-D, y:d}, {x:d, y:d}, {x:d, y:-d}, {x:-D, Y:-d}, {x:-d, y:d}])); Shape.setthickness (4); Shape.settall (w); Shape.setelevation (W/2); Shape.setclosepath (true); SHAPE.S ({' All.color ': ' Rgba (187, 187, 187, 0.8) ', ' All.transparent ':true, ' All.reverse.cull ':true }); Dm.add (Shape); }functionCreateNode (x, y, tag) {varnode =Newht. Node (); Tag= Tag | | "Cell_" + x + "_" +y; Node.settag (tag); Node.a ({x:x, y:y}); NODE.S3 (W*0.9, w*0.9, w*0.9); NODE.P3 (-D+W*X+W/2, W/2,-D+W*Y+W/2);node.s ({' All.reverse.cull ':true, ' Shape3d.reverse.cull ':true }); Dm.add (node); returnnode;} functionCreateformpane () {Formpane=NewHt.widget.FormPane (); Formpane.setwidth (230); Formpane.setheight (70); Formpane.getview (). ClassName= ' Formpane '; Document.body.appendChild (Formpane.getview ()); Formpane.addrow ([' Wall Frequency ', {ID:' Frequency ', slider: {min:0, Max:0.8, Value:0.1, onvaluechanged:function() {Creategrid (); } } }], [100, 0.1]); Formpane.addrow ([{ID:' Closest ', CheckBox: {label:' Try Closest '}}, {ID:' Diagonal ', CheckBox: {label:' Allow Diagonal ' } } ], [0.1, 0.1]);}
It is much more comfortable to test 3D applications on mobile devices only from iOS8 support for WebGL, and the above examples are very fluent in the rendering and algorithms of the iOS system, http://v.youku.com/v_show/id_ Xodmzotu1njcy.html, of course, this small example data volume is not small, the essence is actually still 2D shortest path algorithm, not the true meaning of 3D space shortest path, but still enough to solve a lot of practical application problems.
HT for web-based 3D rendering * Search algorithm