lullaby/tests/physics_system_test.cc
// Test contact events. Use a Static and Kinematic trigger for simplicity.
Test contact events. Use a Static and Kinematic trigger for simplicity.
Test contact events . Use a Static and Kinematic trigger for simplicity .
TEST_F(PhysicsSystemTest, ContactEvents) {
  auto* dispatcher_system = registry_->Get<DispatcherSystem>();
  auto* physics_system = registry_->Get<PhysicsSystem>();
  auto* transform_system = registry_->Get<TransformSystem>();
  const Entity origin = CreateBasicRigidBody(
      mathfu::kZeros3f, RigidBodyType::RigidBodyType_Static,
      ColliderType::ColliderType_Trigger);
  EXPECT_THAT(origin, Ne(kNullEntity));
  int origin_contacts = 0;
  dispatcher_system->Connect(
      origin, this, [&origin_contacts](const EnterPhysicsContactEvent& e) {
        ++origin_contacts;
      });
  dispatcher_system->Connect(
      origin, this, [&origin_contacts](const ExitPhysicsContactEvent& e) {
        --origin_contacts;
      });
  const Entity kinematic = CreateBasicRigidBody(
      mathfu::vec3(0.f, 3.f, 0.f), RigidBodyType::RigidBodyType_Kinematic,
      ColliderType::ColliderType_Trigger);
  EXPECT_THAT(kinematic, Ne(kNullEntity));
  int kinematic_contacts = 0;
  dispatcher_system->Connect(
      kinematic, this, [&kinematic_contacts]
      (const EnterPhysicsContactEvent& e) {
        ++kinematic_contacts;
      });
  dispatcher_system->Connect(
      kinematic, this, [&kinematic_contacts]
      (const ExitPhysicsContactEvent& e) {
        --kinematic_contacts;
      });
  physics_system->AdvanceFrame(kFrameDuration);
  EXPECT_FALSE(physics_system->AreInContact(origin, kinematic));
  EXPECT_FALSE(physics_system->AreInContact(kinematic, origin));
  EXPECT_THAT(origin_contacts, Eq(0));
  EXPECT_THAT(kinematic_contacts, Eq(0));
  transform_system->SetLocalTranslation(
      kinematic, mathfu::vec3(0.f, 1.5f, 0.f));
  physics_system->AdvanceFrame(kFrameDuration);
  EXPECT_TRUE(physics_system->AreInContact(origin, kinematic));
  EXPECT_TRUE(physics_system->AreInContact(kinematic, origin));
  EXPECT_THAT(origin_contacts, Eq(1));
  EXPECT_THAT(kinematic_contacts, Eq(1));
  physics_system->AdvanceFrame(kFrameDuration);
  EXPECT_TRUE(physics_system->AreInContact(origin, kinematic));
  EXPECT_TRUE(physics_system->AreInContact(kinematic, origin));
  EXPECT_THAT(origin_contacts, Eq(1));
  EXPECT_THAT(kinematic_contacts, Eq(1));
  transform_system->SetLocalTranslation(kinematic, mathfu::vec3(0.f, 3.f, 0.f));
  physics_system->AdvanceFrame(kFrameDuration);
  EXPECT_FALSE(physics_system->AreInContact(origin, kinematic));
  EXPECT_FALSE(physics_system->AreInContact(kinematic, origin));
  EXPECT_THAT(origin_contacts, Eq(0));
  EXPECT_THAT(kinematic_contacts, Eq(0));
}
TEST_F ( PhysicsSystemTest , ContactEvents ) { auto * dispatcher_system = registry_ -> Get < DispatcherSystem > ( ) ; auto * physics_system = registry_ -> Get < PhysicsSystem > ( ) ; auto * transform_system = registry_ -> Get < TransformSystem > ( ) ; const Entity origin = CreateBasicRigidBody ( mathfu :: kZeros3f , RigidBodyType :: RigidBodyType_Static , ColliderType :: ColliderType_Trigger ) ; EXPECT_THAT ( origin , Ne ( kNullEntity ) ) ; int origin_contacts = 0 ; dispatcher_system -> Connect ( origin , this , [ & origin_contacts ] ( const EnterPhysicsContactEvent & e ) { ++ origin_contacts ; } ) ; dispatcher_system -> Connect ( origin , this , [ & origin_contacts ] ( const ExitPhysicsContactEvent & e ) { -- origin_contacts ; } ) ; const Entity kinematic = CreateBasicRigidBody ( mathfu :: vec3 ( 0.f , 3.f , 0.f ) , RigidBodyType :: RigidBodyType_Kinematic , ColliderType :: ColliderType_Trigger ) ; EXPECT_THAT ( kinematic , Ne ( kNullEntity ) ) ; int kinematic_contacts = 0 ; dispatcher_system -> Connect ( kinematic , this , [ & kinematic_contacts ] ( const EnterPhysicsContactEvent & e ) { ++ kinematic_contacts ; } ) ; dispatcher_system -> Connect ( kinematic , this , [ & kinematic_contacts ] ( const ExitPhysicsContactEvent & e ) { -- kinematic_contacts ; } ) ; physics_system -> AdvanceFrame ( kFrameDuration ) ; EXPECT_FALSE ( physics_system -> AreInContact ( origin , kinematic ) ) ; EXPECT_FALSE ( physics_system -> AreInContact ( kinematic , origin ) ) ; EXPECT_THAT ( origin_contacts , Eq ( 0 ) ) ; EXPECT_THAT ( kinematic_contacts , Eq ( 0 ) ) ; transform_system -> SetLocalTranslation ( kinematic , mathfu :: vec3 ( 0.f , 1.5f , 0.f ) ) ; physics_system -> AdvanceFrame ( kFrameDuration ) ; EXPECT_TRUE ( physics_system -> AreInContact ( origin , kinematic ) ) ; EXPECT_TRUE ( physics_system -> AreInContact ( kinematic , origin ) ) ; EXPECT_THAT ( origin_contacts , Eq ( 1 ) ) ; EXPECT_THAT ( kinematic_contacts , Eq ( 1 ) ) ; physics_system -> AdvanceFrame ( kFrameDuration ) ; EXPECT_TRUE ( physics_system -> AreInContact ( origin , kinematic ) ) ; EXPECT_TRUE ( physics_system -> AreInContact ( kinematic , origin ) ) ; EXPECT_THAT ( origin_contacts , Eq ( 1 ) ) ; EXPECT_THAT ( kinematic_contacts , Eq ( 1 ) ) ; transform_system -> SetLocalTranslation ( kinematic , mathfu :: vec3 ( 0.f , 3.f , 0.f ) ) ; physics_system -> AdvanceFrame ( kFrameDuration ) ; EXPECT_FALSE ( physics_system -> AreInContact ( origin , kinematic ) ) ; EXPECT_FALSE ( physics_system -> AreInContact ( kinematic , origin ) ) ; EXPECT_THAT ( origin_contacts , Eq ( 0 ) ) ; EXPECT_THAT ( kinematic_contacts , Eq ( 0 ) ) ; }
Test contact events.
Test contact events .
// Create a Static Trigger box at the origin. // Create a 2x2x2 Kinematic Trigger box above the origin and out of contact // with the first Entity. // Advance the PhysicsSystem and confirm they are not in contact. Ensure order // doesn't matter. // Move the kinematic object down, then re-do contact tests. // Advancing the PhysicsSystem again changes nothing. // Move the kinematic object back up, then re-do contact tests.