日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

六边形格子地图坐标计算与转换

發(fā)布時(shí)間:2024/1/1 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 六边形格子地图坐标计算与转换 小編覺得挺不錯(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í)會(huì)在底部增加 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;//二分之根號(hào)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)格對(duì)角線平方向上取整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 最上半格 也會(huì)存在 -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; };


var tileCalculate = {// 兩六邊形 tilePos 獲取 無視阻擋的最短距離格子數(shù) 0~1 return 1getTwoTileDistance : function(lightTilePos, findTilePos) {var offX = 1, offY = 1; // 初始 findTilePos 在 lightTilePos 左上,往右下尋var tilePos, count;if (lightTilePos.y === findTilePos.y) {return Math.abs(lightTilePos.x - findTilePos.x);} else if (lightTilePos.x === findTilePos.x) {return Math.abs(lightTilePos.y - findTilePos.y);} else {count = 0;tilePos = {x:findTilePos.x, y:findTilePos.y};// 在右,往左尋if (lightTilePos.x < tilePos.x) {offX = -1;}// 在下,往上尋if (lightTilePos.y < tilePos.y) {offY = -1;}do {++count;tilePos.x += tilePos.y % 2 > 0 ? (offX > 0 ? offX : 0) : (offX < 0 ? offX : 0);tilePos.y += offY;} while (lightTilePos.x !== tilePos.x && lightTilePos.y !== tilePos.y);count += Math.abs(lightTilePos.x - tilePos.x) + Math.abs(lightTilePos.y - tilePos.y);return count;}},// 獲取格子周圍一圈,共六個(gè)格子的格子坐標(biāo)getAroundTilePos : function(tilePos) {if (tilePos.y % 2 > 0) {return [{x : tilePos.x, y : tilePos.y - 1}, {x : tilePos.x, y : tilePos.y + 1}, // 上下{x : tilePos.x - 1, y : tilePos.y}, {x : tilePos.x + 1, y : tilePos.y}, // 左右{x : tilePos.x + 1, y : tilePos.y - 1}, {x : tilePos.x + 1, y : tilePos.y + 1}]; // 右上,右下} else {return [{x : tilePos.x, y : tilePos.y - 1}, {x : tilePos.x, y : tilePos.y + 1}, // 上下{x : tilePos.x - 1, y : tilePos.y}, {x : tilePos.x + 1, y : tilePos.y}, // 左右{x : tilePos.x - 1, y : tilePos.y - 1}, {x : tilePos.x - 1, y : tilePos.y + 1}]; // 左上,左下}},// 獲取格子周圍兩圈,共十八個(gè)格子的格子坐標(biāo)getAroundTwoLapsTilePos : function(tilePos) {var i, j, tiles1, tiles2 = [];var tilePoses = {};// 把第一圈及第一圈每一個(gè)的第一圈都收集起來tiles1 = this.getAroundTilePos(tilePos);for (i in tiles1) {tiles2 = this.getAroundTilePos(tiles1[i]);for (j in tiles2) {if (tiles2[j].x !== tilePos.x || tiles2[j].y !== tilePos.y) {tilePoses[JSON.stringify(tiles2[j])] = tiles2[j];}}}return tilePoses;},};

總結(jié)

以上是生活随笔為你收集整理的六边形格子地图坐标计算与转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。