Sorry for double post, but you probably wouldn't notice this if it didn't magically go back to the top :V
Ok, first I'm sorry for leaving that piece of crap on IRC last night, please disregard it... it was completely wrong and messed up, but I thought of the solution to the Collision problem already... It's pretty long and my current computer time is extremely limited so I may not be able to type everything down, and I'm not sure when I'll get back on... I don't care if you solved it already :V I just wanna post this somewhere...
This is unprecedented non-serious affair. Guy is preparing. Please wait warmly.
EDIT: God that a a fucking lot... I might've missed something though :V
//I probably could've shortened this a lot and removed some parts, but it's easier to understand like this
function Collision_Line_Line(x1, y1, x2, y2, x3, y3, x4, y4) //Gets whether 2 widthless segments intersect, actually there's an easier way, but this is easier to understand
{
let lineangle=atan2(y4-y3, x4-x3);
let slope=(y2-y1)/(x2-x1);
if(x2-x1==0){slope=NULL;} //to prevent x/0
let slopeb=(y4-y3)/(x4-x3);
if(x4-x3==0){slopeb=NULL;} //to prevent x/0
if(slopeb==NULL){ //if lineb is vertical
if( (x1<=x3 && x2>=x3)||(x1>=x3 && x2<=x3) ){return true;}else{return false;} //if linea's 2 points lie on opposite sides of lineb, true, else, false
}else if(slopeb==0){ //if lineb is horizontal
if( (y1<=y3 && y2>=y3)||(y1>=y3 && y2<=y3) ){return true;}else{return false;} //if linea's 2 points lie on opposite sides of lineb, true, else, false
}else{
let x5=x1+absolute(x1-x3)*cos(lineangle); //fails at getting the x coordinate of the point where the y's are equal, but you get the idea
let y5=y1+absolute(y1-y3)*sin(lineangle); //fails at getting the y coordinate of the point where the x's are equal, but you get the idea
let x6=x2+absolute(x2-x3)*cos(lineangle); //fails at getting the x coordinate of the point where the y's are equal, but you get the idea
let y6=y2+absolute(y2-y3)*sin(lineangle); //fails at getting the y coordinate of the point where the x's are equal, but you get the idea
if(slopeb>0){ //if lineb y decreases as x increases
if(slope==NULL){
if( (x1<=x5 && x2>=x6)|| (x1>=x5 && x2<=x6) ){return true;}else{return false;} //opposite sides again
}else{
if( x1<x5 && y1<y5 ){ //if point1 is to the left-up of lineb
if( x2>x6 && y2>y6 ){return true;}else{return false;} //if point2 is to the right-down of line b, true, else, false
}else if( x1>x5 && y1>y5 ){ //opposite of previous statement (for angle+180 instances)
if( x2<x6 && y2<y6 ){return true;}else{return false;}
}else if( (x1==x5 && y1==y5) || (x2==x6 && y2==y6) ){return true;}else{return false;} //if point1/2 is collinear with lineb true, else, false
}
}
if(slopeb<0){ //opposite of the entire slopeb>0 if statement
if(slope==NULL){
if( (x1<=x5 && x2>=x6)|| (x1>=x5 && x2<=x6) ){return true;}else{return false;} //opposite sides again
}else{
if( x1>x5 && y1<y5 ){ //if point1 is to the left-up of lineb
if( x2<x6 && y2>y6 ){return true;}else{return false;} //if point2 is to the right-down of line b, true, else, false
}else if( x1<x5 && y1>y5 ){ //opposite of previous statement (for angle+180 instances)
if( x2>x6 && y2<y6 ){return true;}else{return false;}
}else if( (x1==x5 && y1==y5) || (x2==x6 && y2==y6) ){return true;}else{return false;} //if point1/2 is collinear with lineb true, else, false
}
}
}
} //actually... this is already the line to line function you're looking for :V I just want to continue
function Collision_Box_Line(centx, centy, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6) //Gets whether any segment from the center of the box to any corner intersects the line. (if at least one does then the box intersects the line obviously) Admittedly, this could be faster done by just checking the opposite corners, but this method is easier to understand
{ //you need to center cause I'm too lazy to calculate for it :V
let returnval=0; //it's easier than an if statement with 3 ||'s right?
if(Collision_Line_Line(centx, centy, x1, y1, x5, y5, x6, y6)==true){returnval++;}
if(Collision_Line_Line(centx, centy, x2, y2, x5, y5, x6, y6)==true){returnval++;}
if(Collision_Line_Line(centx, centy, x3, y3, x5, y5, x6, y6)==true){returnval++;}
if(Collision_Line_Line(centx, centy, x4, y4, x5, y5, x6, y6)==true){returnval++;}
if(returnval>=1){return true;}else{return false;}
} //notice that this statement can easily be used for a polygon with any number of sides by changing the number of parameters
function PointSideFromLine(x1, y1, x2, y2, x3, y3) //Gets whether a point is on the left or the right of a line
{
let lineangle=atan2(y3-y2, x3-x2);
let x4=x1+absolute(x1-x2)*cos(lineangle); //fails at getting the x coordinate of the point where the y's are equal, but you get the idea
let y4=y1+absolute(y1-y2)*sin(lineangle); //fails at getting the y coordinate of the point where the x's are equal, but you get the idea
if(x1<=x4){return "LEFT";}else{return "RIGHT";}
} //note that I could've just called this multiple times for the first statement, but it's easier to understand like that (at least, for me it is)
function Collision_Box_Laser(centx, centy, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, width) //god... 15 parameters
{
if(PointSideFromLine(centx, centy, x5, y5, x6, y6)=="LEFT"){
if(Collision_Box_Line(centx, centy, x1, y1, x2, y2, x3, y3, x4, y4, x5-width/2, y5-width/2, x6-width/2, y6-width/2)==true){return true;}else{return false;}
//uses the leftmost line of the laser
}else{
if(Collision_Box_Line(centx, centy, x1, y1, x2, y2, x3, y3, x4, y4, x5+width/2, y5+width/2, x6+width/2, y6+width/2)==true){return true;}else{return false;}
//uses the rightmost line of the laser
}
}
Collision_Box_Laser... the computation for the x's and y's may be a bit off, but you can use your super math to get those :V, but you get the idea I'm trying to show right?
and now I have to leave until god knows when...