@@ -55,7 +55,9 @@ public class ModuleIRServo : PartModule, IRescalable, IJointLockState
5555 [ KSPField ( isPersistant = false ) ]
5656 public float jointSpring = 0 ;
5757 [ KSPField ( isPersistant = false ) ]
58- public float jointDamping = 0 ;
58+ public float jointDamping = 0 ;
59+
60+ bool isOnRails = false ;
5961
6062 [ KSPField ( isPersistant = true ) ] public bool rotateLimits = false ;
6163 [ KSPField ( isPersistant = true ) ] public float rotateMax = 360 ;
@@ -131,6 +133,7 @@ public class ModuleIRServo : PartModule, IRescalable, IJointLockState
131133 protected const string ELECTRIC_CHARGE_RESOURCE_NAME = "ElectricCharge" ;
132134
133135 protected ConfigurableJoint joint ;
136+ protected ConfigurableJoint savedJoint ;
134137 protected Rigidbody jointRigidBody ;
135138
136139 protected SoundSource motorSound ;
@@ -427,9 +430,62 @@ public override void OnAwake()
427430
428431 Translator . Init ( isMotionLock , new Servo ( this ) , Interpolator ) ;
429432
433+ GameEvents . onVesselGoOnRails . Add ( OnVesselGoOnRails ) ;
434+ GameEvents . onVesselGoOffRails . Add ( OnVesselGoOffRails ) ;
435+
430436 Logger . Log ( string . Format ( "[OnAwake] End, rotateLimits={0}, minTweak={1}, maxTweak={2}, rotateJoint={0}" , rotateLimits , minTweak , maxTweak ) , Logger . Level . Debug ) ;
431437 }
432-
438+
439+ public void OnVesselGoOnRails ( Vessel v )
440+ {
441+ if ( v != vessel )
442+ return ;
443+
444+ Logger . Log ( "[OnVesselGoOnRails] Reverting Joint" , Logger . Level . Debug ) ;
445+
446+ part . attachJoint . Joint . angularXDrive = savedJoint . angularXDrive ;
447+ part . attachJoint . Joint . angularYZDrive = savedJoint . angularYZDrive ;
448+ part . attachJoint . Joint . xDrive = savedJoint . xDrive ;
449+ part . attachJoint . Joint . yDrive = savedJoint . yDrive ;
450+ part . attachJoint . Joint . zDrive = savedJoint . zDrive ;
451+ part . attachJoint . Joint . enableCollision = false ;
452+
453+ if ( joint )
454+ {
455+ // lock all movement by default
456+ joint . xMotion = ConfigurableJointMotion . Locked ;
457+ joint . yMotion = ConfigurableJointMotion . Locked ;
458+ joint . zMotion = ConfigurableJointMotion . Locked ;
459+ joint . angularXMotion = ConfigurableJointMotion . Locked ;
460+ joint . angularYMotion = ConfigurableJointMotion . Locked ;
461+ joint . angularZMotion = ConfigurableJointMotion . Locked ;
462+ }
463+
464+ rotationDelta = GetRealRotation ( ) ;
465+ translationDelta = GetRealTranslation ( ) ;
466+
467+ isOnRails = true ;
468+ }
469+
470+
471+ public void OnVesselGoOffRails ( Vessel v )
472+ {
473+ if ( v != vessel )
474+ return ;
475+
476+ JointSetupDone = false ;
477+ Logger . Log ( "[OnVesselGoOffRails] Resetting Joint" , Logger . Level . Debug ) ;
478+
479+ if ( joint )
480+ {
481+ DestroyImmediate ( joint ) ;
482+ }
483+
484+ SetupJoints ( ) ;
485+
486+ isOnRails = false ;
487+ }
488+
433489 public override void OnSave ( ConfigNode node )
434490 {
435491 Logger . Log ( "[OnSave] Start" , Logger . Level . Debug ) ;
@@ -563,15 +619,17 @@ protected virtual void AttachToParent()
563619 {
564620 Transform fix = FixedMeshTransform ;
565621 //Transform fix = obj;
622+
623+
566624 if ( rotateJoint )
567625 {
568- fix . RotateAround ( transform . TransformPoint ( rotatePivot ) , transform . TransformDirection ( rotateAxis ) ,
626+ fix . RotateAround ( part . transform . TransformPoint ( rotatePivot ) , part . transform . TransformDirection ( rotateAxis ) ,
569627 //(invertSymmetry ? ((part.symmetryCounterparts.Count != 1) ? -1 : 1) : -1) *
570628 - rotation ) ;
571629 }
572630 else if ( translateJoint )
573631 {
574- fix . Translate ( transform . TransformDirection ( translateAxis . normalized ) * translation , Space . World ) ;
632+ fix . Translate ( part . transform . TransformDirection ( translateAxis . normalized ) * translation , Space . World ) ;
575633 }
576634 fix . parent = part . parent . transform ;
577635 }
@@ -746,19 +804,11 @@ public virtual bool SetupJoints()
746804 return false ;
747805 }
748806
749- if ( part . attachJoint . Joint . xDrive . maximumForce > 0.0001 )
750- {
751- JointSetupDone = false ;
752- Logger . Log ( "Resetting Joint" , Logger . Level . Debug ) ;
753- if ( joint )
754- {
755- DestroyImmediate ( joint ) ;
756- }
757- }
758-
759807 if ( JointSetupDone )
760808 return false ;
761-
809+
810+ savedJoint = part . attachJoint . Joint ;
811+
762812 // Catch reversed joint
763813 // Maybe there is a best way to do it?
764814 if ( transform . position != part . attachJoint . Joint . connectedBody . transform . position )
@@ -1387,6 +1437,9 @@ public void FixedUpdate()
13871437 return ;
13881438 }
13891439
1440+ if ( isOnRails )
1441+ return ;
1442+
13901443 if ( minTweak > maxTweak )
13911444 {
13921445 maxTweak = minTweak ;
0 commit comments