Files
o3de/Gems/PhysX/Code/Source/Joint/PhysXJoint.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

197 lines
8.1 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <Joint/PhysXJoint.h>
#include <AzCore/Serialization/EditContext.h>
#include <Include/PhysX/NativeTypeIdentifiers.h>
#include <AzCore/Math/MathUtils.h>
#include <AzFramework/Physics/Common/PhysicsSimulatedBody.h>
#include <Source/Joint/PhysXJointUtils.h>
#include <PhysX/MathConversion.h>
namespace PhysX
{
AzPhysics::SimulatedBodyHandle PhysXJoint::GetParentBodyHandle() const
{
return m_parentBodyHandle;
}
AzPhysics::SimulatedBodyHandle PhysXJoint::GetChildBodyHandle() const
{
return m_childBodyHandle;
}
PhysXJoint::PhysXJoint(
AzPhysics::SceneHandle sceneHandle,
AzPhysics::SimulatedBodyHandle parentBodyHandle,
AzPhysics::SimulatedBodyHandle childBodyHandle)
: m_sceneHandle(sceneHandle)
, m_parentBodyHandle(parentBodyHandle)
, m_childBodyHandle(childBodyHandle)
{
}
bool PhysXJoint::SetPxActors()
{
physx::PxRigidActor* parentActor = Utils::GetPxRigidActor(m_sceneHandle, m_parentBodyHandle);
physx::PxRigidActor* childActor = Utils::GetPxRigidActor(m_sceneHandle, m_childBodyHandle);
if (!parentActor && !childActor)
{
AZ_Error("PhysX Joint", false, "Invalid PhysX actors in joint - at least one must be a PxRigidActor.");
return false;
}
m_pxJoint->setActors(parentActor, childActor);
return true;
}
void PhysXJoint::SetParentBody(AzPhysics::SimulatedBodyHandle parentBodyHandle)
{
auto* parentBody = Utils::GetSimulatedBodyFromHandle(m_sceneHandle, parentBodyHandle);
auto* childBody = Utils::GetSimulatedBodyFromHandle(m_sceneHandle, m_childBodyHandle);
if (Utils::IsAtLeastOneDynamic(parentBody, childBody))
{
m_parentBodyHandle = parentBodyHandle;
SetPxActors();
}
else
{
AZ_Warning("PhysX Joint", false, "Call to SetParentBody would result in invalid joint - at least one "
"body in a joint must be dynamic.");
}
}
void PhysXJoint::SetChildBody(AzPhysics::SimulatedBodyHandle childBodyHandle)
{
auto* parentBody = Utils::GetSimulatedBodyFromHandle(m_sceneHandle, m_parentBodyHandle);
auto* childBody = Utils::GetSimulatedBodyFromHandle(m_sceneHandle, childBodyHandle);
if (Utils::IsAtLeastOneDynamic(parentBody, childBody))
{
m_childBodyHandle = childBodyHandle;
SetPxActors();
}
else
{
AZ_Warning("PhysX Joint", false, "Call to SetChildBody would result in invalid joint - at least one "
"body in a joint must be dynamic.");
}
}
void* PhysXJoint::GetNativePointer() const
{
return m_pxJoint.get();
}
PhysXD6Joint::PhysXD6Joint(const D6JointLimitConfiguration& configuration,
AzPhysics::SceneHandle sceneHandle,
AzPhysics::SimulatedBodyHandle parentBodyHandle,
AzPhysics::SimulatedBodyHandle childBodyHandle)
: PhysXJoint(sceneHandle, parentBodyHandle, childBodyHandle)
{
m_pxJoint = Utils::PxJointFactories::CreatePxD6Joint(configuration, sceneHandle, parentBodyHandle, childBodyHandle);
}
PhysXFixedJoint::PhysXFixedJoint(const FixedJointConfiguration& configuration,
AzPhysics::SceneHandle sceneHandle,
AzPhysics::SimulatedBodyHandle parentBodyHandle,
AzPhysics::SimulatedBodyHandle childBodyHandle)
: PhysXJoint(sceneHandle, parentBodyHandle, childBodyHandle)
{
m_pxJoint = Utils::PxJointFactories::CreatePxFixedJoint(configuration, sceneHandle, parentBodyHandle, childBodyHandle);
}
PhysXBallJoint::PhysXBallJoint(const BallJointConfiguration& configuration,
AzPhysics::SceneHandle sceneHandle,
AzPhysics::SimulatedBodyHandle parentBodyHandle,
AzPhysics::SimulatedBodyHandle childBodyHandle)
: PhysXJoint(sceneHandle, parentBodyHandle, childBodyHandle)
{
m_pxJoint = Utils::PxJointFactories::CreatePxBallJoint(configuration, sceneHandle, parentBodyHandle, childBodyHandle);
}
PhysXHingeJoint::PhysXHingeJoint(const HingeJointConfiguration& configuration,
AzPhysics::SceneHandle sceneHandle,
AzPhysics::SimulatedBodyHandle parentBodyHandle,
AzPhysics::SimulatedBodyHandle childBodyHandle)
: PhysXJoint(sceneHandle, parentBodyHandle, childBodyHandle)
{
m_pxJoint = Utils::PxJointFactories::CreatePxHingeJoint(configuration, sceneHandle, parentBodyHandle, childBodyHandle);
}
AZ::Crc32 PhysXD6Joint::GetNativeType() const
{
return NativeTypeIdentifiers::D6Joint;
}
AZ::Crc32 PhysXFixedJoint::GetNativeType() const
{
return NativeTypeIdentifiers::FixedJoint;
}
AZ::Crc32 PhysXBallJoint::GetNativeType() const
{
return NativeTypeIdentifiers::BallJoint;
}
AZ::Crc32 PhysXHingeJoint::GetNativeType() const
{
return NativeTypeIdentifiers::HingeJoint;
}
void PhysXD6Joint::GenerateJointLimitVisualizationData(
float scale,
AZ::u32 angularSubdivisions,
AZ::u32 radialSubdivisions,
[[maybe_unused]] AZStd::vector<AZ::Vector3>& vertexBufferOut,
[[maybe_unused]] AZStd::vector<AZ::u32>& indexBufferOut,
AZStd::vector<AZ::Vector3>& lineBufferOut,
AZStd::vector<bool>& lineValidityBufferOut)
{
auto* parentBody = Utils::GetSimulatedBodyFromHandle(m_sceneHandle, m_parentBodyHandle);
auto* childBody = Utils::GetSimulatedBodyFromHandle(m_sceneHandle, m_childBodyHandle);
const AZ::u32 angularSubdivisionsClamped = AZ::GetClamp(angularSubdivisions, 4u, 32u);
const AZ::u32 radialSubdivisionsClamped = AZ::GetClamp(radialSubdivisions, 1u, 4u);
const physx::PxD6Joint* joint = static_cast<physx::PxD6Joint*>(m_pxJoint.get());
const AZ::Quaternion parentLocalRotation = PxMathConvert(joint->getLocalPose(physx::PxJointActorIndex::eACTOR0).q);
const AZ::Quaternion parentWorldRotation = parentBody ? parentBody->GetOrientation() : AZ::Quaternion::CreateIdentity();
const AZ::Quaternion childLocalRotation = PxMathConvert(joint->getLocalPose(physx::PxJointActorIndex::eACTOR1).q);
const AZ::Quaternion childWorldRotation = childBody ? childBody->GetOrientation() : AZ::Quaternion::CreateIdentity();
const float swingAngleY = joint->getSwingYAngle();
const float swingAngleZ = joint->getSwingZAngle();
const float swingLimitY = joint->getSwingLimit().yAngle;
const float swingLimitZ = joint->getSwingLimit().zAngle;
const float twistAngle = joint->getTwist();
const float twistLimitLower = joint->getTwistLimit().lower;
const float twistLimitUpper = joint->getTwistLimit().upper;
Utils::Joints::AppendD6SwingConeToLineBuffer(
parentLocalRotation, swingAngleY, swingAngleZ, swingLimitY, swingLimitZ,
scale, angularSubdivisionsClamped, radialSubdivisionsClamped, lineBufferOut, lineValidityBufferOut);
Utils::Joints::AppendD6TwistArcToLineBuffer(
parentLocalRotation, twistAngle, twistLimitLower, twistLimitUpper,
scale, angularSubdivisionsClamped, radialSubdivisionsClamped, lineBufferOut, lineValidityBufferOut);
Utils::Joints::AppendD6CurrentTwistToLineBuffer(
parentLocalRotation, twistAngle, twistLimitLower, twistLimitUpper,
scale, lineBufferOut, lineValidityBufferOut);
// draw the X-axis of the child joint frame
// make the axis slightly longer than the radius of the twist arc so that it is easy to see
float axisLength = 1.25f * scale;
AZ::Vector3 childAxis = (parentWorldRotation.GetConjugate() * childWorldRotation * childLocalRotation).TransformVector(
AZ::Vector3::CreateAxisX(axisLength));
lineBufferOut.push_back(AZ::Vector3::CreateZero());
lineBufferOut.push_back(childAxis);
}
} // namespace PhysX