3D visualization of A-star algorithm for WEBGL rendering based on HTML5

Source: Internet
Author: User

Http://www.hightopo.com/demo/astar/astar.html

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:

functionInit () {w = n; m = +; 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) {var p =G3d.gethitposition (e); var x = Math.floor ((p[0] + D)/W); var y = Math.floor ((p[2] + 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 = newGraph (Gridrows, {diagonal:formpane.v (' diagonal ')) }); var start = G.GRID[STARTBALL.A (' x ')][startball.a (' Y '))]; var end =G.grid[x][y]; var 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, 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) {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}); } } }); }}}, False); Createformpane (); Creategrid (); } functionCreategrid () {dm.clear (); varBall Gridrows.length = 0; for (var x = 0; x < m;) {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 '), 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) {var node = 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); return  node;} function  Createformpane () {formpane = new  Ht.widget.FormPane (); Formpane.setwidth (); Formpane.setheight (); 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 ();}}], [[+], 0.1 ]); Formpane.addrow ([{id: ' closest ' , checkbox: {label: ' Try closest ' }}, {id: ' diagonal ' , checkbox: {L Abel: ' 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.

Http://www.hightopo.com/demo/astar/astar.html

3D visualization of A-star algorithm for WEBGL rendering based on HTML5

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.