My 2d ray tracer has worked fine up until the point where I sorted the calculated rays by angle (radian to be specific). I assume it has to do with the way tan is acting but I am not sure. What is the best way to sort the angles with a known x,y for the collision and for the originating point? I have been working on the same problem on and off for 2 weeks and have tried almost everything.
I can upload pictures now here:
Here is the guilty code if you want to fiddle with it:
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();
};
}
Here is google doc with the entire js code and html (html is after the js) https://docs.google.com/document/d/12chxLiaj9gz-irlM0VdZs-BNoNqoMbz5AS0Dm0CpXfI/edit?usp=sharing