Page 1 of 1
Triangle/line Collision
Posted: September 19th, 2017, 2:36 am
by XxWalKaxX
Could use a hand figuring out to detect a line colliding with a triangle. The Triangle would be pointing down if that makes a difference. I wasn't sure on how to implement this because make simple hit box wont work.
Re: Triangle/line Collision
Posted: September 19th, 2017, 7:39 am
by chili
https://gamedev.stackexchange.com/quest ... ction-test
For future reference, the term you wanna stick into the google machine for times like this is 'intersection'.
https://www.google.com/search?q=line+in ... Zr7teZL2Zg
Re: Triangle/line Collision
Posted: September 19th, 2017, 11:10 am
by Yumtard
Re: Triangle/line Collision
Posted: September 19th, 2017, 12:16 pm
by ceofil
Re: Triangle/line Collision
Posted: September 20th, 2017, 2:13 am
by XxWalKaxX
Thanks for the replies. After digging around with some of the things you guys suggested I came across this and it seems to work perfectly.
Code: Select all
lineIntersect(p0,p1,p2,p3)
{
float a1 = p1.y - p0.y;
float b1 = p0.x - p1.x;
float c1 = a1 * p0.x + b1 * po.y;
float a2 = p3.y - p2.y;
float b2 = p2.x - p3.x;
float c2 = a2 * p2.x + b2 * p2.y;
float denominator = a1 * b2 - a2 * b1;
int x = (b2 * c1 - b1 * c2) / denominator;
int y = (a1 * c2 - a2 * c1) / denominator;
return x,y;
}