<commit_before>#include <Internal/PhysicsModuleImplementation.hpp>


namespace DoremiEngine
{
    namespace Physics
    {
        PhysicsModuleImplementation::PhysicsModuleImplementation(const Core::SharedContext& p_sharedContext) : m_sharedContext(p_sharedContext) {}
        PhysicsModuleImplementation::~PhysicsModuleImplementation() {}

        void PhysicsModuleImplementation::Startup()
        {
            // Start physX
            m_utils.m_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, m_utils.m_allocator, m_utils.m_errorCallback);
            PxProfileZoneManager* profileZoneManager = &PxProfileZoneManager::createProfileZoneManager(m_utils.m_foundation);
            m_utils.m_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *m_utils.m_foundation, PxTolerancesScale(), true, profileZoneManager);

            // Create world scene TODOJB create scene handler for this kind of job
            CreateWorldScene();

            // Create sub modules
            m_utils.m_rigidBodyManager = new RigidBodyManagerImpl(m_utils);
            m_utils.m_physicsMaterialManager = new PhysicsMaterialManagerImpl(m_utils);
            m_utils.m_characterControlManager = new CharacterControlManagerImpl(m_utils);
            m_utils.m_fluidManager = new FluidManagerImpl(m_utils);
            m_utils.m_rayCastManager = new RayCastManagerImpl(m_utils);

            // Make some other important thingies
            m_utils.m_characterControlManager->SetCallbackClass(this);

