博客
关于我
基于HTML5的WebGL呈现A星算法的3D可视化
阅读量:472 次
发布时间:2019-03-06

本文共 1717 字,大约阅读时间需要 5 分钟。

 

最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于写了个A*算法的WebGL 3D呈现,算法基于开源  的javascript实现,其实作者也有个不错的2D例子实现  ,只不过觉得所有A*算法的可视化实现都是平面的不够酷,另外还有不少参数需要调节控制,还是值得好好搞个全面的Demo,先上张2D和3D例子的对照图。

实现代码比较容易一百多行,不过算法核心在astar.js了,界面核心在里面了,我只需要构建网格信息,只需监听用户点击,然后调用astar.js进行最短路径计算,将结果通过动画的方式呈现出走动的过程,所有代码如下:

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){ 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 = new Graph(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

只从iOS8支持WebGL后在移动终端上测试3D应用比当前的大部分Android平板舒服多了,以上的例子在iOS系统下呈现和算法都挺流畅,,当然这个小例子数据量也不大,本质其实还是2D的最短路径算法,并非真正意义的3D空间最短路径,但还是足够解决很多实际应用问题了。

 

转载地址:http://lwjbz.baihongyu.com/

你可能感兴趣的文章
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>