void CollisionSystem::Update( double DeltaTime ) { static const auto enableCollision = Settings::Get().GetInt( "collisions.enable", 1 ) != 0; if( !enableCollision ) { return; } mUpdateTimer.Log( "start collision" ); mPerfTimer.Log( "pre build grid" ); std::vector<std::pair<Opt<CollisionSubSystem>, Actor*>> collisionAndActors; for (auto actor : mScene.GetActorsFromMap( GetType_static() )) { Opt<ICollisionComponent> collisionC = actor->Get<ICollisionComponent>(); if ( collisionC.IsValid() ) { Opt<CollisionSubSystem> collisionSS = GetCollisionSubSystem( collisionC->GetId() ); if ( collisionSS.IsValid() ) { collisionAndActors.push_back( std::make_pair( collisionSS, actor ) ); collisionSS->ClipScene( *actor ); } mCollisionGrid.AddActor( actor, DeltaTime, collisionC ); } } mPerfTimer.Log( "post build grid" ); PossibleCollisions_t const& PossibleCollisions = mCollisionGrid.GetPossibleCollisions(); for( PossibleCollisions_t::const_iterator i = PossibleCollisions.begin(), e = PossibleCollisions.end(); i != e; ++i ) { Actor& A = *( i->A1 ); Actor& B = *( i->A2 ); Opt<ICollisionComponent> ACollisionC = A.Get<ICollisionComponent>(); Opt<ICollisionComponent> BCollisionC = B.Get<ICollisionComponent>(); BOOST_ASSERT( ACollisionC.IsValid() && BCollisionC.IsValid() ); //TODO: here this one should be true CollisionModel const& CollModel = mCollisionStore.GetCollisionModel( ACollisionC->GetCollisionClass(), BCollisionC->GetCollisionClass() ); if( !CollModel.AreActorsColliding( A, B, DeltaTime ) ) { continue; } //TODO: needs optimization, maybe a template parameter for SubSystemHolder to subsystem would do Opt<CollisionSubSystem> ACollisionSS = GetCollisionSubSystem( ACollisionC->GetId() ); if ( ACollisionSS.IsValid() ) { ACollisionSS->Collide( A, B ); } Opt<CollisionSubSystem> BCollisionSS = GetCollisionSubSystem( BCollisionC->GetId() ); if ( BCollisionSS.IsValid() ) { BCollisionSS->Collide( B, A ); } } mPerfTimer.Log( "post collide" ); for (auto& collAndActor : collisionAndActors) { collAndActor.first->Update( *collAndActor.second, DeltaTime ); } mPerfTimer.Log( "post collSS" ); mUpdateTimer.Log( "end collision" ); }
void CollisionSystem::Update( double DeltaTime ) { mCollisionGrid.Clear(); for( ActorList_t::iterator it = mScene.GetActors().begin(), e = mScene.GetActors().end(); it != e; ++it ) { Actor& actor = **it; Opt<ICollisionComponent> collisionC = actor.Get<ICollisionComponent>(); if ( collisionC.IsValid() ) { mCollisionGrid.AddActor( &actor, DeltaTime ); Opt<CollisionSubSystem> collisionSS = GetCollisionSubSystem( collisionC->GetId() ); if ( collisionSS.IsValid() ) { collisionSS->ClipScene( actor ); } } } PossibleCollisions_t const& PossibleCollisions = mCollisionGrid.GetPossibleCollisions(); for( PossibleCollisions_t::const_iterator i = PossibleCollisions.begin(), e = PossibleCollisions.end(); i != e; ++i ) { Actor& A = *( i->A1 ); Actor& B = *( i->A2 ); Opt<ICollisionComponent> ACollisionC = A.Get<ICollisionComponent>(); Opt<ICollisionComponent> BCollisionC = B.Get<ICollisionComponent>(); BOOST_ASSERT( ACollisionC.IsValid() && BCollisionC.IsValid() ); //TODO: here this one should be true CollisionModel const& CollModel = mCollisionStore.GetCollisionModel( ACollisionC->GetCollisionClass(), BCollisionC->GetCollisionClass() ); if( !CollModel.AreActorsColliding( A, B, DeltaTime ) ) { continue; } //TODO: needs optimization, maybe a template parameter for SubSystemHolder to subsystem would do Opt<CollisionSubSystem> ACollisionSS = GetCollisionSubSystem( ACollisionC->GetId() ); if ( ACollisionSS.IsValid() ) { ACollisionSS->Collide( A, B ); } Opt<CollisionSubSystem> BCollisionSS = GetCollisionSubSystem( BCollisionC->GetId() ); if ( BCollisionSS.IsValid() ) { BCollisionSS->Collide( B, A ); } } for( ActorList_t::iterator it = mScene.GetActors().begin(), e = mScene.GetActors().end(); it != e; ++it ) { Actor& actor = **it; Opt<ICollisionComponent> collisionC = actor.Get<ICollisionComponent>(); if ( collisionC.IsValid() ) { Opt<CollisionSubSystem> collisionSS = GetCollisionSubSystem( collisionC->GetId() ); if ( collisionSS.IsValid() ) { collisionSS->Update( actor, DeltaTime ); } } } }
bool CollisionSystem::IsColliding( Actor const& actor ) const { Opt<ICollisionComponent> collisionC = actor.Get<ICollisionComponent>(); if ( !collisionC.IsValid() ) { return false; } std::set<Actor*>&& all( mCollisionGrid.GetAllNearbyActors( &actor ) ); for( auto act : all ) { if( act == &actor ) { continue; } Opt<ICollisionComponent> ACollisionC = act->Get<ICollisionComponent>(); CollisionModel const& CollModel = mCollisionStore.GetCollisionModel( ACollisionC->GetCollisionClass(), collisionC->GetCollisionClass() ); if( CollModel.AreActorsColliding( actor, *act, 0.0 ) ) { return true; } } return false; }