Advertisement

Spaceolopy

Started by July 25, 2022 01:09 AM
86 comments, last by pbivens67 1 year, 11 months ago

well, I stubbed out some code as suggested, I am confused about what aabox really means as used in IntersectsBox() function.

#include <iostream>

using namespace std;

struct AABox
{
	int  minmax[2][2];

	bool IntersectsBox(const AABox &box) const
	{
		for (int i = 0; i < 2; i++)
		{
		if (box.minmax[1][i] < box.minmax[0][i] || box.minmax[0][i] > box.minmax[1][i])
		return false; // disjoint
		}
		return true; // intersecting
	}
};

int main()
{
	AABox aabox;
	cout << aabox.IntersectsBox(aabox) << endl;

	return 0;
}

pbivens67 said:
I am confused about what aabox really means as used in IntersectsBox() function.

You have introduced a bug / misunderstanding. The original code with some comments:

				struct AABox 
				{
					vec2 minmax[2]; // [0] are the minimum coords per dimension, [1] are the maximum coords
					/* it's not really good to use an array here, i should have used this instead:
					vec2 minCroner;
					vec2 maxCorner;
					*/
	
					bool IntersectsBox (const AABox &box) const // this intersets the current box (*this) with a scond box given by parameter
					{
						for (int i=0; i<2; i++)
						{
							if (box.minmax[1][i] < minmax[0][i] || box.minmax[0][i] > minmax[1][i])
								return false; // disjoint
						}
						return true; // intersecting
					}
				}

You have missed the detail about the current box, as noted in the comment.

To do an intersection of two boxes we would do this:

AABox boxA; // = {...}
AABox boxB; // = {...}
bool intersects = boxA.IntersectsBox(boxB);

It's because the function in my example is a member function of the box class, so one box is given by the instance calling the member function, only the second is given parameter.

Advertisement

pbivens67 said:
can you explain regular grid

Regular grid is just a simple grid:

Here it's a 4x4 grid with some objects linked to cells. The red square finds the blue circle because it is in an adjacent cell. But it ignores the green triangle because it's cell is too distant.
(To make this work robustly, we need to be careful about the cell size. If cells are too small, one object would need to be linked to multiple cells.)

The term ‘regular’ means that all cells have the same size like above. An ‘irregular’ grid would look like that:

…which is'n useful for anything i guess. But we see such divisions in kd-trees or BSP trees for example.

So the simplest grid we want to use if we can is both 'regular' and ‘dense’.

Opposed to a dense grid, a ‘sparse’ grid would look like this:

Basically we try to have grid cells only where we need them, but we still aim to find them quickly by a given location or range.
That's very useful and exists in many different variations.



well, I figured out my stubbed-out code

#include <iostream>

using namespace std;

struct AABox
{
	int  minmax[2][2] = { {1, 1},{0, 0} };

	bool IntersectsBox(const AABox &box) const
	{
		for (int i = 0; i < 2; i++)
		{
			if (box.minmax[1][i] < box.minmax[0][i] || box.minmax[0][i] > box.minmax[1][i])
			{
			return false; // disjoint
			}
		}
		return true; // intersecting
	}
};

int main()
{
	AABox aabox;
	cout << aabox.IntersectsBox(aabox) << endl;

	return 0;
}

Can't work, because you still use only one box for the intersection test:

pbivens67 said:
if (box.minmax[1][i] < box.minmax[0][i] || box.minmax[0][i] > box.minmax[1][i])

All coordinates here come from the second box given by function parameter, but the (*this) box is ignored.

then how do I implement two boxes, it seems to work

Advertisement

Spot the difference.
Yours:

pbivens67 said:
if (box.minmax[1][i] < box.minmax[0][i] || box.minmax[0][i] > box.minmax[1][i])

mine:

JoeJ said:
if (box.minmax[1][i] < minmax[0][i] || box.minmax[0][i] > minmax[1][i])

pbivens67 said:
it seems to work

You need more test cases. To be sure something works, the test cases need to cover all potential branches the code might take.

here is my new code, it still works

#include <iostream>

using namespace std;

struct AABox
{
	int  minmax[2][2] = { {1, 1},{0, 0} };

	bool IntersectsBox(const AABox &box) const
	{
		for (int i = 0; i < 2; i++)
		{
			if (box.minmax[1][i] < minmax[0][i] || box.minmax[0][i] > minmax[1][i])
			{
			return false; // disjoint
			}
		}
		return true; // intersecting
	}
};

int main()
{
	AABox aabox;
	cout << aabox.IntersectsBox(aabox) << endl;

	return 0;
}

pbivens67 said:
int main() { AABox aabox; cout << aabox.IntersectsBox(aabox) << endl; return 0; }

Ok, this clarifies why you did not notice the bug. Because for the test you also used just one box. : )

But well, you'll see when using it in game if it actually works or not. I guess it does.

ok well how do I do two or more boxes?

This topic is closed to new replies.

Advertisement