Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/PointConstraint.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>89JPH_NAMESPACE_BEGIN1011/// Point constraint settings, used to create a point constraint12class JPH_EXPORT PointConstraintSettings final : public TwoBodyConstraintSettings13{14JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PointConstraintSettings)1516public:17// See: ConstraintSettings::SaveBinaryState18virtual void SaveBinaryState(StreamOut &inStream) const override;1920/// Create an instance of this constraint21virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;2223/// This determines in which space the constraint is setup, all properties below should be in the specified space24EConstraintSpace mSpace = EConstraintSpace::WorldSpace;2526/// Body 1 constraint position (space determined by mSpace).27RVec3 mPoint1 = RVec3::sZero();2829/// Body 2 constraint position (space determined by mSpace).30/// Note: Normally you would set mPoint1 = mPoint2 if the bodies are already placed how you want to constrain them (if mSpace = world space).31RVec3 mPoint2 = RVec3::sZero();3233protected:34// See: ConstraintSettings::RestoreBinaryState35virtual void RestoreBinaryState(StreamIn &inStream) override;36};3738/// A point constraint constrains 2 bodies on a single point (removing 3 degrees of freedom)39class JPH_EXPORT PointConstraint final : public TwoBodyConstraint40{41public:42JPH_OVERRIDE_NEW_DELETE4344/// Construct point constraint45PointConstraint(Body &inBody1, Body &inBody2, const PointConstraintSettings &inSettings);4647// Generic interface of a constraint48virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Point; }49virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;50virtual void SetupVelocityConstraint(float inDeltaTime) override;51virtual void ResetWarmStart() override;52virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;53virtual bool SolveVelocityConstraint(float inDeltaTime) override;54virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;55#ifdef JPH_DEBUG_RENDERER56virtual void DrawConstraint(DebugRenderer *inRenderer) const override;57#endif // JPH_DEBUG_RENDERER58virtual void SaveState(StateRecorder &inStream) const override;59virtual void RestoreState(StateRecorder &inStream) override;60virtual Ref<ConstraintSettings> GetConstraintSettings() const override;6162/// Update the attachment point for body 163void SetPoint1(EConstraintSpace inSpace, RVec3Arg inPoint1);6465/// Update the attachment point for body 266void SetPoint2(EConstraintSpace inSpace, RVec3Arg inPoint2);6768/// Get the attachment point for body 1 relative to body 1 COM (transform by Body::GetCenterOfMassTransform to take to world space)69inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }7071/// Get the attachment point for body 2 relative to body 2 COM (transform by Body::GetCenterOfMassTransform to take to world space)72inline Vec3 GetLocalSpacePoint2() const { return mLocalSpacePosition2; }7374// See: TwoBodyConstraint75virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }76virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition2); } // Note: Incorrect rotation as we don't track the original rotation difference, should not matter though as the constraint is not limiting rotation.7778///@name Get Lagrange multiplier from last physics update (the linear impulse applied to satisfy the constraint)79inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }8081private:82// Internal helper function to calculate the values below83void CalculateConstraintProperties();8485// Local space constraint positions86Vec3 mLocalSpacePosition1;87Vec3 mLocalSpacePosition2;8889// The constraint part90PointConstraintPart mPointConstraintPart;91};9293JPH_NAMESPACE_END949596