我正在尝试通过鼠标位置在等距世界中找到合适的瓷砖。
我已经阅读了一些有关该主题的内容,但它似乎不适用于我。
基本思想是将正常鼠标坐标重新计算为等轴测图的平铺坐标。
如您所见,鼠标光标位于图块5/4处,并且重新计算错误(选择的图块为4/5)。这是我的代码:
var params.tx = 100, params.ty=54,
PI = 3.14152,
x1 = x_mouse - params.tx*5,
y1 = y_mouse * -2,
xr = Math.cos(PI/4)*x1 - Math.sin(PI/4)*y1,
yr = Math.sin(PI/4)*x1 + Math.cos(PI/4)*y1,
diag = (params.ty) * Math.sqrt(2),
x2 = parseInt(xr / diag)+1,
y2 = parseInt(yr * -1 / diag)+1;
磁贴的原始高度为54px,但是正如您所看到的,只有边框磁贴显示其完整高度。其余的图块被削减4个像素。
请帮助我-可能是我的整个公式是错误的。
最佳答案
我大约5年前写的等距库中的代码:
screenPositionToIsoXY : function(o, w, h){
var sX = ((((o.x - this.canvas.xPosition) - this.screenOffsetX) / this.unitWidth ) * 2) >> 0,
sY = ((((o.y - this.canvas.yPosition) - this.screenOffsetY) / this.unitHeight) * 2) >> 0,
isoX = ((sX + sY - this.cols) / 2) >> 0,
isoY = (((-1 + this.cols) - (sX - sY)) / 2) >> 0;
return $.extend(o, {
isoX : Math.constrain(isoX, 0, this.cols - (w||0)),
isoY : Math.constrain(isoY, 0, this.rows - (h||0))
});
},
应该相当不言自明。
关于javascript - 鼠标坐标到等距坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23412712/