Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feedback Exact collision detection proposal

Discussion in 'General Graphics' started by aschonfe, Oct 14, 2023.

  1. aschonfe

    aschonfe

    Joined:
    Jul 15, 2023
    Posts:
    6
    Hello community,

    I have successfully implemented a compound collider based on primitives (In my case cubes). Now I have to implement the exact collision on polygon level.
    The built-in methods CCD and speculative CCD did not sound to me the right ones since they seemed to be targeting fast moving objects. That would not be a requirement in my case.

    My idea was the outlined stepwise approach below. Any feedback would be highly welcome regarding:
    • Have I missed a built-in unity feature that would make it easier
    • if yes: starting point how to do it better
    • Assumption of algorithm: Correct to avoid using MeshCollider as long as possible
    My scenario:
    • A tool e.g. a chisel (marked red) is hitting a sculpture (marked in blue). The sculpture consists of multiple convex shapes with box colliders
    • Technically: Tool has a rigidbody attached and the collisionproivder is marked as trigger
    • Need for exact collision: box collider don't mesh the underlying mesh exactly
    Planned stepwise approach (see also attached picture Step 0 = starting situation; Steps 1 - 3 as described above)
    1. Standard box collider test with cubes
    Done between: Box colliders of tool and all box colliders of the convex shapes within the shape
    Yields: Multiple convex shapes that collided with tool based on box colliders

    2. Filter relevant edges against box collider tool
    Done between: Edges of relevant sculpture shapes against box collider tool that collided
    How done:

    My ideas is to do a check with multiple raycasts. Each fired ray would exactly match one edge of the convex shape. Since the cube based collider is also convex testing the edges should be enough. Per tested edge the result is stored (Hit at all; convex shape; which collider within the tool hit).
    What is the result:
    • Relevant convex shapes that need to be tested exactly (=> relevant if at least one ray check hit)
    • Relevant edges for exact test (Only those collided with the simplified bounding box tool)
    3. Exact raycast check against exact tool meshcollider
    Raycheck as before per relevant edge against tool with exact mesh collider. Raytest is limited per layer to the mesh collider. If at least one ray collided there is a collision.
     

    Attached Files:

  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    853
    Is this simply meant to be a sculpting simulation? In other words, is there anything else significant going on in your game physics-wise? If not, you can just skip steps 1 and 2 and go straight to 3.

    Computers are pretty darn fast at calculating stuff so unless you need to scale up quite a bit (like you need to do this with hundreds of statutes at once) there is really no reason to bother with all of that extra work until you know it will actually hurt performance. Under the hood it will almost certainly be doing something similar to steps 1 and 2 anyway.

    However, do note that there is a pretty severe limit to how many vertices a mesh collider can have so you might need to use more than one or have some kind of system that dynamically subdivides into new colliders as the mesh becomes more complex. You also cannot use concave meshes if there is a rigidbody attached to the GameObject. There is also the issue that the mesh colliders won't always have the level of accuracy you might expect, especially with concave shapes. If you really need something that is super accurate it might be necessary to roll your own collision simulation for this. It would probably also run a lot faster (which may not even be an issue in this case) since you could make a lot more assumptions about the search spaces for the individual faces of the mesh than a general-purpose physics engine ever could.
     
    aschonfe likes this.
  3. aschonfe

    aschonfe

    Joined:
    Jul 15, 2023
    Posts:
    6
    Hello Sluggy,

    absolute fantastic to get such a fast answer and you hit exactly the point where I was not sure about (=whether you need the intermediate steps or not). Will go then with much more confidence into trying to implement it.

    Regarding your question: Yes it is just a sculpting simulation. So just one sculpture the player is working at.

    Best regards
    Albrecht