HT for web-based 3D rendering * Search algorithm

Source: Internet
Author: User

Recently a game encountered the shortest path of the general game problem, a qixing based on the HT for Web wrote a * algorithm of WEBGL 3D rendering, based on open source Https://github.com/bgrins/javascript-astar JavaScript implementation, in fact, the author also has a good 2D example to implement http://www.briangrinstead.com/files/astar/, just think all A * algorithm visualization implementation is not cool, There are also a number of parameters need to adjust the control, or it is worth doing a 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:

Function init ()  {                 w = 40; m = 20; d = w * m / 2;             gridRows = [];                          dm = new ht. Datamodel ();              g3d = new  ht.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) {        &nbsP;        if (!anim) {var p = g3d.gethitposition (e); var  x = math.floor ((p[0] + d)/ w); Var y = math.floor (p[2] + &NBSP;D)/ w) Var endball = dm.getdatabytag ("Cell_"  + x +  "_"  +  y); if (Endball && endball.s (' batch ')  !==  ' wall ') {                       if (STARTBALL.A (' x ')  === x && startball.a (' y ')  === y) {return;}                          var g = new graph (gridrows, { diagonal: &NBSP;FORMPANE.V (' diagonal ')  }) VAR&NBSP;START&NBSP;=&NBSP;G.GRID[STARTBALL.A (' x ')][startball.a (' Y ')];var  end = g.grid[x][y];var&nbsP;result = 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 (var i=0; i< result.length; i++) {Var ball = 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,&NBSP;W/2,&NBSP;-D+W*Y+W/2); StARTBALL.A ({x: x, y: y}); Startball.s (' 3d.visible ',  true);} Anim = null;formpane.setdisabled (false);},action: function (v) {var index =  Math.Round (v*result.length); for (var i=0; i<index; i++) {var ball =  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});                                      }});                                                   }}&Nbsp;              }, false);                                       Createformpane (); Creategrid ();                                  }                 Function creategrid () {dm.clear ();             Var ball;gridrows.length = 0;for (var x = 0; x < m; x++)  {var noderow = [];gridrows.push (Noderow); for (var y = 0; y <  m; y++)  {                                 var iswall = 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 '), &NBSP;BALL.A (' y '),  ' start '). S ({' shape3d ':  ' sphere ',   ' shape3d.color ' ':  ' # ff703f '                     });   shape = new ht. Shape (); Shape.setpoints (new ht. List ([{x: -d, y: d},{x: d, y: d},{x: d, y: -d},{x: -d,  Y:&NBSP;-D},{X:&NBSP;-D,&NBSP;Y:&NBSP;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});d M.add (Shape);                              }function createnode (X, y, tag) {var node = new  ht. Node (); tag = tag | |   "Cell_"  + x +  "_"  + y;                node.settag (tag);       &Nbsp;     node.a ({ x: x,  y: y }); Node.s3 (w*0.9, w *0.9, w*0.9); node.p3 (-D+W*X+W/2,&NBSP;W/2,&NBSP;-D+W*Y+W/2); Node.s ({' All.reverse.cull ':  true, ' Shape3d.reverse.cull ':  true});d M.add (node); return node;}                         function createformpane ()  {            formpane = new ht.widget.formpane (); formpane.setwidth (230); Formpane.setheight (Formpane.getview) .classname =  ' Formpane ';d ocument.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]);}

Since IOS8 supports WEBGL, it is much more comfortable to test 3D applications on mobile devices than most of the current Android tablets, 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

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.