Triangle/line Collision

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
XxWalKaxX
Posts: 244
Joined: June 11th, 2012, 7:15 pm

Triangle/line Collision

Post by XxWalKaxX » September 19th, 2017, 2:36 am

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.
What you call a bug...I call a new feature!

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Triangle/line Collision

Post by chili » September 19th, 2017, 7:39 am

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
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Triangle/line Collision

Post by Yumtard » September 19th, 2017, 11:10 am

https://www.youtube.com/watch?v=4bIsntT ... 698tmcwLk9


maybe this is what you're looking for


User avatar
XxWalKaxX
Posts: 244
Joined: June 11th, 2012, 7:15 pm

Re: Triangle/line Collision

Post by XxWalKaxX » September 20th, 2017, 2:13 am

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;
}
What you call a bug...I call a new feature!

Post Reply