PhysicsBasedSource.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Serialization/SerializeContext.h>
  9. #include <AzFramework/Physics/PhysicsSystem.h>
  10. #include <ROS2/Sensor/Events/PhysicsBasedSource.h>
  11. #include <ROS2/Utilities/ROS2Conversions.h>
  12. #include <ROS2/Clock/ROS2ClockRequestBus.h>
  13. namespace ROS2
  14. {
  15. void PhysicsBasedSource::Reflect(AZ::ReflectContext* context)
  16. {
  17. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serializeContext->Class<PhysicsBasedSource>()->Version(1);
  20. }
  21. }
  22. void PhysicsBasedSource::Start()
  23. {
  24. m_onSceneSimulationEventHandler.Disconnect();
  25. m_onSceneSimulationEventHandler = AzPhysics::SceneEvents::OnSceneSimulationFinishHandler(
  26. [this](AzPhysics::SceneHandle sceneHandle, float deltaTime)
  27. {
  28. const auto simulationTime = ROS2ClockInterface::Get()->GetROSTimestamp();
  29. const float deltaSimulationTime = ROS2Conversions::GetTimeDifference(m_lastSimulationTime, simulationTime);
  30. m_sourceEvent.Signal(sceneHandle, deltaSimulationTime);
  31. m_lastSimulationTime = simulationTime;
  32. });
  33. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  34. AzPhysics::SceneHandle sceneHandle = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
  35. sceneInterface->RegisterSceneSimulationFinishHandler(sceneHandle, m_onSceneSimulationEventHandler);
  36. }
  37. void PhysicsBasedSource::Stop()
  38. {
  39. m_onSceneSimulationEventHandler.Disconnect();
  40. }
  41. float PhysicsBasedSource::GetDeltaTime([[maybe_unused]] AzPhysics::SceneHandle sceneHandle, float deltaTime) const
  42. {
  43. return deltaTime;
  44. }
  45. } // namespace ROS2