Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/HingeConstraint.h
22173 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>7#include <Jolt/Physics/Constraints/MotorSettings.h>8#include <Jolt/Physics/Constraints/ConstraintPart/PointConstraintPart.h>9#include <Jolt/Physics/Constraints/ConstraintPart/HingeRotationConstraintPart.h>10#include <Jolt/Physics/Constraints/ConstraintPart/AngleConstraintPart.h>1112JPH_NAMESPACE_BEGIN1314/// Hinge constraint settings, used to create a hinge constraint15class JPH_EXPORT HingeConstraintSettings final : public TwoBodyConstraintSettings16{17JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, HingeConstraintSettings)1819public:20// See: ConstraintSettings::SaveBinaryState21virtual void SaveBinaryState(StreamOut &inStream) const override;2223/// Create an instance of this constraint24virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;2526/// This determines in which space the constraint is setup, all properties below should be in the specified space27EConstraintSpace mSpace = EConstraintSpace::WorldSpace;2829/// Body 1 constraint reference frame (space determined by mSpace).30/// Hinge axis is the axis where rotation is allowed.31/// When the normal axis of both bodies align in world space, the hinge angle is defined to be 0.32/// mHingeAxis1 and mNormalAxis1 should be perpendicular. mHingeAxis2 and mNormalAxis2 should also be perpendicular.33/// If you configure the joint in world space and create both bodies with a relative rotation you want to be defined as zero,34/// you can simply set mHingeAxis1 = mHingeAxis2 and mNormalAxis1 = mNormalAxis2.35RVec3 mPoint1 = RVec3::sZero();36Vec3 mHingeAxis1 = Vec3::sAxisY();37Vec3 mNormalAxis1 = Vec3::sAxisX();3839/// Body 2 constraint reference frame (space determined by mSpace)40RVec3 mPoint2 = RVec3::sZero();41Vec3 mHingeAxis2 = Vec3::sAxisY();42Vec3 mNormalAxis2 = Vec3::sAxisX();4344/// Rotation around the hinge axis will be limited between [mLimitsMin, mLimitsMax] where mLimitsMin e [-pi, 0] and mLimitsMax e [0, pi].45/// Both angles are in radians.46float mLimitsMin = -JPH_PI;47float mLimitsMax = JPH_PI;4849/// When enabled, this makes the limits soft. When the constraint exceeds the limits, a spring force will pull it back.50SpringSettings mLimitsSpringSettings;5152/// Maximum amount of torque (N m) to apply as friction when the constraint is not powered by a motor53float mMaxFrictionTorque = 0.0f;5455/// In case the constraint is powered, this determines the motor settings around the hinge axis56MotorSettings mMotorSettings;5758protected:59// See: ConstraintSettings::RestoreBinaryState60virtual void RestoreBinaryState(StreamIn &inStream) override;61};6263/// A hinge constraint constrains 2 bodies on a single point and allows only a single axis of rotation64class JPH_EXPORT HingeConstraint final : public TwoBodyConstraint65{66public:67JPH_OVERRIDE_NEW_DELETE6869/// Construct hinge constraint70HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);7172// Generic interface of a constraint73virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Hinge; }74virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;75virtual void SetupVelocityConstraint(float inDeltaTime) override;76virtual void ResetWarmStart() override;77virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;78virtual bool SolveVelocityConstraint(float inDeltaTime) override;79virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;80#ifdef JPH_DEBUG_RENDERER81virtual void DrawConstraint(DebugRenderer *inRenderer) const override;82virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;83#endif // JPH_DEBUG_RENDERER84virtual void SaveState(StateRecorder &inStream) const override;85virtual void RestoreState(StateRecorder &inStream) override;86virtual Ref<ConstraintSettings> GetConstraintSettings() const override;8788// See: TwoBodyConstraint89virtual Mat44 GetConstraintToBody1Matrix() const override;90virtual Mat44 GetConstraintToBody2Matrix() const override;9192/// Get the attachment point for body 1 relative to body 1 COM (transform by Body::GetCenterOfMassTransform to take to world space)93inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }9495/// Get the attachment point for body 2 relative to body 2 COM (transform by Body::GetCenterOfMassTransform to take to world space)96inline Vec3 GetLocalSpacePoint2() const { return mLocalSpacePosition2; }9798// Local space hinge directions (transform direction by Body::GetCenterOfMassTransform to take to world space)99Vec3 GetLocalSpaceHingeAxis1() const { return mLocalSpaceHingeAxis1; }100Vec3 GetLocalSpaceHingeAxis2() const { return mLocalSpaceHingeAxis2; }101102// Local space normal directions (transform direction by Body::GetCenterOfMassTransform to take to world space)103Vec3 GetLocalSpaceNormalAxis1() const { return mLocalSpaceNormalAxis1; }104Vec3 GetLocalSpaceNormalAxis2() const { return mLocalSpaceNormalAxis2; }105106/// Get the current rotation angle from the rest position107float GetCurrentAngle() const;108109// Friction control110void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }111float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }112113// Motor settings114MotorSettings & GetMotorSettings() { return mMotorSettings; }115const MotorSettings & GetMotorSettings() const { return mMotorSettings; }116117// Motor controls118void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }119EMotorState GetMotorState() const { return mMotorState; }120void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; } ///< rad/s121float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }122void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; } ///< rad123float GetTargetAngle() const { return mTargetAngle; }124125/// Set the target orientation in body space (R2 = R1 * inOrientation, where R1 and R2 are the world space rotations for body 1 and 2).126/// Calculates the local space target angle and calls SetTargetAngle. Motor state must be EMotorState::Position for this to have any effect.127/// May set the wrong angle if inOrientation contains large rotations around other axis than the hinge axis.128void SetTargetOrientationBS(QuatArg inOrientation);129130/// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)131void SetLimits(float inLimitsMin, float inLimitsMax);132float GetLimitsMin() const { return mLimitsMin; }133float GetLimitsMax() const { return mLimitsMax; }134bool HasLimits() const { return mHasLimits; }135136/// Update the limits spring settings137const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }138SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }139void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }140141///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)142inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }143inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }144inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }145inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }146147private:148// Internal helper function to calculate the values below149void CalculateA1AndTheta();150void CalculateRotationLimitsConstraintProperties(float inDeltaTime);151void CalculateMotorConstraintProperties(float inDeltaTime);152inline float GetSmallestAngleToLimit() const;153inline bool IsMinLimitClosest() const;154155// CONFIGURATION PROPERTIES FOLLOW156157// Local space constraint positions158Vec3 mLocalSpacePosition1;159Vec3 mLocalSpacePosition2;160161// Local space hinge directions162Vec3 mLocalSpaceHingeAxis1;163Vec3 mLocalSpaceHingeAxis2;164165// Local space normal direction (direction relative to which to draw constraint limits)166Vec3 mLocalSpaceNormalAxis1;167Vec3 mLocalSpaceNormalAxis2;168169// Inverse of initial relative orientation between bodies (which defines hinge angle = 0)170Quat mInvInitialOrientation;171172// Hinge limits173bool mHasLimits;174float mLimitsMin;175float mLimitsMax;176177// Soft constraint limits178SpringSettings mLimitsSpringSettings;179180// Friction181float mMaxFrictionTorque;182183// Motor controls184MotorSettings mMotorSettings;185EMotorState mMotorState = EMotorState::Off;186float mTargetAngularVelocity = 0.0f;187float mTargetAngle = 0.0f;188189// RUN TIME PROPERTIES FOLLOW190191// Current rotation around the hinge axis192float mTheta = 0.0f;193194// World space hinge axis for body 1195Vec3 mA1;196197// The constraint parts198PointConstraintPart mPointConstraintPart;199HingeRotationConstraintPart mRotationConstraintPart;200AngleConstraintPart mRotationLimitsConstraintPart;201AngleConstraintPart mMotorConstraintPart;202};203204JPH_NAMESPACE_END205206207