Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/RackAndPinionConstraint.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/ConstraintPart/RackAndPinionConstraintPart.h>89JPH_NAMESPACE_BEGIN1011/// Rack and pinion constraint (slider & gear) settings12class JPH_EXPORT RackAndPinionConstraintSettings final : public TwoBodyConstraintSettings13{14JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, RackAndPinionConstraintSettings)1516public:17// See: ConstraintSettings::SaveBinaryState18virtual void SaveBinaryState(StreamOut &inStream) const override;1920/// Create an instance of this constraint.21/// Body1 should be the pinion (gear) and body 2 the rack (slider).22virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;2324/// Defines the ratio between the rotation of the pinion and the translation of the rack.25/// The ratio is defined as: PinionRotation(t) = ratio * RackTranslation(t)26/// @param inNumTeethRack Number of teeth that the rack has27/// @param inRackLength Length of the rack28/// @param inNumTeethPinion Number of teeth the pinion has29void SetRatio(int inNumTeethRack, float inRackLength, int inNumTeethPinion)30{31mRatio = 2.0f * JPH_PI * inNumTeethRack / (inRackLength * inNumTeethPinion);32}3334/// This determines in which space the constraint is setup, all properties below should be in the specified space35EConstraintSpace mSpace = EConstraintSpace::WorldSpace;3637/// Body 1 (pinion) constraint reference frame (space determined by mSpace).38Vec3 mHingeAxis = Vec3::sAxisX();3940/// Body 2 (rack) constraint reference frame (space determined by mSpace)41Vec3 mSliderAxis = Vec3::sAxisX();4243/// Ratio between the rack and pinion, see SetRatio.44float mRatio = 1.0f;4546protected:47// See: ConstraintSettings::RestoreBinaryState48virtual void RestoreBinaryState(StreamIn &inStream) override;49};5051/// A rack and pinion constraint constrains the rotation of body1 to the translation of body 2.52/// Note that this constraint needs to be used in conjunction with a hinge constraint for body 1 and a slider constraint for body 2.53class JPH_EXPORT RackAndPinionConstraint final : public TwoBodyConstraint54{55public:56JPH_OVERRIDE_NEW_DELETE5758/// Construct gear constraint59RackAndPinionConstraint(Body &inBody1, Body &inBody2, const RackAndPinionConstraintSettings &inSettings);6061// Generic interface of a constraint62virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::RackAndPinion; }63virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Nothing */ }64virtual void SetupVelocityConstraint(float inDeltaTime) override;65virtual void ResetWarmStart() override;66virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;67virtual bool SolveVelocityConstraint(float inDeltaTime) override;68virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;69#ifdef JPH_DEBUG_RENDERER70virtual void DrawConstraint(DebugRenderer *inRenderer) const override;71#endif // JPH_DEBUG_RENDERER72virtual void SaveState(StateRecorder &inStream) const override;73virtual void RestoreState(StateRecorder &inStream) override;74virtual Ref<ConstraintSettings> GetConstraintSettings() const override;7576// See: TwoBodyConstraint77virtual Mat44 GetConstraintToBody1Matrix() const override;78virtual Mat44 GetConstraintToBody2Matrix() const override;7980/// The constraints that constrain the rack and pinion (a slider and a hinge), optional and used to calculate the position error and fix numerical drift.81void SetConstraints(const Constraint *inPinion, const Constraint *inRack) { mPinionConstraint = inPinion; mRackConstraint = inRack; }8283///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)84inline float GetTotalLambda() const { return mRackAndPinionConstraintPart.GetTotalLambda(); }8586private:87// Internal helper function to calculate the values below88void CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);8990// CONFIGURATION PROPERTIES FOLLOW9192// Local space hinge axis93Vec3 mLocalSpaceHingeAxis;9495// Local space sliding direction96Vec3 mLocalSpaceSliderAxis;9798// Ratio between rack and pinion99float mRatio;100101// The constraints that constrain the rack and pinion (a slider and a hinge), optional and used to calculate the position error and fix numerical drift.102RefConst<Constraint> mPinionConstraint;103RefConst<Constraint> mRackConstraint;104105// RUN TIME PROPERTIES FOLLOW106107// World space hinge axis108Vec3 mWorldSpaceHingeAxis;109110// World space sliding direction111Vec3 mWorldSpaceSliderAxis;112113// The constraint parts114RackAndPinionConstraintPart mRackAndPinionConstraintPart;115};116117JPH_NAMESPACE_END118119120