Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/ConeConstraint.h
9913 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/ConstraintPart/PointConstraintPart.h>8#include <Jolt/Physics/Constraints/ConstraintPart/AngleConstraintPart.h>910JPH_NAMESPACE_BEGIN1112/// Cone constraint settings, used to create a cone constraint13class JPH_EXPORT ConeConstraintSettings final : public TwoBodyConstraintSettings14{15JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, ConeConstraintSettings)1617public:18// See: ConstraintSettings::SaveBinaryState19virtual void SaveBinaryState(StreamOut &inStream) const override;2021/// Create an instance of this constraint22virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;2324/// This determines in which space the constraint is setup, all properties below should be in the specified space25EConstraintSpace mSpace = EConstraintSpace::WorldSpace;2627/// Body 1 constraint reference frame (space determined by mSpace)28RVec3 mPoint1 = RVec3::sZero();29Vec3 mTwistAxis1 = Vec3::sAxisX();3031/// Body 2 constraint reference frame (space determined by mSpace)32RVec3 mPoint2 = RVec3::sZero();33Vec3 mTwistAxis2 = Vec3::sAxisX();3435/// Half of maximum angle between twist axis of body 1 and 236float mHalfConeAngle = 0.0f;3738protected:39// See: ConstraintSettings::RestoreBinaryState40virtual void RestoreBinaryState(StreamIn &inStream) override;41};4243/// A cone constraint constraints 2 bodies to a single point and limits the swing between the twist axis within a cone:44///45/// t1 . t2 <= cos(theta)46///47/// Where:48///49/// t1 = twist axis of body 1.50/// t2 = twist axis of body 2.51/// theta = half cone angle (angle from the principal axis of the cone to the edge).52///53/// Calculating the Jacobian:54///55/// Constraint equation:56///57/// C = t1 . t2 - cos(theta)58///59/// Derivative:60///61/// d/dt C = d/dt (t1 . t2) = (d/dt t1) . t2 + t1 . (d/dt t2) = (w1 x t1) . t2 + t1 . (w2 x t2) = (t1 x t2) . w1 + (t2 x t1) . w262///63/// d/dt C = J v = [0, -t2 x t1, 0, t2 x t1] [v1, w1, v2, w2]64///65/// Where J is the Jacobian.66///67/// Note that this is the exact same equation as used in AngleConstraintPart if we use t2 x t1 as the world space axis68class JPH_EXPORT ConeConstraint final : public TwoBodyConstraint69{70public:71JPH_OVERRIDE_NEW_DELETE7273/// Construct cone constraint74ConeConstraint(Body &inBody1, Body &inBody2, const ConeConstraintSettings &inSettings);7576// Generic interface of a constraint77virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Cone; }78virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;79virtual void SetupVelocityConstraint(float inDeltaTime) override;80virtual void ResetWarmStart() override;81virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;82virtual bool SolveVelocityConstraint(float inDeltaTime) override;83virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;84#ifdef JPH_DEBUG_RENDERER85virtual void DrawConstraint(DebugRenderer *inRenderer) const override;86virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;87#endif // JPH_DEBUG_RENDERER88virtual void SaveState(StateRecorder &inStream) const override;89virtual void RestoreState(StateRecorder &inStream) override;90virtual Ref<ConstraintSettings> GetConstraintSettings() const override;9192// See: TwoBodyConstraint93virtual Mat44 GetConstraintToBody1Matrix() const override;94virtual Mat44 GetConstraintToBody2Matrix() const override;9596/// Update maximum angle between body 1 and 2 (see ConeConstraintSettings)97void SetHalfConeAngle(float inHalfConeAngle) { JPH_ASSERT(inHalfConeAngle >= 0.0f && inHalfConeAngle <= JPH_PI); mCosHalfConeAngle = Cos(inHalfConeAngle); }98float GetCosHalfConeAngle() const { return mCosHalfConeAngle; }99100///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)101inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }102inline float GetTotalLambdaRotation() const { return mAngleConstraintPart.GetTotalLambda(); }103104private:105// Internal helper function to calculate the values below106void CalculateRotationConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);107108// CONFIGURATION PROPERTIES FOLLOW109110// Local space constraint positions111Vec3 mLocalSpacePosition1;112Vec3 mLocalSpacePosition2;113114// Local space constraint axis115Vec3 mLocalSpaceTwistAxis1;116Vec3 mLocalSpaceTwistAxis2;117118// Angular limits119float mCosHalfConeAngle;120121// RUN TIME PROPERTIES FOLLOW122123// Axis and angle of rotation between the two bodies124Vec3 mWorldSpaceRotationAxis;125float mCosTheta;126127// The constraint parts128PointConstraintPart mPointConstraintPart;129AngleConstraintPart mAngleConstraintPart;130};131132JPH_NAMESPACE_END133134135