#### Archived

This topic is now archived and is closed to further replies.

# Triangle Point Collision

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

## Recommended Posts

I wanted to check if a tile is contained within a rect. So I split the rect into 2 triangles and checked the tiles edge points against both triangles. But this test seems to return true no matter what. Before showing the code I do admit that I do not really understand what is going on in this test. All I know is that this test is meant to check if a point is contained within a triangle by checking if it is on the same side of 2 sides as the 3 edge point is.
bool TrianglePointCollision(LPPOINT3D pPoint, LPTRIANGLE3D pTriangle) {
D3DVECTOR V1, V2;

V1 = CrossProduct(VectorSubtract(pTriangle->UpperCenter, pTriangle->LowerRight), VectorSubtract(*pPoint, pTriangle->LowerRight));
V2 = CrossProduct(VectorSubtract(pTriangle->UpperCenter, pTriangle->LowerRight), VectorSubtract(pTriangle->LowerLeft, pTriangle->LowerRight));

if(DotProduct(V1, V2) < 0.0f) {
return false;
}

V1 = CrossProduct(VectorSubtract(pTriangle->UpperCenter, pTriangle->LowerLeft), VectorSubtract(*pPoint, pTriangle->LowerLeft));
V2 = CrossProduct(VectorSubtract(pTriangle->UpperCenter, pTriangle->LowerLeft), VectorSubtract(pTriangle->LowerRight, pTriangle->LowerLeft));

if(DotProduct(V1, V2) < 0.0f) {
return false;
}

V1 = CrossProduct(VectorSubtract(pTriangle->LowerRight, pTriangle->LowerLeft), VectorSubtract(*pPoint, pTriangle->LowerLeft));
V2 = CrossProduct(VectorSubtract(pTriangle->LowerRight, pTriangle->LowerLeft), VectorSubtract(pTriangle->UpperCenter, pTriangle->LowerLeft));

if(DotProduct(V1, V2) < 0.0f) {
return false;
}

return true;
}

And I do this to check the tile:
bool CMap::TileInRect(DWORD dwMapX, DWORD dwMapY, LPRECT3D pRect) {
RECT3D TileRect;
}else {
TileRect.LowerLeft.fX = (m_Header.fGeometryWidth / 2.0f) * (dwMapX - dwMapY) - (m_Header.fGeometryWidth / 2.0f);
TileRect.UpperLeft.fX = (m_Header.fGeometryWidth / 2.0f) * (dwMapX - dwMapY) - (m_Header.fGeometryWidth / 2.0f);
TileRect.UpperLeft.fY = (m_Header.fGeometryHeight / 2.0f) * (dwMapX + dwMapY);
TileRect.LowerRight.fX = (m_Header.fGeometryWidth / 2.0f) * (dwMapX - dwMapY) + (m_Header.fGeometryWidth / 2.0f);
TileRect.UpperRight.fX = (m_Header.fGeometryWidth / 2.0f) * (dwMapX - dwMapY) + (m_Header.fGeometryWidth / 2.0f);
TileRect.UpperRight.fY = (m_Header.fGeometryHeight / 2.0f) * (dwMapX + dwMapY);
}
//Prüfen ob sich die Rechtecke überschneiden
//Das Rechteck wird in 2 Dreiecke zerlegt und das gegen das Tile geprüft
TRIANGLE3D Triangle;

Triangle.LowerLeft = pRect->LowerLeft;
Triangle.LowerRight = pRect->LowerRight;
Triangle.UpperCenter = pRect->UpperLeft;
if(TrianglePointCollision(&(TileRect.LowerLeft), &Triangle)) {
return true;
}
if(TrianglePointCollision(&(TileRect.LowerRight), &Triangle)) {
return true;
}
if(TrianglePointCollision(&(TileRect.UpperLeft), &Triangle)) {
return true;
}
if(TrianglePointCollision(&(TileRect.UpperRight), &Triangle)) {
return true;
}

Triangle.LowerLeft = pRect->UpperLeft;
Triangle.LowerRight = pRect->LowerRight;
Triangle.UpperCenter = pRect->UpperRight;
if(TrianglePointCollision(&(TileRect.LowerLeft), &Triangle)) {
return true;
}
if(TrianglePointCollision(&(TileRect.LowerRight), &Triangle)) {
return true;
}
if(TrianglePointCollision(&(TileRect.UpperLeft), &Triangle)) {
return true;
}
if(TrianglePointCollision(&(TileRect.UpperRight), &Triangle)) {
return true;
}
return false;
}

And it returns true every time...

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013709
×