Search Unity

Procedurally Generated Meshes Sometimes Not Colliding

Discussion in 'Physics for ECS' started by craftsmanbeck, May 24, 2020.

  1. craftsmanbeck

    craftsmanbeck

    Joined:
    Nov 20, 2010
    Posts:
    40
    I'm creating a voxel terrain system that loads the land in chunks, and I create MeshColliders on the fly. The issue is maybe one out of every twenty chunks or so doesn't collide with anything or respond to raycasts. The mesh and edges are visible when using PhysicsDebugDisplayData. Re-running the physics mesh creation system on the relevant terrain chunk fixes the issue, as does creating new terrain chunks in unrelated areas.
    My hunch is that the broadphase isn't capturing them properly and there's some internal optimization that only runs when there's a change in a memory chunk, and somehow that's not getting triggered, but I don't know how to tell or what to do about it. Is there anything else that could cause a collision mesh to be visible with Debug but not interact with anything?
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Hey, if you're up for some deeper debugging, you could go into BuildPhysicsWorld.cs and look a t haveStatucBodiesChanged array of size 1. haveStaticBodiesChanges[0] will be set to 1 if we detected a change in the colliders of any kind (I'm assuming the stuff you are adding is static). If it doesn't detect the change, that's most likely the reason you are not seeing collisions with it. This is just a guess of course, so let's check and continue from there...
     
    craftsmanbeck likes this.
  3. craftsmanbeck

    craftsmanbeck

    Joined:
    Nov 20, 2010
    Posts:
    40
    Hey thanks for the response. Yeah they're static, or at least just don't have a PhysicsVelocity component added. I added a debug statement into the body count changed section, and that being triggered sometimes caused the mesh to be fixed, but not all the time. In fact, when body count is triggered, it sometimes also seems to break formerly working colliders. However, it doesn't seem like whether they work or not ever changes without the debug message being triggered. Also worth noting I've tried both including the PhysicsCollider in the archetype and then setting its value, as well as adding it in a command buffer, to no avail. Also the issue occurs even when disabling Use Job Threads, so it's not a race condition.
     
  4. Adam-Mechtley

    Adam-Mechtley

    Administrator

    Joined:
    Feb 5, 2007
    Posts:
    290
    Just to clarify: When you create these on the fly, are you going through MeshCollider.Create() each time and writing back to PhysicsCollider.Value, or are you using unsafe code to modify some mesh collider that was already created?
     
    craftsmanbeck likes this.
  5. craftsmanbeck

    craftsmanbeck

    Joined:
    Nov 20, 2010
    Posts:
    40
    I'm using MeshCollider.Create in an Entities.ForEach, and have tried both using a CommandBuffer to add and set a new PhysicsCollider with the Value from it, as well as just setting the value of a PhysicsCollider passed by ref. Both behave identically as described earlier.
     
  6. Adam-Mechtley

    Adam-Mechtley

    Administrator

    Joined:
    Feb 5, 2007
    Posts:
    290
    I think this might be hard for us to diagnose further here. Are you able to submit a bug report with a minimal repro that we can take a look at?
     
    craftsmanbeck likes this.
  7. craftsmanbeck

    craftsmanbeck

    Joined:
    Nov 20, 2010
    Posts:
    40
    I'll see if I can put one together in the next few days. Thanks in advance.
     
    Adam-Mechtley likes this.
  8. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    petarmHavok and cultureulterior like this.
  9. craftsmanbeck

    craftsmanbeck

    Joined:
    Nov 20, 2010
    Posts:
    40
    If I'm understanding it correctly, that would only cause a delay of a frame, not permanent collider not working. Further, that would cause the problem to be uniform across all meshes, not just happening on some. Additionally, it would not cause meshes that formerly worked to break. Correct me if I'm wrong.
     
    MNNoxMortem likes this.
  10. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    You are right.
     
    craftsmanbeck likes this.