六边形格子地图坐标计算与转换
生活随笔
收集整理的這篇文章主要介紹了
六边形格子地图坐标计算与转换
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// 世界場景的一些數(shù)據(jù)管理,如提供坐標(biāo)轉(zhuǎn)換之類的接口var WorldMapManager = function () {this.mapSize = null; // 地圖大小,像素this.curViewPos = null; // 當(dāng)前大地圖視野坐標(biāo)// 初始化世界地圖的數(shù)據(jù)this.init = function (mapSize, tileSize) {this.mapSize = {width : globalConsts.WorldMapSize.width * globalConsts.TileSize.width + globalConsts.TileSize.width / 2,height : globalConsts.WorldMapSize.height * ((globalConsts.TileSize.height - globalConsts.TileSize.hex) / 2 + globalConsts.TileSize.hex) + (globalConsts.TileSize.height - globalConsts.TileSize.hex) / 2};this.tileSize = globalConsts.TileSize;};// 大地圖坐標(biāo)轉(zhuǎn)成蜂窩cellthis.mapPosToTile = function (pos) {// 算出縮放成正六邊形后邊長 a 的值var a = this.tileSize.width / Math.sqrt(3);var x = pos.x, y = (this.mapSize.height - pos.y) / this.tileSize.height * a * 2 + a / 2; // 加 a / 2 是因?yàn)榫匦尉W(wǎng)格計(jì)算時(shí)會在底部增加 a / 2//位于矩形網(wǎng)格邊線上的三個(gè)CELL中心點(diǎn)var points = new Array(cc.p(0, 0), cc.p(0, 0), cc.p(0, 0));//當(dāng)前距離的平方var dist;// index:被捕獲的索引var i, index;//二分之根號3 邊長的平方,如果距離比它還小,就必然捕獲var g_MinDistance2 = Math.pow(a * Math.sqrt(3) / 2, 2);// 網(wǎng)格寬、高var g_unitx = a * Math.sqrt(3); //sqrt(3) * avar g_unity = a * 1.5; //a * 3 / 2// 網(wǎng)格對角線平方向上取整var mindist= Math.ceil(Math.pow(g_unitx, 2) + Math.pow(g_unity, 2));//計(jì)算出鼠標(biāo)點(diǎn)位于哪一個(gè)矩形網(wǎng)格中var cx = parseInt(x/g_unitx);var cy = parseInt(y/g_unity);points[0].x = parseInt(g_unitx * cx);points[1].x = parseInt(g_unitx * (cx+0.5));points[2].x = parseInt(g_unitx * (cx+1));//根據(jù)cy是否是偶數(shù),決定三個(gè)點(diǎn)的縱坐標(biāo)if(cy % 2 == 0){//偶數(shù)時(shí),三個(gè)點(diǎn)組成倒立三角points[0].y = points[2].y = parseInt(g_unity * cy);points[1].y = parseInt(g_unity * (cy+1));} else{//奇數(shù)時(shí),三個(gè)點(diǎn)組成正立三角points[0].y = points[2].y = parseInt(g_unity * (cy+1));points[1].y = parseInt(g_unity * cy);}// 計(jì)算兩點(diǎn)間距離的平方function distance2(x1, y1, x2, y2){return ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));}//現(xiàn)在找出鼠標(biāo)距離哪一個(gè)點(diǎn)最近for(i = 0; i < 3; ++i){//求出距離的平方dist = distance2(x, y, points[i].x, points[i].y);//如果已經(jīng)肯定被捕獲if(dist < g_MinDistance2){index = i;break;}//更新最小距離值和索引if(dist < mindist){mindist = dist;index = i;}}// x 第 0 個(gè)點(diǎn)的列值減 1 等于cell.x ( x 最左半格有 -1 值 )// cy 偶數(shù)時(shí)中間點(diǎn) + 1,奇數(shù)時(shí)兩邊點(diǎn) + 1,減 1 是因?yàn)槌跏紴榱擞?jì)算方便 y 補(bǔ)了 a / 2 ( y 最上半格 也會存在 -1 )return {x : cx - (index > 0? 0 : 1), y : cy + (cy % 2 + index % 2) % 2 - 1};};// 格子坐標(biāo)轉(zhuǎn)成地圖坐標(biāo)this.tilePosToMap = function (pos) {var tileCenter, xPixel, yPixel;tileCenter = (pos.x * this.tileSize.width) + this.tileSize.width / 2;xPixel = tileCenter + (pos.y % 2) * this.tileSize.width / 2;yPixel = this.tileSize.height / 2 + pos.y * (this.tileSize.height / 2 + this.tileSize.hex / 2);// 因?yàn)殄^點(diǎn)的關(guān)系,y值需要倒過來,這很重要yPixel = this.mapSize.height - yPixel;return cc.p(xPixel, yPixel);};// 獲取相鄰兩個(gè)六邊形格子中心點(diǎn)距離this.getAdjacentHexagonCenterPointDistance = function (isLeftAndRight) {if (isLeftAndRight) {return this.tileSize.width;} else {return Math.sqrt(Math.pow((this.tileSize.height + this.tileSize.hex) / 2, 2) + Math.pow(this.tileSize.width / 2, 2));}};
};// 單例WorldMapManager.sharedInstance = null;WorldMapManager.getInstance = function(){if (WorldMapManager.sharedInstance == null) {WorldMapManager.sharedInstance = new WorldMapManager();WorldMapManager.sharedInstance.init();}return WorldMapManager.sharedInstance;
};
總結(jié)
以上是生活随笔為你收集整理的六边形格子地图坐标计算与转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员神器!
- 下一篇: 3D打印:FDM打印使用CURA4.13