Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/HingeConstraint.h
9912 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/// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)126void SetLimits(float inLimitsMin, float inLimitsMax);127float GetLimitsMin() const { return mLimitsMin; }128float GetLimitsMax() const { return mLimitsMax; }129bool HasLimits() const { return mHasLimits; }130131/// Update the limits spring settings132const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }133SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }134void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }135136///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)137inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }138inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }139inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }140inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }141142private:143// Internal helper function to calculate the values below144void CalculateA1AndTheta();145void CalculateRotationLimitsConstraintProperties(float inDeltaTime);146void CalculateMotorConstraintProperties(float inDeltaTime);147inline float GetSmallestAngleToLimit() const;148inline bool IsMinLimitClosest() const;149150// CONFIGURATION PROPERTIES FOLLOW151152// Local space constraint positions153Vec3 mLocalSpacePosition1;154Vec3 mLocalSpacePosition2;155156// Local space hinge directions157Vec3 mLocalSpaceHingeAxis1;158Vec3 mLocalSpaceHingeAxis2;159160// Local space normal direction (direction relative to which to draw constraint limits)161Vec3 mLocalSpaceNormalAxis1;162Vec3 mLocalSpaceNormalAxis2;163164// Inverse of initial relative orientation between bodies (which defines hinge angle = 0)165Quat mInvInitialOrientation;166167// Hinge limits168bool mHasLimits;169float mLimitsMin;170float mLimitsMax;171172// Soft constraint limits173SpringSettings mLimitsSpringSettings;174175// Friction176float mMaxFrictionTorque;177178// Motor controls179MotorSettings mMotorSettings;180EMotorState mMotorState = EMotorState::Off;181float mTargetAngularVelocity = 0.0f;182float mTargetAngle = 0.0f;183184// RUN TIME PROPERTIES FOLLOW185186// Current rotation around the hinge axis187float mTheta = 0.0f;188189// World space hinge axis for body 1190Vec3 mA1;191192// The constraint parts193PointConstraintPart mPointConstraintPart;194HingeRotationConstraintPart mRotationConstraintPart;195AngleConstraintPart mRotationLimitsConstraintPart;196AngleConstraintPart mMotorConstraintPart;197};198199JPH_NAMESPACE_END200201202