From 99a74975acd04de49877f1ed1cb4a3ab3c21492f Mon Sep 17 00:00:00 2001 From: Evgeniy Sidorenko Date: Tue, 13 Aug 2024 15:29:11 +0300 Subject: [PATCH] constraints: Implemented pulley --- vphysics_jolt/cbase.h | 1 + vphysics_jolt/vjolt_constraints.cpp | 33 +++++++++++++++++++++++++++++ vphysics_jolt/vjolt_constraints.h | 1 + vphysics_jolt/vjolt_environment.cpp | 5 +++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/vphysics_jolt/cbase.h b/vphysics_jolt/cbase.h index 76bd02e2..13d3820a 100644 --- a/vphysics_jolt/cbase.h +++ b/vphysics_jolt/cbase.h @@ -126,6 +126,7 @@ #include #include #include +#include #include #include #include diff --git a/vphysics_jolt/vjolt_constraints.cpp b/vphysics_jolt/vjolt_constraints.cpp index a698db30..0808bffa 100644 --- a/vphysics_jolt/vjolt_constraints.cpp +++ b/vphysics_jolt/vjolt_constraints.cpp @@ -566,6 +566,39 @@ void JoltPhysicsConstraint::InitialiseLength( IPhysicsConstraintGroup *pGroup, c m_pPhysicsSystem->AddConstraint( m_pConstraint ); } +//------------------------------------------------------------------------------------------------- +// Pulley +//------------------------------------------------------------------------------------------------- + +void JoltPhysicsConstraint::InitialisePulley( IPhysicsConstraintGroup *pGroup, const constraint_pulleyparams_t &pulley ) +{ + SetGroup( pGroup ); + m_ConstraintType = CONSTRAINT_PULLEY; + + // Get our bodies + JPH::Body* refBody = m_pObjReference->GetBody(); + JPH::Body* attBody = m_pObjAttached->GetBody(); + + JPH::PulleyConstraintSettings settings; + settings.mNumVelocityStepsOverride = vjolt_constraint_velocity_substeps.GetInt(); + settings.mNumPositionStepsOverride = vjolt_constraint_position_substeps.GetInt(); + settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM; + settings.mBodyPoint1 = SourceToJolt::Distance( pulley.objectPosition[0] ) - refBody->GetShape()->GetCenterOfMass(); + settings.mBodyPoint2 = SourceToJolt::Distance( pulley.objectPosition[1] ) - attBody->GetShape()->GetCenterOfMass(); + + settings.mFixedPoint1 = SourceToJolt::Distance( pulley.pulleyPosition[0] ); + settings.mFixedPoint2 = SourceToJolt::Distance( pulley.pulleyPosition[1] ); + + settings.mRatio = pulley.gearRatio; + + settings.mMaxLength = SourceToJolt::Distance( pulley.totalLength ); // PiMoN: from my testing, it is the same value as Jolt would calculate automatically + + m_pConstraint = settings.Create( *refBody, *attBody ); + m_pConstraint->SetEnabled( !pGroup && pulley.constraint.isActive ); + + m_pPhysicsSystem->AddConstraint( m_pConstraint ); +} + //------------------------------------------------------------------------------------------------- void JoltPhysicsConstraint::SaveConstraintSettings( JPH::StateRecorder &recorder ) diff --git a/vphysics_jolt/vjolt_constraints.h b/vphysics_jolt/vjolt_constraints.h index 349b6e0d..4945ac4a 100644 --- a/vphysics_jolt/vjolt_constraints.h +++ b/vphysics_jolt/vjolt_constraints.h @@ -79,6 +79,7 @@ class JoltPhysicsConstraint final : public IPhysicsConstraint, public IJoltObjec void InitialiseBallsocket( IPhysicsConstraintGroup *pGroup, const constraint_ballsocketparams_t &ballsocket ); void InitialiseFixed( IPhysicsConstraintGroup *pGroup, const constraint_fixedparams_t &fixed ); void InitialiseLength( IPhysicsConstraintGroup *pGroup, const constraint_lengthparams_t &length ); + void InitialisePulley( IPhysicsConstraintGroup *pGroup, const constraint_pulleyparams_t &pulley ); void SaveConstraintSettings( JPH::StateRecorder &recorder ); diff --git a/vphysics_jolt/vjolt_environment.cpp b/vphysics_jolt/vjolt_environment.cpp index 754846fd..125d69f0 100644 --- a/vphysics_jolt/vjolt_environment.cpp +++ b/vphysics_jolt/vjolt_environment.cpp @@ -631,8 +631,9 @@ IPhysicsConstraint *JoltPhysicsEnvironment::CreateBallsocketConstraint( IPhysics IPhysicsConstraint *JoltPhysicsEnvironment::CreatePulleyConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_pulleyparams_t &pulley ) { - Log_Stub( LOG_VJolt ); - return nullptr; + JoltPhysicsConstraint *pConstraint = new JoltPhysicsConstraint( this, pReferenceObject, pAttachedObject ); + pConstraint->InitialisePulley( pGroup, pulley ); + return pConstraint; } IPhysicsConstraint *JoltPhysicsEnvironment::CreateLengthConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_lengthparams_t &length )