Skip to content

Commit c48d894

Browse files
committed
fixed hinge joint spring drive.
1 parent 7d09bf9 commit c48d894

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

Editor/CustomEditors/ZOHingeJointEditor.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
using UnityEditor;
55

66
namespace ZO.Editor {
7-
[CustomEditor(typeof(ZO.Physics.ZOHingeJoint))]
7+
[CustomEditor(typeof(ZO.Physics.ZOHingeJoint)), CanEditMultipleObjects]
88

99
public class ZOHingeJointEditor : UnityEditor.Editor {
10+
11+
private static readonly string[] _dontIncludeMe = new string[] { "_updateRateHz" };
12+
1013
public override void OnInspectorGUI() {
11-
DrawDefaultInspector();
14+
DrawPropertiesExcluding(serializedObject, _dontIncludeMe);
15+
serializedObject.ApplyModifiedProperties();
16+
17+
// DrawDefaultInspector();
1218

1319
// ZO.Physics.ZOHingeJoint hingeJoint = (ZO.Physics.ZOHingeJoint)target;
1420

Runtime/Scripts/Physics/ZOHingeJoint.cs

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Runtime/Scripts/Physics/ZOPIDController.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ public string Name {
114114
private JObject _json;
115115
public JObject JSON {
116116
get {
117-
// if (_json == null) {
118-
// _json = BuildJSON(_documentRoot);
119-
// }
120117
return _json;
121118
}
122119
}

0 commit comments

Comments
 (0)