Jump to content
  • Advertisement

Archived

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

Hippokrates

Triangle Point Collision

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

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;
	if(MAP_TILE == m_Header.byMode) {
		TileRect.UpperLeft.fX = dwMapX * m_Header.fGeometryWidth;
		TileRect.UpperLeft.fY = dwMapY * -(m_Header.fGeometryHeight);
		TileRect.UpperLeft.fZ = m_Header.fZoom;
		TileRect.LowerLeft.fX = dwMapX * m_Header.fGeometryWidth;
		TileRect.LowerLeft.fY = dwMapY * -(m_Header.fGeometryHeight) - m_Header.fGeometryHeight;
		TileRect.LowerLeft.fZ = m_Header.fZoom;
		TileRect.UpperRight.fX = dwMapX * m_Header.fGeometryWidth + m_Header.fGeometryWidth;
		TileRect.UpperRight.fY = dwMapY * -(m_Header.fGeometryHeight);
		TileRect.UpperRight.fZ = m_Header.fZoom;
		TileRect.LowerRight.fX = dwMapX * m_Header.fGeometryWidth + m_Header.fGeometryWidth;
		TileRect.LowerRight.fY = dwMapY * -(m_Header.fGeometryHeight) - m_Header.fGeometryHeight;
		TileRect.LowerRight.fZ = m_Header.fZoom;
	}else {
		TileRect.LowerLeft.fX = (m_Header.fGeometryWidth / 2.0f) * (dwMapX - dwMapY) - (m_Header.fGeometryWidth / 2.0f);	
		TileRect.LowerLeft.fY = (m_Header.fGeometryHeight / 2.0f) * (dwMapX + dwMapY) - m_Header.fGeometryHeight;
		TileRect.LowerLeft.fZ = m_Header.fZoom;
		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.UpperLeft.fZ = m_Header.fZoom;
		TileRect.LowerRight.fX = (m_Header.fGeometryWidth / 2.0f) * (dwMapX - dwMapY) + (m_Header.fGeometryWidth / 2.0f);
		TileRect.LowerRight.fY = (m_Header.fGeometryHeight / 2.0f) * (dwMapX + dwMapY) - m_Header.fGeometryHeight;
		TileRect.LowerRight.fZ = m_Header.fZoom;
		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);
		TileRect.UpperRight.fZ = m_Header.fZoom;
	}
	//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...

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!