@@ -22,14 +22,21 @@ namespace ZO.Physics {
2222 /// constrains the joint angle.
2323 /// </summary>
2424 [ ExecuteAlways ]
25- public class ZOHingeJoint : MonoBehaviour , ZOJointInterface {
25+ public class ZOHingeJoint : ZOGameObjectBase , ZOJointInterface {
2626
2727 public Rigidbody _connectedBody ;
2828 public Vector3 _anchor = Vector3 . zero ;
2929 public Vector3 _axis = Vector3 . forward ;
30+
31+ [ Header ( "Joint Motor Control" ) ]
3032 public bool _useMotor = true ;
3133 public float _motorForce = 1.0f ;
3234
35+ [ Header ( "Joint Torsional Spring Control" ) ]
36+ public bool _useSpring = false ;
37+ public float _springConstant = 100.0f ;
38+ public float _springDampening = 20.0f ;
39+
3340 [ SerializeField ] [ ZOReadOnlyAttribute ] public UnityEngine . HingeJoint _hingeJoint ;
3441
3542 /// <summary>
@@ -51,6 +58,7 @@ public bool UseMotor {
5158 return UnityHingeJoint . useMotor ;
5259 }
5360 set {
61+ _useMotor = value ;
5462 UnityHingeJoint . useMotor = value ;
5563 }
5664 }
@@ -70,6 +78,53 @@ public float MotorForce {
7078 }
7179 }
7280
81+
82+ /// <summary>
83+ /// Use spring to drive joint.
84+ /// </summary>
85+ /// <value></value>
86+ public bool UseSpring {
87+ get {
88+ return UnityHingeJoint . useSpring ;
89+ }
90+ set {
91+ _useSpring = value ;
92+ UnityHingeJoint . useSpring = value ;
93+ }
94+ }
95+
96+ /// <summary>
97+ /// Spring constant force.
98+ /// </summary>
99+ /// <value></value>
100+ public float SpringConstant {
101+ get {
102+ return UnityHingeJoint . spring . spring ;
103+ }
104+ set {
105+ JointSpring spring = UnityHingeJoint . spring ;
106+ _springConstant = value ;
107+ spring . spring = value ;
108+ UnityHingeJoint . spring = spring ;
109+ }
110+ }
111+
112+ /// <summary>
113+ /// The damper force used to dampen the spring.
114+ /// </summary>
115+ /// <value></value>
116+ public float SpringDampening {
117+ get {
118+ return UnityHingeJoint . spring . damper ;
119+ }
120+ set {
121+ JointSpring spring = UnityHingeJoint . spring ;
122+ _springDampening = value ;
123+ spring . damper = value ;
124+ UnityHingeJoint . spring = spring ;
125+ }
126+ }
127+
73128 /// <summary>
74129 /// The direction of axis in which the body is constrained.
75130 /// </summary>
@@ -141,9 +196,6 @@ public Rigidbody ConnectedBody {
141196 }
142197
143198
144- public bool _debug = false ;
145-
146-
147199 #region ZOJointInterface
148200
149201 /// <summary>
@@ -172,7 +224,9 @@ public float Position {
172224 /// </summary>
173225 /// <value></value>
174226 public float Velocity {
175- get { return UnityHingeJoint . velocity * Mathf . Deg2Rad ; }
227+ get {
228+ return UnityHingeJoint . velocity * Mathf . Deg2Rad ;
229+ }
176230
177231 set {
178232 JointMotor motor = UnityHingeJoint . motor ;
@@ -187,7 +241,7 @@ public float Velocity {
187241
188242
189243 /// <summary>
190- /// The effor that is applied to the hinge Nm
244+ /// The effort that is applied to the hinge (Nm)
191245 /// </summary>
192246 /// <value></value>
193247 public float Effort {
@@ -226,7 +280,8 @@ public ZOSimOccurrence ConnectedOccurrence {
226280 private float _currentAngleRadians = 0 ;
227281
228282 #region MonoBehaviour
229- private void Start ( ) {
283+ protected override void ZOStart ( ) {
284+ base . ZOStart ( ) ;
230285 // calculate starting "zero" joint angle in radians
231286 // NOTE: this has to be done because there is a bug in the Unity HingeJoint angle :-/
232287 if ( ConnectedBody ) {
@@ -235,7 +290,8 @@ private void Start() {
235290 }
236291 }
237292
238- private void FixedUpdate ( ) {
293+ protected override void ZOFixedUpdate ( ) {
294+ base . ZOFixedUpdate ( ) ;
239295 // calculate joint angle relative to the starting joint
240296 // NOTE: this has to be done because there is a bug in the Unity HingeJoint angle :-/
241297 Quaternion r = Quaternion . Inverse ( this . transform . rotation ) * ConnectedBody . transform . rotation ;
@@ -244,7 +300,8 @@ private void FixedUpdate() {
244300
245301 }
246302
247- private void OnGUI ( ) {
303+ protected override void ZOOnGUI ( ) {
304+ base . ZOOnGUI ( ) ;
248305 if ( _debug ) {
249306 // Vector3 worldAxis = this.transform.rotation * UnityHingeJoint.axis;
250307 // ZOSimOccurrence occurrence = GetComponent<ZOSimOccurrence>();
@@ -261,12 +318,16 @@ private void OnGUI() {
261318 }
262319 }
263320
264- private void OnValidate ( ) {
321+ protected override void ZOOnValidate ( ) {
322+ base . ZOOnValidate ( ) ;
265323 Axis = _axis ;
266324 ConnectedBody = _connectedBody ;
267325 Anchor = _anchor ;
268326 UseMotor = _useMotor ;
269327 MotorForce = _motorForce ;
328+ UseSpring = _useSpring ;
329+ SpringConstant = _springConstant ;
330+ SpringDampening = _springDampening ;
270331 }
271332
272333 #endregion
@@ -278,7 +339,8 @@ private void OnValidate() {
278339 /// <summary>
279340 /// Reset is a MonoBehavior method that gets called on creation of this component.
280341 /// </summary>
281- private void Reset ( ) {
342+ protected override void ZOReset ( ) {
343+ base . ZOReset ( ) ;
282344 CreateRequirements ( ) ;
283345 }
284346
@@ -331,9 +393,6 @@ public string Name {
331393 }
332394 }
333395
334-
335-
336-
337396 }
338397
339398}
0 commit comments