# Line Collision -- vertical = bad

## Recommended Posts

pascalosti    100
Can anyone see whats wrong with this code? It works fine aslong as the wall isnt vertical.
// purpose of code
// take 2 lines and see if they collide, collide collision is saved
// inside of hitPT

bool Intersection(Line &Object, Line &wall, vector_2d *hitPT){

//// check if wall or Object is a dot you cant solve
if(Object.FirstPoint.X() == Object.SecondPoint.Y() && Object.SecondPoint.X() ==  Object.FirstPoint.X()){
return false;}
if(wall.FirstPoint.X() == wall.SecondPoint.Y() && wall.SecondPoint.X() ==  wall.FirstPoint.X()){
return false;}

// if lines parallel
if(Object.m == wall.m){
return false;
}

// -	If 1 line vertical, xHit = x from
//vertical line, use m & b from non-vertical line to find yHit.

if(Object.isVertical)
{
hitPT->X(Object.SecondPoint.X());
hitPT->Y(( wall.m * hitPT->X()) + wall.b) ;
}

else if(wall.isVertical){
hitPT->X(wall.FirstPoint.X());
hitPT->Y((Object.m * hitPT->X()) + Object.b);
}

//-	If 1 line horizontal, yHit = y from horizontal line, solve for xHit(already solved)
else if(Object.FirstPoint.Y() == Object.SecondPoint.Y())
{
hitPT->Y(Object.FirstPoint.Y());
hitPT->X((Object.b - wall.b)/(wall.m - Object.m));
}

else if(wall.FirstPoint.Y()  == wall.SecondPoint.Y())
{
hitPT->Y(wall.SecondPoint.Y());
hitPT->X((Object.b - wall.b)/(wall.m - Object.m));
}

else {
hitPT->X((Object.b - wall.b)/(wall.m - Object.m));
hitPT->Y((Object.m * hitPT->X()) + Object.b);
}

return true;
}

[\source]

##### Share on other sites
jyk    2094
Short answer: don't use the explicit (slope-intercept) form for lines in this context; use the parametric or implicit form instead.

##### Share on other sites
Alrecenk    400
Slope is undefined for vertical lines. You will probably have better luck with a parametricly defined line intersection test. This is a common problem so a quick search should return plenty of results.

edit : Curse my slow posting!