2
0

WheelControllerComponent.cpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "WheelControllerComponent.h"
  9. #include <AzCore/Debug/Trace.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/EditContextConstants.inl>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <Source/RigidBodyComponent.h>
  14. namespace ROS2Controllers::VehicleDynamics
  15. {
  16. void WheelControllerComponent::Activate()
  17. {
  18. m_rigidBodyPtr = nullptr;
  19. }
  20. void WheelControllerComponent::Deactivate()
  21. {
  22. }
  23. void WheelControllerComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  24. {
  25. required.push_back(AZ_CRC_CE("PhysicsRigidBodyService"));
  26. }
  27. void WheelControllerComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  28. {
  29. provided.push_back(AZ_CRC_CE("WheelControllerService"));
  30. }
  31. void WheelControllerComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  32. {
  33. // Only one per entity
  34. incompatible.push_back(AZ_CRC_CE("WheelControllerService"));
  35. }
  36. AZ::Vector3 WheelControllerComponent::GetRotationVelocity()
  37. {
  38. if (m_rigidBodyPtr == nullptr)
  39. {
  40. Physics::RigidBodyRequestBus::EventResult(m_rigidBodyPtr, m_entity->GetId(), &Physics::RigidBodyRequests::GetRigidBody);
  41. }
  42. AZ_Assert(m_rigidBodyPtr, "No Rigid Body in the WheelController entity!");
  43. if (m_rigidBodyPtr)
  44. {
  45. const auto transform = m_rigidBodyPtr->GetTransform().GetInverse();
  46. const auto local = transform.TransformVector(m_rigidBodyPtr->GetAngularVelocity());
  47. return local;
  48. }
  49. return AZ::Vector3::CreateZero();
  50. }
  51. void WheelControllerComponent::Reflect(AZ::ReflectContext* context)
  52. {
  53. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  54. {
  55. serialize->Class<WheelControllerComponent, AZ::Component>()
  56. ->Version(3)
  57. ->Field("SteeringEntity", &WheelControllerComponent::m_steeringEntity)
  58. ->Field("SteeringScale", &WheelControllerComponent::m_steeringScale);
  59. if (AZ::EditContext* ec = serialize->GetEditContext())
  60. {
  61. ec->Class<WheelControllerComponent>("Wheel Controller", "Handle wheel physics")
  62. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  63. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  64. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  65. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/WheelController.svg")
  66. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/WheelController.svg")
  67. ->DataElement(
  68. AZ::Edit::UIHandlers::Default,
  69. &WheelControllerComponent::m_steeringEntity,
  70. "Steering entity",
  71. "Entity which steers the wheel - typically a parent entity")
  72. ->DataElement(
  73. AZ::Edit::UIHandlers::Default,
  74. &WheelControllerComponent::m_steeringScale,
  75. "Scale of steering axis",
  76. "The direction of torque applied to the steering entity");
  77. }
  78. }
  79. }
  80. WheelControllerComponent::WheelControllerComponent(const AZ::EntityId& steeringEntity, const float steeringScale)
  81. : m_steeringEntity(steeringEntity)
  82. , m_steeringScale(steeringScale)
  83. {
  84. }
  85. } // namespace ROS2Controllers::VehicleDynamics