Calculating if a point is within a triangle?

Started by
30 comments, last by DevLiquidKnight 21 years, 5 months ago
Your code looks very interesting, Mulligan. Naturally the triangles'' vertices are ordered to allow for fast backface culling. But it seems that with your method there is no way of telling exactly how far from the triangle the point is, if it''s outside it.

This is important for me since I''m doing collision detection. When doing sphere-to-triangle collision, I''d need to make sure the distance from the center of the sphere to the nearest point on the triangle is less than or equal to the radius.

~CGameProgrammer( );
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Advertisement
DevLiquidKnight: Taking the dot product of two vectors is a very useful way of finding the angle between them. That is what it's used for. This is the equation:

Vector1 dot Vector2 = Vector1.Length * Vector2.Length * cos(Angle)

Angle is the angle between the two vectors. If the vectors are normalized, which they should be, then their lengths are both one, and it can be simplified to:

Vector1 dot Vector2 = cos(Angle)

If you wanted to, you could take the arccosine of the dot product to find the angle, but for most applications this is not necessary or even helpful.

The cosine of 0 degrees is 1, the cosine of 90 is zero, the cosine of -90 is also zero. Therefore if the two product of two normalized (unit) vectors is zero, we know they are perpendicular. If the dot product is +/- one, they are parallel. And so on.

Also, to whomever asked, to insert an image, say &ltimg src="http://www.website.com/image.jpg"&gt. To insert a thumbnail, say &lta href="http://www.website.com/image.jpg">&ltimg src="http://www.website.com/thumbnail.jpg"></a>

~CGameProgrammer( );

EDIT: Forgot to state the dot-product equation. It is this:
2D: Vector1 dot Vector2 = (Vector1.X * Vector2.X) + (Vector1.Y * Vector2.Y);
3D: Vector1 dot Vector2 = (Vector1.X * Vector2.X) + (Vector1.Y * Vector2.Y) + (Vector1.Z * Vector2.Z);

[edited by - CGameProgrammer on October 21, 2002 5:03:36 PM]
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement