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\$
  • \$\begingroup\$ Sounds like you should show us your loop code if you want help debugging it. You're probably checking a point or radius slightly off from where you think you are. \$\endgroup\$ – DMGregory Jun 26 '19 at 16:23
  • \$\begingroup\$ there you go, I added my loop \$\endgroup\$ – Samuel Fyckes Jun 26 '19 at 16:59
  • \$\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 Jun 26 '19 at 17:14
  • \$\begingroup\$ A 1 sphere radius make it worse. \$\endgroup\$ – Samuel Fyckes Jun 26 '19 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 Jun 26 '19 at 18:27
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)
| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Samuel Fyckes Jun 28 '19 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\$ – Willtheoct Jun 29 '19 at 18:09
  • \$\begingroup\$ Just tried out, simple box with simple box collider 1x1x1, still doing the same thing \$\endgroup\$ – Samuel Fyckes Jun 29 '19 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 Jul 7 '19 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.