0
\$\begingroup\$

I try to use Physics.CheckSphere to check if a coordonate is empty. I use three for loop to cycle threw a 2 x Y x 2 chunk. The problem is that it return true if the corner is touching the coordonateenter image description here

and its actually not even touching it, since I use a 0.5 sphere radius: if(Physics.CheckSphere(new Vector3(x,y,z), 0.5f) == false)...

(by the way the white cubes you can see on the picture is only to visualize where it detected empty space, they dont have any collider enabled)

and the chunks have a mesh collider, so why is it returning true when its no even touching it ?

if(Input.GetKeyDown("i")) {

for(int y = 0; y <= amp + 3; y++)
{
    for(int x = 0; x <= chunkSize - 1; x++)
    {
        for(int z = 0; z <= chunkSize - 1; z++)
        {
            if(Physics.CheckSphere(new Vector3(Mathf.Round(x),Mathf.Round(y),Mathf.Round(z)),0.5f) == false)
            {
                Instantiate(grass,new Vector3(x,y,z), Quaternion.identity);
                dontPlaceHere.Add(new Vector3(x,y,z));
            }
        }
    }
}
}
\$\endgroup\$
6
  • \$\begingroup\$ A 0.5 radius sphere placed at eg. (0, 0, 0) will have a diameter from (0, 0, -0.5) to (0, 0, 0.5). A 1-unit wide cube centered at (0, 0, 1) will cover the interval (0, 0, 0.5) to (0, 0, 1.5). So these two volumes will kiss (overlap at exactly one point) at (0, 0, 0.5). So, would we not expect an overlap to be detected in that case? \$\endgroup\$
    – DMGregory
    Commented Jun 26, 2019 at 17:14
  • \$\begingroup\$ A 1 sphere radius make it worse. \$\endgroup\$ Commented Jun 26, 2019 at 18:26
  • \$\begingroup\$ Obviously, making the radius larger will lead to more collisions being detected, which is why I did not suggest such a thing. \$\endgroup\$
    – DMGregory
    Commented Jun 26, 2019 at 18:27
  • \$\begingroup\$ what do you suggest then \$\endgroup\$ Commented Jun 26, 2019 at 18:27
  • \$\begingroup\$ I'm trying to understand your problem. The behaviour you describe sounds like exactly the outcome we'd expect given the code you wrote. Did you mean to use a smaller radius so that kissing contacts are not included as overlaps? \$\endgroup\$
    – DMGregory
    Commented Jun 26, 2019 at 18:29

1 Answer 1

1
\$\begingroup\$

If you want to stop the 'kissing' from triggering a detection, you'll need to make sure it doesn't kiss.

Use a smaller detection radius. DMGregory points out that 0.49999997f would work.

            if(Physics.CheckSphere(new Vector3(Mathf.Round(x),Mathf.Round(y),Mathf.Round(z)),0.49999997f) == false)
\$\endgroup\$
4
  • \$\begingroup\$ Nice try, but the result dont change, and this is understandable, since initially I wanted the value to be 1 instead of 0.5 , but if 1 - 0.5 is still colliding, i dont see why 0.5 - mathf.epsilon would have worked. \$\endgroup\$ Commented Jun 28, 2019 at 12:58
  • \$\begingroup\$ I can only imagine that your mesh colliders are bigger than a 1x1 box, then. What happens when you remove the mesh colliders and replace them with boxes? \$\endgroup\$ Commented Jun 29, 2019 at 18:09
  • \$\begingroup\$ Just tried out, simple box with simple box collider 1x1x1, still doing the same thing \$\endgroup\$ Commented Jun 29, 2019 at 18:17
  • \$\begingroup\$ Epsilon is not an appropriate value to use here. It's the smallest representable float distinct from zero, or about 1.4 * 10^-45. But in the vicinity of 0.5f, we have precision to only about 1.5 * 10^-0.8. So 0.5f - Mathf.Epsilon rounds correctly to 0.5f and accomplishes nothing. The next representable float smaller than 0.5f is 0.49999997f \$\endgroup\$
    – DMGregory
    Commented Jul 7, 2019 at 17:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .