Advanced C++ Tutorial 2 HW star overlapped question

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
WilsonHuang
Posts: 44
Joined: February 13th, 2019, 3:23 am

Advanced C++ Tutorial 2 HW star overlapped question

Post by WilsonHuang » August 12th, 2020, 4:01 pm

I'm doing Advanced C++ Tutorial 2 homework. One of tasks is that generating the stars which do not overlapped each other.
I wrote following code to check overlapped stars, but it's not working (the star still overlapped). What's wrong with my code?

Code: Select all

bool Game::IsOverlapped(Vec2 position, float radius)
{
	return
	std::any_of(stars.begin(), stars.end(), [=](MyStar& star)
		{
			float starRadius = star.GetRadius();
			Vec2 starPosition = star.GetPosition();
			return (starPosition - position).GetLength() <=
				(starRadius + radius);
		});
}
Here is full solution
advmath.zip
(601.37 KiB) Downloaded 186 times

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Advanced C++ Tutorial 2 HW star overlapped question

Post by albinopapa » August 12th, 2020, 7:58 pm

Luckily you uploaded your solution, because the problem isn't in the function at all.

Code: Select all

	std::uniform_real_distribution<float> distOuter(10.0f, 100.0f);
	std::uniform_real_distribution<float> distInner(5.0f, 50.0f);
I originally thought that this was the problem, since there is overlap between the largest inner and smallest outer radii and you only check for outerRadius when checking for overlap. However, even after swapping them if outer < inner, there's still overlap. I wasn't able to find the problem.

I translated all the points of each star and tested for collision and it found collisions with the points of the stars, but when testing distance between the stars against the sum of the radii, no collisions were found.

Sorry I don't have a solution for you. The only thing I can think that would cause the issue is something in the transformation.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

WilsonHuang
Posts: 44
Joined: February 13th, 2019, 3:23 am

Re: Advanced C++ Tutorial 2 HW star overlapped question

Post by WilsonHuang » August 13th, 2020, 1:15 am

albinopapa wrote:
August 12th, 2020, 7:58 pm
Luckily you uploaded your solution, because the problem isn't in the function at all.

Code: Select all

	std::uniform_real_distribution<float> distOuter(10.0f, 100.0f);
	std::uniform_real_distribution<float> distInner(5.0f, 50.0f);
I originally thought that this was the problem, since there is overlap between the largest inner and smallest outer radii and you only check for outerRadius when checking for overlap. However, even after swapping them if outer < inner, there's still overlap. I wasn't able to find the problem.

I translated all the points of each star and tested for collision and it found collisions with the points of the stars, but when testing distance between the stars against the sum of the radii, no collisions were found.

Sorry I don't have a solution for you. The only thing I can think that would cause the issue is something in the transformation.
You are right. I didn't check if length of inner radius is greater than the outer radius. I didn't found the overlapped stars after I add the check condition and swap the two radii.

Code: Select all

float outerRadius = distOuter(random_engine);
float innerRadius = distInner(random_engine);
if (innerRadius >= outerRadius)
{
	std::swap(innerRadius, outerRadius);
}

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Advanced C++ Tutorial 2 HW star overlapped question

Post by albinopapa » August 13th, 2020, 1:33 am

cool
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply