PhysicsBasedSource.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. namespace ROS2
  13. {
  14. void PhysicsBasedSource::Reflect(AZ::ReflectContext* context)
  15. {
  16. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  17. {
  18. serializeContext->Class<PhysicsBasedSource>()->Version(1);
  19. }
  20. }
  21. void PhysicsBasedSource::Start()
  22. {
  23. m_onSceneSimulationEventHandler.Disconnect();
  24. const auto* ros2Interface = ROS2Interface::Get();
  25. AZ_Assert(ros2Interface, "ROS2 interface is not initialized.");
  26. m_onSceneSimulationEventHandler = AzPhysics::SceneEvents::OnSceneSimulationFinishHandler(
  27. [this, ros2Interface](AzPhysics::SceneHandle sceneHandle, float deltaTime)
  28. {
  29. const auto simulationTime = ros2Interface->GetROSTimestamp();
  30. const float deltaSimulationTime = ROS2Conversions::GetTimeDifference(m_lastSimulationTime, simulationTime);
  31. m_sourceEvent.Signal(sceneHandle, deltaSimulationTime);
  32. m_lastSimulationTime = simulationTime;
  33. });
  34. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  35. AzPhysics::SceneHandle sceneHandle = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
  36. sceneInterface->RegisterSceneSimulationFinishHandler(sceneHandle, m_onSceneSimulationEventHandler);
  37. }
  38. void PhysicsBasedSource::Stop()
  39. {
  40. m_onSceneSimulationEventHandler.Disconnect();
  41. }
  42. float PhysicsBasedSource::GetDeltaTime([[maybe_unused]] AzPhysics::SceneHandle sceneHandle, float deltaTime) const
  43. {
  44. return deltaTime;
  45. }
  46. } // namespace ROS2