@@ -22,7 +22,7 @@ namespace ZO.Physics {
2222 /// constrains the joint angle.
2323 /// </summary>
2424 [ ExecuteAlways ]
25- public class ZOHingeJoint : MonoBehaviour , ZOSerializationInterface , ZOJointInterface {
25+ public class ZOHingeJoint : MonoBehaviour , ZOJointInterface {
2626
2727 public Rigidbody _connectedBody ;
2828 public Vector3 _anchor = Vector3 . zero ;
@@ -338,136 +338,6 @@ public JObject JSON {
338338 }
339339
340340
341- /// <summary>
342- /// Serializes the ZOHingeJoint to ZOSim JSON.
343- /// </summary>
344- /// <param name="documentRoot"></param>
345- /// <param name="parent"></param>
346- /// <returns></returns>
347- public JObject Serialize ( ZOSimDocumentRoot documentRoot , UnityEngine . Object parent = null ) {
348- // calculate the world anchor positions relative to the document root transform
349- // BUGBUG: maybe from the base of the joint chain which is not necessarily the document root?
350- Vector3 worldAnchor = this . transform . TransformPoint ( UnityHingeJoint . anchor ) ;
351- worldAnchor = documentRoot . transform . InverseTransformPoint ( worldAnchor ) ;
352-
353- Vector3 worldConnectedAnchor = this . transform . TransformPoint ( UnityHingeJoint . connectedAnchor ) ;
354- worldConnectedAnchor = documentRoot . transform . InverseTransformPoint ( worldConnectedAnchor ) ;
355-
356- Vector3 worldAxis = this . transform . rotation * UnityHingeJoint . axis ;
357- worldAxis = documentRoot . transform . InverseTransformDirection ( worldAxis ) ;
358-
359- JObject json = new JObject (
360- new JProperty ( "name" , Name ) ,
361- new JProperty ( "type" , Type ) ,
362- new JProperty ( "anchor" , ZOSimDocumentRoot . ToJSON ( UnityHingeJoint . anchor ) ) ,
363- new JProperty ( "world_anchor" , ZOSimDocumentRoot . ToJSON ( worldAnchor ) ) ,
364- new JProperty ( "axis" , ZOSimDocumentRoot . ToJSON ( UnityHingeJoint . axis ) ) ,
365- new JProperty ( "world_axis" , ZOSimDocumentRoot . ToJSON ( worldAxis ) ) ,
366- new JProperty ( "connected_anchor" , ZOSimDocumentRoot . ToJSON ( UnityHingeJoint . connectedAnchor ) ) ,
367- new JProperty ( "world_connected_anchor" , ZOSimDocumentRoot . ToJSON ( worldConnectedAnchor ) ) ,
368- new JProperty ( "use_spring" , UnityHingeJoint . useSpring ) ,
369- new JProperty ( "spring" , new JObject (
370- new JProperty ( "spring" , UnityHingeJoint . spring . spring ) ,
371- new JProperty ( "damper" , UnityHingeJoint . spring . damper ) ,
372- new JProperty ( "target_position" , UnityHingeJoint . spring . targetPosition )
373- ) ) ,
374- new JProperty ( "use_motor" , UnityHingeJoint . useMotor ) ,
375- new JProperty ( "motor" , new JObject (
376- new JProperty ( "target_velocity" , UnityHingeJoint . motor . targetVelocity ) ,
377- new JProperty ( "force" , UnityHingeJoint . motor . force ) ,
378- new JProperty ( "free_spin" , UnityHingeJoint . motor . freeSpin )
379- ) ) ,
380- new JProperty ( "use_limits" , UnityHingeJoint . useLimits ) ,
381- new JProperty ( "limits" , new JObject (
382- new JProperty ( "min" , UnityHingeJoint . limits . min ) ,
383- new JProperty ( "max" , UnityHingeJoint . limits . max ) ,
384- new JProperty ( "bounciness" , UnityHingeJoint . limits . bounciness ) ,
385- new JProperty ( "bounce_min_velocity" , UnityHingeJoint . limits . bounceMinVelocity ) ,
386- new JProperty ( "contact_distance" , UnityHingeJoint . limits . contactDistance )
387- ) )
388- ) ;
389-
390- if ( UnityHingeJoint . connectedBody ) {
391- ZOSimOccurrence connected_occurrence = UnityHingeJoint . connectedBody . gameObject . GetComponent < ZOSimOccurrence > ( ) ;
392-
393- if ( connected_occurrence ) {
394- json [ "connected_occurrence" ] = connected_occurrence . Name ;
395- } else {
396- Debug . LogWarning ( "WARNING: Could not get connected occurrence for ZOHingeJoint: " + Name + "\n Perhaps there is a missing ZOSimOccurrence?" ) ;
397- }
398- } else {
399- Debug . LogWarning ( "WARNING: Could not get connected occurrence for ZOHingeJoint: " + Name ) ;
400- }
401-
402- ZOSimOccurrence parent_occurrence = GetComponent < ZOSimOccurrence > ( ) ;
403- if ( parent_occurrence ) {
404- json [ "parent_occurrence" ] = parent_occurrence . Name ;
405- }
406-
407- _json = json ;
408-
409- return json ;
410- }
411-
412-
413- public void Deserialize ( ZOSimDocumentRoot documentRoot , JObject json ) {
414- // Assert.Equals(json["type"].Value<string>() == Type);
415-
416- _json = json ;
417- Name = json . ValueOrDefault ( "name" , Name ) ;
418- UnityHingeJoint . anchor = json . ToVector3OrDefault ( "anchor" , UnityHingeJoint . anchor ) ;
419- UnityHingeJoint . axis = json . ToVector3OrDefault ( "axis" , UnityHingeJoint . axis ) ;
420- UnityHingeJoint . connectedAnchor = json . ToVector3OrDefault ( "connected_anchor" , UnityHingeJoint . connectedAnchor ) ;
421- UnityHingeJoint . useSpring = json . ValueOrDefault < bool > ( "use_spring" , UnityHingeJoint . useSpring ) ;
422-
423-
424- if ( json . ContainsKey ( "spring" ) ) {
425- JObject springJSON = json [ "spring" ] . Value < JObject > ( ) ;
426- JointSpring spring = UnityHingeJoint . spring ;
427- spring . spring = springJSON . ValueOrDefault < float > ( "spring" , spring . spring ) ;
428- spring . damper = springJSON . ValueOrDefault < float > ( "damper" , spring . damper ) ;
429- spring . targetPosition = springJSON . ValueOrDefault < float > ( "target_position" , spring . targetPosition ) ;
430- UnityHingeJoint . spring = spring ;
431-
432- }
433-
434- UnityHingeJoint . useMotor = json . ValueOrDefault < bool > ( "use_motor" , UnityHingeJoint . useMotor ) ;
435- if ( json . ContainsKey ( "use_motor" ) ) {
436- JObject motorJSON = json [ "motor" ] . Value < JObject > ( ) ;
437- JointMotor motor = UnityHingeJoint . motor ;
438- motor . targetVelocity = motorJSON . ValueOrDefault < float > ( "target_velocity" , motor . targetVelocity ) ;
439- motor . force = motorJSON . ValueOrDefault < float > ( "force" , motor . force ) ;
440- motor . freeSpin = motorJSON . ValueOrDefault < bool > ( "free_spin" , motor . freeSpin ) ;
441- UnityHingeJoint . motor = motor ;
442- }
443-
444- UnityHingeJoint . useLimits = json . ValueOrDefault < bool > ( "use_limits" , UnityHingeJoint . useLimits ) ;
445- if ( json . ContainsKey ( "limits" ) ) {
446- JObject limitsJSON = json [ "limits" ] . Value < JObject > ( ) ;
447- JointLimits limits = UnityHingeJoint . limits ;
448- limits . min = limitsJSON . ValueOrDefault < float > ( "min" , limits . min ) ;
449- limits . max = limitsJSON . ValueOrDefault < float > ( "max" , limits . max ) ;
450- limits . bounciness = limitsJSON . ValueOrDefault < float > ( "bounciness" , limits . bounciness ) ;
451- limits . bounceMinVelocity = limitsJSON . ValueOrDefault < float > ( "bounce_min_velocity" , UnityHingeJoint . limits . bounceMinVelocity ) ;
452- limits . contactDistance = limitsJSON . ValueOrDefault < float > ( "contact_distance" , limits . contactDistance ) ;
453- UnityHingeJoint . limits = limits ;
454- }
455-
456- // find connected body. this likely will need to be done post LoadFromJSON as it may
457- // not be created yet.
458- documentRoot . OnPostDeserializationNotification ( ( docRoot ) => {
459- if ( JSON . ContainsKey ( "connected_occurrence" ) ) {
460- ZOSimOccurrence connectedOccurrence = docRoot . GetOccurrence ( JSON [ "connected_occurrence" ] . Value < string > ( ) ) ;
461- if ( connectedOccurrence ) {
462- UnityHingeJoint . connectedBody = connectedOccurrence . GetComponent < Rigidbody > ( ) ;
463- } else {
464- Debug . LogWarning ( "WARNING: ZOHingeJoint failed to find connected occurrence: " + JSON [ "connected_occurrence" ] . Value < string > ( ) ) ;
465- }
466-
467- }
468- } ) ;
469-
470- }
471341 #endregion
472342
473343 }
0 commit comments