Search Unity

Reusing entire AABB tree/collision detection system from Unity Physics?

Discussion in 'Physics for ECS' started by Mr-Mechanical, Nov 21, 2019.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Hi,

    I'm impressed and intrigued by Unity's physics system, however, my use-case only requires Broadphase tree/Collision detection and no rigid body motion or collision responses. All I need is the physics engine to output collision data from convex hulls without actually simulating any rigid bodies. Before I was just curious about the GJK implementation but now I realize I should also consider reusing the Broadphase aswell for efficiency. What's the best way to go about this (reuse broad phase/pruning without rigid body motion simulation)?

    Thank you for the responses, they are greatly helpful for my ongoing project and geniunely appreciated.
     
    Last edited: Nov 21, 2019
  2. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    A small note, for my use case, all objects are convex hulls (no capsules, spheres, boxes etc). Though I would guess this shouldn't change too much
     
    Last edited: Nov 21, 2019
  3. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    Look at Unity.Physics.Simulation.ScheduleStepJobs() to see the full step. You could pull out the bits you need from there. The bits you need are buried within other bits but depending on how determined you are, you should be able to extract what you need. I can only give you some tips to get started:

    You will need to populate a CollisionWorld somehow and build its broadphase (ScheduleBuildJobs). This is usually done by BuildPhysicsWorld system.

    You will need to find the overlapping body pairs in the broadphase (look at ScheduleFindOverlapsJobs). Note this does not check static vs static, so you could modifiy it or set dummy dynamics info on the bodies you are interested in.

    You will need to dispatch each resulting pair to narrowphase collision detection (ManifoldQueries.BodyBody()). ScheduleCreatePhasedDispatchPairsJob and ScheduleProcessBodyPairsJobs wrap around that to handle multithreading and joints too.

    Note the currently code is all focused on multithreaded use cases, but soon we will also have an API for single threaded stepping which will make some of these parts more accessible.
     
    Mr-Mechanical likes this.
  4. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Thank you greatly for your reply. This advice is extremely helpful for my project, I really appreciate it. I've been studying the code mentioned for the past couple of days. So it seems from my understanding so far is that I can call the following scheduling functions from Unity Physics safely with the proper CollisionWorld created.

    It seems complex to extract the code properly and make it compatible with my system, so I'm trying to minimize modification. I now realize, the only difference between Unity Physics and my system is that instead of the integrator controlling the motion of the rigidbody/hull, I have my own system with changes the shapes of each hull, and position of each hull every frame.

    TL;DR/question:
    Is there some easy way to make some sort of "Kinematic" like mode that disables the integrator/motion system, so I can have my own system to constantly take control over positions of each dynamic hull to examine collisions results (and then my system will decide what to do)?

    Thank you very much, the advice is seriously helpful and appreciated.
     
  5. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507