Jump to content
  • Advertisement
Sign in to follow this  
pascalosti

Line Collision -- vertical = bad

This topic is 3974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


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

More info here (and many other places online as well - just google '2d line intersection').

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!