Advertisement

Find center and extents of AABB box

Started by September 30, 2018 01:23 PM
2 comments, last by TommyThree 6 years, 2 months ago

I have a function:


void findMinMax(XMFLOAT3& center, XMFLOAT3& extents)
{
	XMFLOAT3 vMin = XMFLOAT3(-INFINITY, -INFINITY, -INFINITY);
	XMFLOAT3 vMax = XMFLOAT3(INFINITY, INFINITY, INFINITY);

	XMVECTOR min = XMLoadFloat3(&vMin);
	XMVECTOR max = XMLoadFloat3(&vMax);

	for (int i = 0; i < meshList[0].vertices.size(); i++)
	{
		XMVECTOR p = XMLoadFloat3(&meshList[0].vertices[i].pos);
		min = XMVectorMin(min, p);
		max = XMVectorMax(max, p);
	}

	XMVECTOR c = 0.5f *(min + max);
	XMVECTOR e = 0.5f*(max - min);
	XMStoreFloat3(&center, c);
	XMStoreFloat3(&extents, e);
}

...got it out of the frank luna book. IT's supposed to find the extents and center of a AABB box I think. What am I doing wrong because its not working? The box doesn't draw. It draws when I put other values just not these vectors.

At a guess, the vMin and vMax declarations are swapped. The minimum of -INFINITY and some other number will always leave you with -INFINITY. They are used as "what is the current minimum/maximum we've found so far". Setting them to their "opposite" infinities allows any number to override them and get proper values.

I would guess the corrected version should be:


XMFLOAT3 vMin = XMFLOAT3(INFINITY, INFINITY, INFINITY);
XMFLOAT3 vMax = XMFLOAT3(-INFINITY, -INFINITY, -INFINITY);

 

Hello to all my stalkers.

Advertisement
7 minutes ago, Lactose said:

At a guess, the vMin and vMax declarations are swapped. The minimum of -INFINITY and some other number will always leave you with -INFINITY. They are used as "what is the current minimum/maximum we've found so far". Setting them to their "opposite" infinities allows any number to override them and get proper values.

I would guess the corrected version should be:



XMFLOAT3 vMin = XMFLOAT3(INFINITY, INFINITY, INFINITY);
XMFLOAT3 vMax = XMFLOAT3(-INFINITY, -INFINITY, -INFINITY);

 

Ahh thx. that was the problem.

This topic is closed to new replies.

Advertisement