我的2d射线追踪器一直工作得很好,直到我按角度对计算出的射线进行分类(弧度是具体的)。我想这与黑皮肤的行为方式有关,但我不确定。对于碰撞和起始点,用已知的x,y对角度进行排序的最佳方法是什么?两个星期来,我一直在不停地解决同样的问题,并且几乎什么都试过了。
我现在可以在这里上传图片:


如果你想摆弄它的话,这是罪过的代码:
function sortByAngle(pos){
for (var i = viewFeild.length - 1; i >= 0; i --) {
viewFeild[i][3] = Math.atan((viewFeild[i][4]-pos.y)/(viewFeild[i][0]-pos.x));
if(viewFeild[i][5]<pos.y)
viewFeild[i][6] = viewFeild[i][7]*-1-4;
if (viewFeild[i][8]<0) {viewFeild[i][9]+=2};
};
viewFeild.sort(function(a,b){return a[2]-b[2]});
}
function fillView(pos) {
for (var i = viewFeild.length - 1; i >= 0; i--) {
//console.log(i+" "+viewFeild[i][10] + " " + viewFeild[(i+1)%viewFeild.length][11])
//console.log(viewFeild.length)
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(viewFeild[i][0]+pos.x, viewFeild[i][12]+pos.y);
ctx.lineTo(viewFeild[(i+1)%viewFeild.length][0]+pos.x, viewFeild[(i+1)%viewFeild.length][13]+pos.y);
ctx.closePath();
ctx.fillStyle = "rgba(100, " + 35*i + ", 100, .6)";
ctx.fill();
};
}下面是google和完整的js代码以及html (html在js之后) https://docs.google.com/document/d/12chxLiaj9gz-irlM0VdZs-BNoNqoMbz5AS0Dm0CpXfI/edit?usp=sharing
发布于 2014-04-15 08:51:49
首先要做的是澄清您的代码。
1)不需要按相反顺序填充数组。
2)使用atan2 -我不知道你处理弧线的方法.
3)缓存要重用的数组元素。
4)不要在每种类型上创建一个排序函数。
5)如果按正确的顺序排序,则不需要以相反的顺序显示。
一旦情况变得更清楚,我发现奇怪的是,你用字段3,5或6来表示你的观点。我想说,只要对y数据进行一次偏移就足够了;-)
function sortByAngle(center) {
for (var i = 0 ; i<viewFeild.length ; i++) {
var thisField = viewFeild[i] ;
thisField[2] = Math.atan2( thisField[3] - center.y)
, (thisField[0] - center.x));
};
viewFeild.sort(sortOnSecondItem);
}
function fillView(pos) {
for (var i = 0 ; i<viewFeild.length ; i++) {
var thisField = viewFeild[i] ;
var nextField = (i==viewFeild.length-1) ?
viewFeild[0]
: viewFeild[i+1] ;
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(thisField[0] + pos.x, thisField[5] + pos.y);
ctx.lineTo(nextField[0] + pos.x, nextField[6] + pos.y);
ctx.closePath();
ctx.fillStyle = "rgba(100, " + 35 * i + ", 100, .6)";
ctx.fill();
};
}
function sortOnSecondItem(a,b) { return a[2] - b[2] }https://stackoverflow.com/questions/23074315
复制相似问题