基于WebGL 的3D呈现A* Search Algorithm

简介:  http://www.hightopo.com/demo/astar/astar.html最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现,算法基于开源 https://github.

 

IMG_1486

 

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

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

Screen Shot 2014-11-24 at 5.36.33 PM

实现代码比较容易一百多行,不过算法核心在astar.js了,界面核心在ht.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<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(); } 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'), 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: -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); } function createNode(x, y, tag){ var node = new ht.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(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]); }

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

Screen Shot 2014-11-24 at 5.09.13 PM

 

目录
相关文章
|
5天前
|
存储 人工智能 安全
AI 越智能,数据越危险?
阿里云提供AI全栈安全能力,为客户构建全链路数据保护体系,让企业敢用、能用、放心用
|
7天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
6天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
444 93
|
1天前
|
开发者
「玩透ESA」ESA启用和加速-ER在加速场景中的应用
本文介绍三种配置方法:通过“A鉴权”模板创建函数并设置触发器路由;在ESA上配置回源302跟随;以及自定义响应头。每步均配有详细截图指引,帮助开发者快速完成相关功能设置,提升服务安全性与灵活性。
283 2
|
7天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
406 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
7天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
308 158