Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/GearConstraint.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/GearConstraintPart.h>89JPH_NAMESPACE_BEGIN1011/// Gear constraint settings12class JPH_EXPORT GearConstraintSettings final : public TwoBodyConstraintSettings13{14JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, GearConstraintSettings)1516public:17// See: ConstraintSettings::SaveBinaryState18virtual void SaveBinaryState(StreamOut &inStream) const override;1920/// Create an instance of this constraint.21virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;2223/// Defines the ratio between the rotation of both gears24/// The ratio is defined as: Gear1Rotation(t) = -ratio * Gear2Rotation(t)25/// @param inNumTeethGear1 Number of teeth that body 1 has26/// @param inNumTeethGear2 Number of teeth that body 2 has27void SetRatio(int inNumTeethGear1, int inNumTeethGear2)28{29mRatio = float(inNumTeethGear2) / float(inNumTeethGear1);30}3132/// This determines in which space the constraint is setup, all properties below should be in the specified space33EConstraintSpace mSpace = EConstraintSpace::WorldSpace;3435/// Body 1 constraint reference frame (space determined by mSpace).36Vec3 mHingeAxis1 = Vec3::sAxisX();3738/// Body 2 constraint reference frame (space determined by mSpace)39Vec3 mHingeAxis2 = Vec3::sAxisX();4041/// Ratio between both gears, see SetRatio.42float mRatio = 1.0f;4344protected:45// See: ConstraintSettings::RestoreBinaryState46virtual void RestoreBinaryState(StreamIn &inStream) override;47};4849/// A gear constraint constrains the rotation of body1 to the rotation of body 2 using a gear.50/// Note that this constraint needs to be used in conjunction with a two hinge constraints.51class JPH_EXPORT GearConstraint final : public TwoBodyConstraint52{53public:54JPH_OVERRIDE_NEW_DELETE5556/// Construct gear constraint57GearConstraint(Body &inBody1, Body &inBody2, const GearConstraintSettings &inSettings);5859// Generic interface of a constraint60virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Gear; }61virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Do nothing */ }62virtual void SetupVelocityConstraint(float inDeltaTime) override;63virtual void ResetWarmStart() override;64virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;65virtual bool SolveVelocityConstraint(float inDeltaTime) override;66virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;67#ifdef JPH_DEBUG_RENDERER68virtual void DrawConstraint(DebugRenderer *inRenderer) const override;69#endif // JPH_DEBUG_RENDERER70virtual void SaveState(StateRecorder &inStream) const override;71virtual void RestoreState(StateRecorder &inStream) override;72virtual Ref<ConstraintSettings> GetConstraintSettings() const override;7374// See: TwoBodyConstraint75virtual Mat44 GetConstraintToBody1Matrix() const override;76virtual Mat44 GetConstraintToBody2Matrix() const override;7778/// The constraints that constrain both gears (2 hinges), optional and used to calculate the rotation error and fix numerical drift.79void SetConstraints(const Constraint *inGear1, const Constraint *inGear2) { mGear1Constraint = inGear1; mGear2Constraint = inGear2; }8081///@name Get Lagrange multiplier from last physics update (the angular impulse applied to satisfy the constraint)82inline float GetTotalLambda() const { return mGearConstraintPart.GetTotalLambda(); }8384private:85// Internal helper function to calculate the values below86void CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);8788// CONFIGURATION PROPERTIES FOLLOW8990// Local space hinge axis for body 191Vec3 mLocalSpaceHingeAxis1;9293// Local space hinge axis for body 294Vec3 mLocalSpaceHingeAxis2;9596// Ratio between gear 1 and 297float mRatio;9899// The constraints that constrain both gears (2 hinges), optional and used to calculate the rotation error and fix numerical drift.100RefConst<Constraint> mGear1Constraint;101RefConst<Constraint> mGear2Constraint;102103// RUN TIME PROPERTIES FOLLOW104105// World space hinge axis for body 1106Vec3 mWorldSpaceHingeAxis1;107108// World space hinge axis for body 2109Vec3 mWorldSpaceHingeAxis2;110111// The constraint parts112GearConstraintPart mGearConstraintPart;113};114115JPH_NAMESPACE_END116117118