PhysXJointTestComponent.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "PhysXJointTestComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzFramework/Components/TransformComponent.h>
  12. #include <HingeJointComponent.h>
  13. #include <PrismaticJointComponent.h>
  14. #include <PhysX/Joint/PhysXJointRequestsBus.h>
  15. #include <imgui/imgui.h>
  16. namespace TestScene
  17. {
  18. void ImGuiJointDemo::Activate()
  19. {
  20. ImGui::ImGuiUpdateListenerBus::Handler::BusConnect();
  21. }
  22. void ImGuiJointDemo::Deactivate()
  23. {
  24. ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect();
  25. }
  26. void ImGuiJointDemo::Reflect(AZ::ReflectContext* context)
  27. {
  28. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  29. {
  30. serialize->Class<ImGuiJointDemo, AZ::Component>()
  31. ->Version(3);
  32. if (AZ::EditContext* ec = serialize->GetEditContext())
  33. {
  34. ec->Class<ImGuiJointDemo>("ImGuiJointDemo", "ImGuiJointDemo")
  35. ->ClassElement(AZ::Edit::ClassElements::EditorData, "ImGuiJointDemo")
  36. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  37. ->Attribute(AZ::Edit::Attributes::Category, "TestScene");
  38. }
  39. }
  40. }
  41. void ImGuiJointDemo::OnImGuiUpdate()
  42. {
  43. float velocity{0};
  44. float position{0};
  45. AZStd::pair<float, float> limits{0,0};
  46. auto* component1 = GetEntity()->FindComponent<PhysX::HingeJointComponent>();
  47. auto* component2 = GetEntity()->FindComponent<PhysX::PrismaticJointComponent>();
  48. auto componentId (AZ::InvalidComponentId);
  49. if (component1){
  50. componentId = component1->GetId();
  51. }
  52. if (component2){
  53. componentId = component2->GetId();
  54. }
  55. if (componentId != AZ::InvalidComponentId) {
  56. const AZ::EntityComponentIdPair id(GetEntityId(), componentId);
  57. PhysX::JointRequestBus::EventResult(velocity, id,
  58. &PhysX::JointRequests::GetVelocity);
  59. PhysX::JointRequestBus::EventResult(position, id,
  60. &PhysX::JointRequests::GetPosition);
  61. PhysX::JointRequestBus::EventResult(limits, id,
  62. &PhysX::JointRequests::GetLimits);
  63. ImGui::Begin("SimplePhysXJointDemo");
  64. AZStd::string group_name = "Joint " + GetEntity()->GetName();
  65. ImGui::Text("%s", GetEntity()->GetName().c_str());
  66. ImGui::SliderFloat(
  67. AZStd::string("Position##" + GetEntity()->GetName()).c_str(),
  68. &position, limits.first, limits.second);
  69. ImGui::SliderFloat(
  70. AZStd::string("Velocity##" + GetEntity()->GetName()).c_str(),
  71. &velocity, -0.5f, 0.5f);
  72. ImGui::InputFloat(
  73. AZStd::string("Force##" + GetEntity()->GetName()).c_str(),
  74. &m_forceSet);
  75. ImGui::SliderFloat(
  76. AZStd::string("Speed##" + GetEntity()->GetName()).c_str(),
  77. &m_velocitySet, -1.5f, 1.5f);
  78. float spacing = ImGui::GetStyle().ItemInnerSpacing.x;
  79. ImGui::PushButtonRepeat(true);
  80. float commandVel = 0;
  81. if (ImGui::ArrowButton(
  82. AZStd::string("##left" + GetEntity()->GetName()).c_str(),
  83. ImGuiDir_Left)) {
  84. commandVel = -m_velocitySet;
  85. }
  86. ImGui::SameLine(0.0f, spacing);
  87. if (ImGui::ArrowButton(
  88. AZStd::string("##right" + GetEntity()->GetName()).c_str(),
  89. ImGuiDir_Right)) {
  90. commandVel = m_velocitySet;
  91. }
  92. ImGui::Text("----------------------------------------------------------");
  93. PhysX::JointRequestBus::Event(id, &PhysX::JointRequests::SetVelocity,
  94. commandVel);
  95. PhysX::JointRequestBus::Event(id, &PhysX::JointRequests::SetMaximumForce,
  96. m_forceSet);
  97. }
  98. ImGui::End();
  99. }
  100. }