88using UnityEngine ;
99using Synthesis . Util ;
1010using Synthesis . Physics ;
11+ using Math = System . Math ;
1112
1213#nullable enable
1314
@@ -115,6 +116,7 @@ public Rigidbody RbB {
115116 }
116117 }
117118
119+ private float _lastVel = 0f ;
118120 private float _fakedTheta = 0f ;
119121 private float _fakedOmega = 0f ;
120122
@@ -167,9 +169,9 @@ public RotationalDriver(string name, string[] inputs, string[] outputs, SimObjec
167169 } else {
168170 Motor = new JointMotor ( ) {
169171 // Default Motor. Slow but powerful enough. Also uses Motor to save it
170- force = 2000 ,
172+ force = 0.1f ,
171173 freeSpin = false ,
172- targetVelocity = 5 ,
174+ targetVelocity = 0.2f ,
173175 } ;
174176 }
175177
@@ -269,9 +271,21 @@ private void VelocityControl(float deltaT) {
269271 SynthesisUtil . GetInertiaAroundParallelAxis ( _jointB . connectedBody , _jointA . anchor , _jointA . axis ) ;
270272
271273 if ( _useFakeMotion ) {
272- float alpha = val * _convertedMotorTargetVel ;
274+ float tarVel = val == 0 ? 0 : Mathf . Sign ( val ) * _convertedMotorTargetVel ;
273275
274- _fakedTheta += alpha * deltaT ;
276+ var delta = tarVel - _lastVel ;
277+ var posDelta = _motor . force * Mathf . Rad2Deg * Time . deltaTime ;
278+
279+ if ( Mathf . Abs ( delta ) > posDelta )
280+ delta = posDelta * Mathf . Sign ( delta ) ;
281+
282+ _lastVel += delta ;
283+
284+ if ( Mathf . Abs ( _lastVel ) > _convertedMotorTargetVel )
285+ _lastVel = _convertedMotorTargetVel * Mathf . Sign ( _lastVel ) ;
286+
287+ float lastFakedTheta = _fakedTheta ;
288+ _fakedTheta += _lastVel * deltaT ;
275289
276290 if ( _rotationalLimits . HasValue ) {
277291 if ( _fakedTheta > _rotationalLimits . Value . max ) {
@@ -280,8 +294,13 @@ private void VelocityControl(float deltaT) {
280294 _fakedTheta = _rotationalLimits . Value . min ;
281295 }
282296
297+ // Limit theta to specific range
283298 _fakedTheta = Mathf . Clamp ( _fakedTheta , - 180 , 179 ) ;
284299
300+ // Check and see if we've hit a hard limit to zero out velocity
301+ if ( Math . Abs ( lastFakedTheta - _fakedTheta ) < 0.001f )
302+ _lastVel = 0 ;
303+
285304 _jointA . limits =
286305 new JointLimits { bounceMinVelocity = _rotationalLimits . Value . bounceMinVelocity ,
287306 bounciness = _rotationalLimits . Value . bounciness ,
0 commit comments