BallJointComponent.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <Source/BallJointComponent.h>
  9. #include <PhysX/MathConversion.h>
  10. #include <PhysX/PhysXLocks.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzCore/Interface/Interface.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzFramework/Physics/SimulatedBodies/RigidBody.h>
  15. #include <AzFramework/Physics/RigidBodyBus.h>
  16. #include <AzFramework/Physics/PhysicsScene.h>
  17. #include <PxPhysicsAPI.h>
  18. namespace PhysX
  19. {
  20. void BallJointComponent::Reflect(AZ::ReflectContext* context)
  21. {
  22. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->Class<BallJointComponent, JointComponent>()
  25. ->Version(2)
  26. ;
  27. }
  28. }
  29. BallJointComponent::BallJointComponent(
  30. const JointComponentConfiguration& configuration,
  31. const JointGenericProperties& genericProperties,
  32. const JointLimitProperties& limitProperties)
  33. : JointComponent(configuration, genericProperties, limitProperties)
  34. {
  35. }
  36. void BallJointComponent::InitNativeJoint()
  37. {
  38. if (m_jointHandle != AzPhysics::InvalidJointHandle)
  39. {
  40. return;
  41. }
  42. JointComponent::LeadFollowerInfo leadFollowerInfo;
  43. ObtainLeadFollowerInfo(leadFollowerInfo);
  44. if (leadFollowerInfo.m_followerActor == nullptr ||
  45. leadFollowerInfo.m_followerBody == nullptr)
  46. {
  47. return;
  48. }
  49. // if there is no lead body, this will be a constraint of the follower's global position, so use invalid body handle.
  50. AzPhysics::SimulatedBodyHandle parentHandle = AzPhysics::InvalidSimulatedBodyHandle;
  51. if (leadFollowerInfo.m_leadBody != nullptr)
  52. {
  53. parentHandle = leadFollowerInfo.m_leadBody->m_bodyHandle;
  54. }
  55. else
  56. {
  57. AZ_TracePrintf(
  58. "PhysX", "Entity [%s] Ball Joint component missing lead entity. This joint will be a global constraint on the follower's global position.",
  59. GetEntity()->GetName().c_str());
  60. }
  61. BallJointConfiguration configuration;
  62. configuration.m_parentLocalPosition = leadFollowerInfo.m_leadLocal.GetTranslation();
  63. configuration.m_parentLocalRotation = leadFollowerInfo.m_leadLocal.GetRotation();
  64. configuration.m_childLocalPosition = leadFollowerInfo.m_followerLocal.GetTranslation();
  65. configuration.m_childLocalRotation = leadFollowerInfo.m_followerLocal.GetRotation();
  66. configuration.m_genericProperties = m_genericProperties;
  67. configuration.m_limitProperties = m_limits;
  68. if (auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get())
  69. {
  70. m_jointHandle = sceneInterface->AddJoint(
  71. leadFollowerInfo.m_followerBody->m_sceneOwner,
  72. &configuration,
  73. parentHandle,
  74. leadFollowerInfo.m_followerBody->m_bodyHandle);
  75. m_jointSceneOwner = leadFollowerInfo.m_followerBody->m_sceneOwner;
  76. }
  77. }
  78. } // namespace PhysX