            if(m_utils.m_physics->getPvdConnectionManager() == NULL)
            {
                int failed = 1;
            }
            PxVisualDebuggerConnection* theConnection =
                PxVisualDebuggerExt::createConnection(m_utils.m_physics->getPvdConnectionManager(), "127.0.0.1", 5425, 100,
                                                      PxVisualDebuggerExt::getAllConnectionFlags());
            if(theConnection) theConnection->release();
        }

        void PhysicsModuleImplementation::Shutdown() {}

        void PhysicsModuleImplementation::Update(float p_dt)
        {
            // Start by clearing the list of collision pairs (WARNING potentially bad idea)
            m_collisionPairs.clear();
            m_triggerPairs.clear();
            m_utils.m_fluidManager->Update(p_dt);
            m_utils.m_worldScene->simulate(p_dt);
            m_utils.m_rigidBodyManager->ClearRecentlyWakeStatusLists();
            m_utils.m_worldScene->fetchResults(true);
        }

        RigidBodyManager& PhysicsModuleImplementation::GetRigidBodyManager() { return *m_utils.m_rigidBodyManager; }
        PhysicsMaterialManager& PhysicsModuleImplementation::GetPhysicsMaterialManager() { return *m_utils.m_physicsMaterialManager; }
        CharacterControlManager& PhysicsModuleImplementation::GetCharacterControlManager() { return *m_utils.m_characterControlManager; }
        FluidManager& PhysicsModuleImplementation::GetFluidManager() { return *m_utils.m_fluidManager; }
        RayCastManager& PhysicsModuleImplementation::GetRayCastManager() { return *m_utils.m_rayCastManager; };

        vector<CollisionPair> PhysicsModuleImplementation::GetCollisionPairs() { return m_collisionPairs; }
        vector<CollisionPair> PhysicsModuleImplementation::GetTriggerPairs() { return m_triggerPairs; }

        // Custom collision filter shader
        PxFilterFlags TestFilter(PxFilterObjectAttributes attributes0, PxFilterData filterData0, PxFilterObjectAttributes attributes1,
                                 PxFilterData filterData1, PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
        {
            // Rigid bodies collisions
            if(PxFilterObjectIsTrigger(attributes0) || PxFilterObjectIsTrigger(attributes1))
            {
                pairFlags = PxPairFlag::eTRIGGER_DEFAULT;
                pairFlags |= PxPairFlag::eNOTIFY_TOUCH_PERSISTS;
                pairFlags |= PxPairFlag::eNOTIFY_TOUCH_FOUND;
                pairFlags |= PxPairFlag::eNOTIFY_TOUCH_LOST;
                return PxFilterFlag::eDEFAULT;
            }

            // controller vs. controller collisions
            bool kinematic0 = PxFilterObjectIsKinematic(attributes0);
            bool kinematic1 = PxFilterObjectIsKinematic(attributes1);

            if(kinematic0 && kinematic1)
            {
                pairFlags = PxPairFlag::eTRIGGER_DEFAULT;
                return PxFilterFlag::eDEFAULT;
            }

            // generate contacts for all that were not filtered above
            pairFlags = PxPairFlag::eCONTACT_DEFAULT;

            // trigger the contact callback for pairs (A,B) where
            // the filtermask of A contains the ID of B and vice versa.
            if((filterData0.word0 & filterData1.word1) && (filterData1.word0 & filterData0.word1))
            {
                pairFlags |= PxPairFlag::eNOTIFY_TOUCH_FOUND;
            }

            return PxFilterFlag::eDEFAULT;
        }

        void PhysicsModuleImplementation::CreateWorldScene()
        {
            /// Create the scene
            // No idea what the "tolerances" is
            PxSceneDesc sceneDesc(m_utils.m_physics->getTolerancesScale());
            // Gravity sounds straight forward
            sceneDesc.gravity = PxVec3(0.0f, -9.8f, 0.0f);
            // Not too sure what this is. Probably related to how threads work
            m_utils.m_dispatcher = PxDefaultCpuDispatcherCreate(2);
            sceneDesc.cpuDispatcher = m_utils.m_dispatcher;
            // Some way of filtering collisions. Use default shaders since we cba to write our own
            sceneDesc.filterShader = TestFilter;
            // Notify PhysX that we want callbacks to be called here
            sceneDesc.simulationEventCallback = this;

            // Create the scene<commit_msg>Improved collision filter. Makes more sense now<commit_after>#include <Internal/PhysicsModuleImplementation.hpp>


namespace DoremiEngine
{
    namespace Physics
    {
        PhysicsModuleImplementation::PhysicsModuleImplementation(const Core::SharedContext& p_sharedContext) : m_sharedContext(p_sharedContext) {}
        PhysicsModuleImplementation::~PhysicsModuleImplementation() {}

        void PhysicsModuleImplementation::Startup()
        {
            // Start physX
            m_utils.m_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, m_utils.m_allocator, m_utils.m_errorCallback);
            PxProfileZoneManager* profileZoneManager = &PxProfileZoneManager::createProfileZoneManager(m_utils.m_foundation);
            m_utils.m_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *m_utils.m_foundation, PxTolerancesScale(), true, profileZoneManager);

            // Create world scene TODOJB create scene handler for this kind of job
            CreateWorldScene();

            // Create sub modules
            m_utils.m_rigidBodyManager = new RigidBodyManagerImpl(m_utils);
            m_utils.m_physicsMaterialManager = new PhysicsMaterialManagerImpl(m_utils);
            m_utils.m_characterControlManager = new CharacterControlManagerImpl(m_utils);
            m_utils.m_fluidManager = new FluidManagerImpl(m_utils);
            m_utils.m_rayCastManager = new RayCastManagerImpl(m_utils);

            // Make some other important thingies
            m_utils.m_characterControlManager->SetCallbackClass(this);

            if(m_utils.m_physics->getPvdConnectionManager() == NULL)
            {
                int failed = 1;
            }
            PxVisualDebuggerConnection* theConnection =
                PxVisualDebuggerExt::createConnection(m_utils.m_physics->getPvdConnectionManager(), "127.0.0.1", 5425, 100,
                                                      PxVisualDebuggerExt::getAllConnectionFlags());
            if(theConnection) theConnection->release();
        }

        void PhysicsModuleImplementation::Shutdown() {}

        void PhysicsModuleImplementation::Update(float p_dt)
        {
            // Start by clearing the list of collision pairs (WARNING potentially bad idea)
            m_collisionPairs.clear();
            m_triggerPairs.clear();
            m_utils.m_fluidManager->Update(p_dt);
            m_utils.m_worldScene->simulate(p_dt);
            m_utils.m_rigidBodyManager->ClearRecentlyWakeStatusLists();
            m_utils.m_worldScene->fetchResults(true);
        }

        RigidBodyManager& PhysicsModuleImplementation::GetRigidBodyManager() { return *m_utils.m_rigidBodyManager; }
        PhysicsMaterialManager& PhysicsModuleImplementation::GetPhysicsMaterialManager() { return *m_utils.m_physicsMaterialManager; }
        CharacterControlManager& PhysicsModuleImplementation::GetCharacterControlManager() { return *m_utils.m_characterControlManager; }
        FluidManager& PhysicsModuleImplementation::GetFluidManager() { return *m_utils.m_fluidManager; }
        RayCastManager& PhysicsModuleImplementation::GetRayCastManager() { return *m_utils.m_rayCastManager; };

        vector<CollisionPair> PhysicsModuleImplementation::GetCollisionPairs() { return m_collisionPairs; }
        vector<CollisionPair> PhysicsModuleImplementation::GetTriggerPairs() { return m_triggerPairs; }

        // New fancier collision filter
        PxFilterFlags TestFilter2(PxFilterObjectAttributes attributes0, PxFilterData filterData0, PxFilterObjectAttributes attributes1,
                                  PxFilterData filterData1, PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
        {
            // generate contacts for all that were not filtered above
            pairFlags = PxPairFlag::eCONTACT_DEFAULT;

            // General filter if they match
            if((filterData0.word0 & filterData1.word1) && (filterData1.word0 & filterData0.word1))
            {
                pairFlags |= PxPairFlag::eNOTIFY_TOUCH_FOUND;
            }


            // Filter out collisions with ignore-bodies REALLY UGLY TODOJB Improve
            if(filterData0.word3 == 1 || filterData1.word3 == 1) // been an ignore collision
            {
                if(filterData0.word0 == 1 || filterData1.word0 == 1) return PxFilterFlag::eKILL;
            }

            // Trigger collisions
            if(PxFilterObjectIsTrigger(attributes0) || PxFilterObjectIsTrigger(attributes1))
            {
                pairFlags = PxPairFlag::eTRIGGER_DEFAULT;
                return PxFilterFlag::eDEFAULT;
            }

            return PxFilterFlags();
        }

        // Custom collision filter shader (OLD DEPRICATED CRAP. Still kept around for safety)
        // PxFilterFlags TestFilterOLD(PxFilterObjectAttributes attributes0, PxFilterData filterData0, PxFilterObjectAttributes attributes1,
        //                         PxFilterData filterData1, PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
        //{
        //    // Rigid bodies collisions
        //    if(PxFilterObjectIsTrigger(attributes0) || PxFilterObjectIsTrigger(attributes1))
        //    {
        //        pairFlags = PxPairFlag::eTRIGGER_DEFAULT;
        //        pairFlags |= PxPairFlag::eNOTIFY_TOUCH_PERSISTS;
        //        pairFlags |= PxPairFlag::eNOTIFY_TOUCH_FOUND;
        //        pairFlags |= PxPairFlag::eNOTIFY_TOUCH_LOST;
        //        return PxFilterFlag::eDEFAULT;
        //    }

        //    // controller vs. controller collisions
        //    bool kinematic0 = PxFilterObjectIsKinematic(attributes0);
        //    bool kinematic1 = PxFilterObjectIsKinematic(attributes1);

        //    if(kinematic0 && kinematic1)
        //    {
        //        pairFlags = PxPairFlag::eTRIGGER_DEFAULT;
        //        return PxFilterFlag::eDEFAULT;
        //    }

        //    // generate contacts for all that were not filtered above
        //    pairFlags = PxPairFlag::eCONTACT_DEFAULT;

        //    // trigger the contact callback for pairs (A,B) where
        //    // the filtermask of A contains the ID of B and vice versa.
        //    if((filterData0.word0 & filterData1.word1) && (filterData1.word0 & filterData0.word1))
        //    {
        //        pairFlags |= PxPairFlag::eNOTIFY_TOUCH_FOUND;
        //    }

        //    return PxFilterFlag::eDEFAULT;
        //}

        void PhysicsModuleImplementation::CreateWorldScene()
        {
            /// Create the scene
            // No idea what the "tolerances" is
            PxSceneDesc sceneDesc(m_utils.m_physics->getTolerancesScale());
            // Gravity sounds straight forward
            sceneDesc.gravity = PxVec3(0.0f, -9.8f, 0.0f);
            // Not too sure what this is. Probably related to how threads work
            m_utils.m_dispatcher = PxDefaultCpuDispatcherCreate(2);
            sceneDesc.cpuDispatcher = m_utils.m_dispatcher;
            // Some way of filtering collisions. Use default shaders since we cba to write our own
            sceneDesc.filterShader = TestFilter2;
            // Notify PhysX that we want callbacks to be called here
            sceneDesc.simulationEventCallback = this;

            // Create the scene
{'fraction_non_alphanumeric': 0.07911029580371474, 'fraction_numerical': 0.009019338072307575, 'mean_word_length': 2.520043045466774, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1215575', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3450, 'n_words': 666}