Advertisement Jump to content
Sign in to follow this  

Various 2D Collisions

This topic is 4927 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

Hello. I am currently working on a 2D game engine in C++ and am doing a variety of collision functions. Right now I am making point, line segment, rectangle, and circle classes for collisions. However, I need help with certain collision functions. They are: Line to Line Line to Circle Line to Rect A already have a line to point function, which I will post below. However, there is probably a better and faster way to do it. All of these classes use int values for collision, not floats. If anyone knows how to do these collisions, or where I can find the algorithms, that would be fantastic. int XColPointToLine( CColPoint* pPoint, CColLine* pLine ) { //If the Point's X is less than l1.x of the line, return false if( pPoint->GetX() < pLine->GetX1() ) return XCOL_FALSE; //If the Point's X is greater than the l2.x of hte line, return false if( pPoint->GetX() > pLine->GetX2() ) return XCOL_FALSE; //If the Point's Y is less than the l1.y of the line, return false if( pPoint->GetY() < pLine->GetY1() ) return XCOL_FALSE; //If the Point's Y is greater than the l2.y of the line, return false if( pPoint->GetY() > pLine->GetY2() ) return XCOL_FALSE; //Find the Slope and Y intercept of the line float Slope = ((float)pLine->GetY2()-(float)pLine->GetY1())/((float)pLine->GetX2()-(float)pLine->GetX1()); float YInt = (float)pLine->GetY1()-(Slope*(float)pLine->GetX1()); //Plug in the point's X into the Equation float AnsY = Slope*(float)pPoint->GetX()+YInt; //Convert the Y answer into an integer int AnsYInt = (int)AnsY; //If the Y found is not equal to the Y in the point, return false //Give the Y value a +-1 because of float conversion if( pPoint->GetY() > AnsYInt+1 ) return XCOL_FALSE; if( pPoint->GetY() < AnsYInt-1 ) return XCOL_FALSE; //Else return True return XCOL_TRUE; } Thanks. P.S. How dow you put the code snippets in code boxes?

Share this post

Link to post
Share on other sites

I wouldnt worry about line-point collision if you are using floating point numbers. It is a case that will only happen 0.000000001% of the time.

Line to line i suggest you google theirs 100 explanations.

Line to circle, you need to relize that the circle will always intersect the point perpendicular from the circles center to the line point.
you just need to find the projection of the ceneter of the circle onto the line.
Then check for the length of the projection to see if its less then the radius

Given line A-B and circle with a center C and radius R

The projection of C onto A-B is

let t = ((C-A)dot(B-A) ) / LenSq(B-A)

proj(C, A, B) = A + t * (B-A)

the circle intersects the line if

LenSq( proj(C, A, B) ) < R * R )

given a segment just modify the equation so that

if ( t<=0 ) t = 0; && if ( t>=1 ) t = 1;

and for a ray

if ( t<= 0 ) t = 0;

Line to Rect can be solved with 4 line to lines and a containment test of the segs bounding box, you probably can optimize this is you mean Axis alligned rects

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!