private void Awake()
    {
        Vector3 vector = base.transform.TransformPoint(anchor);

        if (autoConfigureConnectedAnchor)
        {
            connectedAnchor = connectedBody.transform.InverseTransformPoint(vector);
        }
        Vector3 b = connectedBody.transform.TransformPoint(connectedAnchor);

        if (autoconfigureStringLength)
        {
            stringLength = (vector - b).magnitude;
        }
        ConfigurableJoint configurableJoint = base.gameObject.AddComponent <ConfigurableJoint>();

        configurableJoint.anchor = anchor;
        configurableJoint.autoConfigureConnectedAnchor = false;
        configurableJoint.connectedBody   = connectedBody;
        configurableJoint.connectedAnchor = connectedAnchor;
        SoftJointLimit softJointLimit = default(SoftJointLimit);

        softJointLimit.limit = stringLength;
        SoftJointLimit          softJointLimit3          = configurableJoint.linearLimit = softJointLimit;
        ConfigurableJointMotion configurableJointMotion2 = configurableJoint.zMotion = ConfigurableJointMotion.Limited;

        configurableJointMotion2 = (configurableJoint.xMotion = (configurableJoint.yMotion = configurableJointMotion2));
        configurableJointMotion2 = (configurableJoint.angularZMotion = ConfigurableJointMotion.Free);
        configurableJointMotion2 = (configurableJoint.angularXMotion = (configurableJoint.angularYMotion = configurableJointMotion2));
    }
Beispiel #2
0
        void Start()
        {
            Debug.LogWarning("PropRoot and Prop system is deprecated. Please see the 'Prop' demo to learn about the new easier and much more performance-efficient PropMuscle and PuppetMasterProp system.", transform);

            if (transform.position != muscle.transform.position)
            {
                Debug.LogError("Prop target position must match exactly with its muscle's position!", transform);
            }

            xMotion        = muscle.xMotion;
            yMotion        = muscle.yMotion;
            zMotion        = muscle.zMotion;
            angularXMotion = muscle.angularXMotion;
            angularYMotion = muscle.angularYMotion;
            angularZMotion = muscle.angularZMotion;

            colliders = muscle.GetComponentsInChildren <Collider>();

            if (!isPickedUp)
            {
                ReleaseJoint();
            }

            OnStart();

            initiated = true;
        }
    private void Awake()
    {
        ConfigurableJoint component = GetComponent <ConfigurableJoint>();

        if ((bool)component)
        {
            connectedBody     = component.connectedBody;
            xMotion           = component.xMotion;
            yMotion           = component.yMotion;
            zMotion           = component.zMotion;
            lowAngularXLimit  = component.lowAngularXLimit;
            highAngularXLimit = component.highAngularXLimit;
            angularYLimit     = component.angularYLimit;
            angularZLimit     = component.angularZLimit;
            angularXDrive     = component.angularXDrive;
            angularYZDrive    = component.angularYZDrive;
            slerpDrive        = component.slerpDrive;
            linearLimit       = component.linearLimit;
            breakForce        = component.breakForce;
            breakTorque       = component.breakTorque;
            if (connectedBelow != null)
            {
                component = connectedBelow.GetComponent <ConfigurableJoint>();
                if (component != null)
                {
                    Rigidbody rigidbody = component.connectedBody = GetComponent <Rigidbody>();
                }
            }
        }
        else
        {
            noJoint = true;
        }
    }
 public void EnableJointLimits(ConfigurableJointMotion m)
 {
     if (joint)
     {
         joint.angularXMotion = joint.angularYMotion = joint.angularZMotion = m;
     }
 }
Beispiel #5
0
 public virtual void Reset()
 {
     jointType           = JointType.FixedJoint;
     anchor              = connectedAnchor = Vector3.zero;
     axis                = new Vector3(1, 0, 0);
     swingAxis           = new Vector3(0, 1, 0);
     breakForce          = Mathf.Infinity;
     breakTorque         = Mathf.Infinity;
     enablePreprocessing = true;
     tolerance           = 0.025f;
     spring.Reset();
     lowTwistLimit      = new SerializableSoftJointLimit(-20, 0, 0);
     highTwistLimit     = new SerializableSoftJointLimit(70, 0, 0);
     swing1Limit        = new SerializableSoftJointLimit(40, 0, 0);
     swing2Limit        = new SerializableSoftJointLimit(40, 0, 0);
     projectionDistance = 0.1f;
     projectionAngle    = 180;
     secondaryAxis      = new Vector3(0, 1, 0);
     xMotion            = ConfigurableJointMotion.Free;
     yMotion            = ConfigurableJointMotion.Free;
     zMotion            = ConfigurableJointMotion.Free;
     angularXMotion     = ConfigurableJointMotion.Free;
     angularYMotion     = ConfigurableJointMotion.Free;
     angularZMotion     = ConfigurableJointMotion.Free;
     xDrive.Reset();
     yDrive.Reset();
     zDrive.Reset();
     angularXDrive.Reset();
     angularYZDrive.Reset();
     slerpDrive.Reset();
     targetRotation = Quaternion.identity;
 }
Beispiel #6
0
        private void CreateJoint()
        {
            joint               = body.gameObject.AddComponent <ConfigurableJoint>();
            joint.axis          = Vector3.up;
            joint.secondaryAxis = Vector3.right;
            joint.xMotion       = ConfigurableJointMotion.Free;
            joint.yMotion       = ConfigurableJointMotion.Free;
            joint.zMotion       = ConfigurableJointMotion.Free;
            ConfigurableJoint       configurableJoint       = joint;
            ConfigurableJointMotion configurableJointMotion = ConfigurableJointMotion.Free;

            joint.angularZMotion               = configurableJointMotion;
            configurableJointMotion            = configurableJointMotion;
            joint.angularYMotion               = configurableJointMotion;
            configurableJoint.angularXMotion   = configurableJointMotion;
            joint.autoConfigureConnectedAnchor = false;
            joint.anchor = body.transform.InverseTransformPoint(base.transform.position);
            joint.xDrive = new JointDrive
            {
                positionSpring = parameters.posSpringX,
                positionDamper = parameters.posDampX,
                maximumForce   = float.PositiveInfinity
            };
            joint.yDrive = new JointDrive
            {
                positionSpring = parameters.posSpringY,
                positionDamper = parameters.posDampY,
                maximumForce   = float.PositiveInfinity
            };
            current = base.transform.position;
        }
Beispiel #7
0
 public BasePoint Motionable(bool x, bool y, bool z)
 {
     motionable   = true;
     this.xMotion = (ConfigurableJointMotion)(2 * Convert.ToInt16(x));
     this.yMotion = (ConfigurableJointMotion)(2 * Convert.ToInt16(y));
     this.zMotion = (ConfigurableJointMotion)(2 * Convert.ToInt16(z));
     return(this);
 }
Beispiel #8
0
 protected static void setAllAxies(ConfigurableJoint joint, ConfigurableJointMotion motion)
 {
     joint.xMotion        = motion;
     joint.yMotion        = motion;
     joint.zMotion        = motion;
     joint.angularXMotion = motion;
     joint.angularYMotion = motion;
     joint.angularZMotion = motion;
 }
        public void Get(ConfigurableJoint configurableJoint)
        {
            motionX = configurableJoint.xMotion;
            motionY = configurableJoint.yMotion;
            motionZ = configurableJoint.zMotion;

            angularMotionX = configurableJoint.angularXMotion;
            angularMotionY = configurableJoint.angularYMotion;
            angularMotionZ = configurableJoint.angularZMotion;
        }
Beispiel #10
0
    void IReset.ResetState(int checkpoint, int subObjectives)
    {
        ConfigurableJoint       configurableJoint       = joint;
        ConfigurableJointMotion configurableJointMotion = ConfigurableJointMotion.Locked;

        joint.xMotion             = configurableJointMotion;
        configurableJointMotion   = configurableJointMotion;
        joint.yMotion             = configurableJointMotion;
        configurableJoint.zMotion = configurableJointMotion;
        disconnected.SetValue(0f);
        targetVelocity = 0f;
    }
Beispiel #11
0
 private void DisconnectJoint()
 {
     if (!(disconnected.value > 0f))
     {
         disconnected.SetValue(1f);
         ConfigurableJoint       configurableJoint       = joint;
         ConfigurableJointMotion configurableJointMotion = ConfigurableJointMotion.Free;
         joint.xMotion             = configurableJointMotion;
         configurableJointMotion   = configurableJointMotion;
         joint.yMotion             = configurableJointMotion;
         configurableJoint.zMotion = configurableJointMotion;
     }
 }
Beispiel #12
0
    public void AllowHandBallRotation(bool allow)
    {
        ConfigurableJointMotion configurableJointMotion = allow ? ConfigurableJointMotion.Free : ConfigurableJointMotion.Locked;
        ConfigurableJoint       component = partLeftHand.rigidbody.GetComponent <ConfigurableJoint>();

        component.angularXMotion = configurableJointMotion;
        component.angularYMotion = configurableJointMotion;
        component.angularZMotion = configurableJointMotion;
        component = partRightHand.rigidbody.GetComponent <ConfigurableJoint>();
        component.angularXMotion = configurableJointMotion;
        component.angularYMotion = configurableJointMotion;
        component.angularZMotion = configurableJointMotion;
    }
Beispiel #13
0
        /*
         *      enable disable joint limits
         */
        public void EnableJointLimits(bool enabled)
        {
            if (CheckForErroredRagdoll("EnableJointLimits"))
            {
                return;
            }

            ConfigurableJointMotion m = enabled ? ConfigurableJointMotion.Limited : ConfigurableJointMotion.Free;

            for (int i = 0; i < bonesCount; i++)
            {
                allElements[i].EnableJointLimits(m);
            }
        }
Beispiel #14
0
    private void AddAntistretch(HumanSegment seg1, HumanSegment seg2)
    {
        ConfigurableJoint       configurableJoint        = seg1.rigidbody.gameObject.AddComponent <ConfigurableJoint>();
        ConfigurableJointMotion configurableJointMotion2 = configurableJoint.zMotion = ConfigurableJointMotion.Limited;

        configurableJointMotion2      = (configurableJoint.xMotion = (configurableJoint.yMotion = configurableJointMotion2));
        configurableJoint.linearLimit = new SoftJointLimit
        {
            limit = (seg1.transform.position - seg2.transform.position).magnitude
        };
        configurableJoint.autoConfigureConnectedAnchor = false;
        configurableJoint.anchor          = Vector3.zero;
        configurableJoint.connectedBody   = seg2.rigidbody;
        configurableJoint.connectedAnchor = Vector3.zero;
    }
Beispiel #15
0
        protected virtual void Awake()
        {
            if (transform.position != muscle.transform.position)
            {
                Debug.LogError("Prop target position must match exactly with it's muscle's position!", transform);
            }

            xMotion        = muscle.xMotion;
            yMotion        = muscle.yMotion;
            zMotion        = muscle.zMotion;
            angularXMotion = muscle.angularXMotion;
            angularYMotion = muscle.angularYMotion;
            angularZMotion = muscle.angularZMotion;

            colliders = muscle.GetComponentsInChildren <Collider>();
        }
Beispiel #16
0
        public void AttachFixed()
        {
            GameObject gameObject = joint.gameObject;

            UnityEngine.Object.Destroy(joint);
            ConfigurableJoint       configurableJoint        = gameObject.AddComponent <ConfigurableJoint>();
            ConfigurableJointMotion configurableJointMotion2 = configurableJoint.zMotion = ConfigurableJointMotion.Locked;

            configurableJointMotion2        = (configurableJoint.xMotion = (configurableJoint.yMotion = configurableJointMotion2));
            configurableJointMotion2        = (configurableJoint.angularZMotion = ConfigurableJointMotion.Free);
            configurableJointMotion2        = (configurableJoint.angularXMotion = (configurableJoint.angularYMotion = configurableJointMotion2));
            configurableJoint.anchor        = Vector3.zero;
            configurableJoint.connectedBody = body.body;
            configurableJoint.autoConfigureConnectedAnchor = false;
            configurableJoint.connectedAnchor = body.transform.InverseTransformPoint(col.transform.position);
            configurableJoint.breakForce      = 15000f;
            configurableJoint.breakTorque     = 10000f;
            isFixed = true;
            joint   = configurableJoint;
        }
Beispiel #17
0
        public void ConversionSystems_WhenGOHasConfigurableJoint_AngularMotionLockOrLimited_AxesAreConstrained(
            ConfigurableJointMotion angularXMotion, ConfigurableJointMotion angularYMotion, ConfigurableJointMotion angularZMotion,
            bool expectedConstrainedX, bool expectedConstrainedY, bool expectedConstrainedZ
            )
        {
            CreateHierarchy(new[] { typeof(LegacyConfigurable) }, Array.Empty <Type>(), Array.Empty <Type>());
            var joint = Root.GetComponent <LegacyConfigurable>();

            joint.xMotion        = ConfigurableJointMotion.Free;
            joint.yMotion        = ConfigurableJointMotion.Free;
            joint.zMotion        = ConfigurableJointMotion.Free;
            joint.angularXMotion = angularXMotion;
            joint.angularYMotion = angularYMotion;
            joint.angularZMotion = angularZMotion;

            TestConvertedData <PhysicsJoint>(j =>
            {
                Assume.That(j.GetConstraints().Length, Is.EqualTo(1), "ConfigurableJoint with limits on only one axis should produce exactly 1 constraint");
                var expectedConstrainedAxes = new bool3(expectedConstrainedX, expectedConstrainedY, expectedConstrainedZ);
                Assert.That(j[0].ConstrainedAxes, Is.EqualTo(expectedConstrainedAxes));
            });
        }
Beispiel #18
0
        void Start()
        {
            if (transform.position != muscle.transform.position)
            {
                Debug.LogError("Prop target position must match exactly with it's muscle's position!", transform);
            }

            xMotion        = muscle.xMotion;
            yMotion        = muscle.yMotion;
            zMotion        = muscle.zMotion;
            angularXMotion = muscle.angularXMotion;
            angularYMotion = muscle.angularYMotion;
            angularZMotion = muscle.angularZMotion;

            colliders = muscle.GetComponentsInChildren <Collider>();

            if (!isPickedUp)
            {
                ReleaseJoint();
            }

            OnStart();
        }
Beispiel #19
0
        protected Joint CreateLimitJoint(float min, float max)
        {
            ConfigurableJoint configurableJoint = body.gameObject.AddComponent <ConfigurableJoint>();

            configurableJoint.anchor = joint.anchor;
            configurableJoint.axis   = joint.axis;
            configurableJoint.autoConfigureConnectedAnchor = false;
            configurableJoint.connectedBody   = joint.connectedBody;
            configurableJoint.connectedAnchor = joint.connectedAnchor;
            ConfigurableJointMotion configurableJointMotion2 = configurableJoint.zMotion = ConfigurableJointMotion.Free;

            configurableJointMotion2           = (configurableJoint.xMotion = (configurableJoint.yMotion = configurableJointMotion2));
            configurableJointMotion2           = (configurableJoint.angularYMotion = (configurableJoint.angularZMotion = ConfigurableJointMotion.Free));
            configurableJoint.angularXMotion   = ConfigurableJointMotion.Limited;
            configurableJoint.lowAngularXLimit = new SoftJointLimit
            {
                limit = 0f - max
            };
            configurableJoint.highAngularXLimit = new SoftJointLimit
            {
                limit = 0f - min
            };
            return(configurableJoint);
        }
Beispiel #20
0
        public JointAngularLimitHandle()
        {
            this.m_XMinHandle = new ArcHandle();
            this.m_XMaxHandle = new ArcHandle();
            this.m_YMinHandle = new ArcHandle();
            this.m_YMaxHandle = new ArcHandle();
            this.m_ZMinHandle = new ArcHandle();
            this.m_ZMaxHandle = new ArcHandle();
            ConfigurableJointMotion configurableJointMotion = ConfigurableJointMotion.Limited;

            this.zMotion            = configurableJointMotion;
            configurableJointMotion = configurableJointMotion;
            this.yMotion            = configurableJointMotion;
            this.xMotion            = configurableJointMotion;
            this.radius             = 1f;
            this.fillAlpha          = 0.1f;
            this.wireframeAlpha     = 1f;
            Vector2 vector = new Vector2(-180f, 180f);

            this.zRange = vector;
            vector      = vector;
            this.yRange = vector;
            this.xRange = vector;
        }
Beispiel #21
0
 /// <summary>
 /// Sets all translation motion properties on the joint.
 /// </summary>
 /// <param name="joint">Joint.</param>
 /// <param name="motion">Motion.</param>
 public static void SetAllTranslationMotion(this ConfigurableJoint joint, ConfigurableJointMotion motion)
 {
     joint.xMotion = joint.yMotion = joint.zMotion = motion;
 }
Beispiel #22
0
 /// <summary>
 /// Sets all angular motion properties on the joint.
 /// </summary>
 /// <param name="joint">Joint.</param>
 /// <param name="motion">Motion.</param>
 public static void SetAllAngularMotion(this ConfigurableJoint joint, ConfigurableJointMotion motion)
 {
     joint.angularXMotion = joint.angularYMotion = joint.angularZMotion = motion;
 }
Beispiel #23
0
 bool3 GetAxesWithMotionType(
     ConfigurableJointMotion motionType,
     ConfigurableJointMotion x, ConfigurableJointMotion y, ConfigurableJointMotion z
     ) =>
 new bool3(x == motionType, y == motionType, z == motionType);
    //-------------------------------------------------
    private void PhysicsAttach(Hand hand, GrabTypes startingGrabType)
    {
        PhysicsDetach(hand);

        Rigidbody holdingBody  = null;
        Vector3   holdingPoint = Vector3.zero;

        // The hand should grab onto the nearest rigid body
        float closestDistance = float.MaxValue;

        for (int i = 0; i < rigidBodies.Count; i++)
        {
            float distance = Vector3.Distance(rigidBodies[i].worldCenterOfMass, hand.transform.position);
            if (distance < closestDistance)
            {
                holdingBody     = rigidBodies[i];
                closestDistance = distance;
            }
        }

        // Couldn't grab onto a body
        if (holdingBody == null)
        {
            return;
        }

        Rigidbody handRigidbody = Util.FindOrAddComponent <Rigidbody>(hand.gameObject);

        handRigidbody.isKinematic = true;

        ConfigurableJoint handJoint = hand.gameObject.AddComponent <ConfigurableJoint>();

        handJoint.connectedBody = holdingBody;
        handJoint.xMotion       = handJoint.yMotion = handJoint.zMotion = ConfigurableJointMotion.Locked;

        // Don't let the hand interact with other things while it's holding us
        hand.HoverLock(null);

        // Affix this point
        Vector3 offset = hand.transform.position - holdingBody.worldCenterOfMass;

        offset = Mathf.Min(offset.magnitude, 1.0f) * offset.normalized;

        Collider col = holdingBody.GetComponentInChildren <Collider>();

        Vector3 closestToMesh = Physics.ClosestPoint(holdingBody.worldCenterOfMass + offset, col, col.transform.position, col.transform.rotation);

        holdingPoint = holdingBody.transform.InverseTransformPoint(closestToMesh);

        //if not already holding, move object to meet hand
        if (true)//if(holdingHands.Count == 0)
        {
            Vector3 moveVector = hand.transform.position - closestToMesh;
            //print(moveVector);
            holdingBody.transform.position += moveVector;
        }

        // Create a configurable joint from the hand to the holding body
        ConfigurableJointMotion _m = ConfigurableJointMotion.Limited;

        handJoint.angularXMotion = handJoint.angularYMotion = handJoint.angularZMotion = _m;

        SoftJointLimit _l = new SoftJointLimit
        {
            limit = 50f
        };

        handJoint.lowAngularXLimit = handJoint.angularYLimit = handJoint.angularZLimit = _l;

        hand.AttachObject(this.gameObject, startingGrabType, m_attachmentFlags);

        // Update holding list
        holdingHands.Add(hand);
        holdingBodies.Add(holdingBody);
        holdingPoints.Add(holdingPoint);
        holdingJoints.Add(handJoint);
    }
Beispiel #25
0
        protected static void CreateJoint(CreateJointParams p)
        {
            Vector3 axis = GetLocalOrthoDirection(p.rigidbody.transform, p.worldSwingAxis);

            Vector3 twistAxis = Vector3.forward;

            if (p.child != null)
            {
                twistAxis = GetLocalOrthoDirection(p.rigidbody.transform, p.child.position - p.rigidbody.transform.position);
            }
            else if (p.connectedBody != null)
            {
                twistAxis = GetLocalOrthoDirection(p.rigidbody.transform, p.rigidbody.transform.position - p.connectedBody.transform.position);
            }

            Vector3 secondaryAxis = Vector3.Cross(axis, twistAxis);

            if (p.type == JointType.Configurable)
            {
                ConfigurableJoint j = p.rigidbody.gameObject.AddComponent <ConfigurableJoint>();
                j.connectedBody = p.connectedBody;

                ConfigurableJointMotion linearMotion  = p.connectedBody != null? ConfigurableJointMotion.Locked: ConfigurableJointMotion.Free;
                ConfigurableJointMotion angularMotion = p.connectedBody != null? ConfigurableJointMotion.Limited: ConfigurableJointMotion.Free;

                j.xMotion = linearMotion;
                j.yMotion = linearMotion;
                j.zMotion = linearMotion;

                j.angularXMotion = angularMotion;
                j.angularYMotion = angularMotion;
                j.angularZMotion = angularMotion;

                if (p.connectedBody != null)
                {
                    j.axis          = axis;
                    j.secondaryAxis = secondaryAxis;

                    j.lowAngularXLimit  = ToSoftJointLimit(p.limits.minSwing);
                    j.highAngularXLimit = ToSoftJointLimit(p.limits.maxSwing);
                    j.angularYLimit     = ToSoftJointLimit(p.limits.swing2);
                    j.angularZLimit     = ToSoftJointLimit(p.limits.twist);
                }

                j.anchor = Vector3.zero;
            }
            else
            {
                if (p.connectedBody == null)
                {
                    return;
                }
                CharacterJoint j = p.rigidbody.gameObject.AddComponent <CharacterJoint>();
                j.connectedBody = p.connectedBody;

                j.axis      = axis;
                j.swingAxis = secondaryAxis;

                j.lowTwistLimit  = ToSoftJointLimit(p.limits.minSwing);
                j.highTwistLimit = ToSoftJointLimit(p.limits.maxSwing);
                j.swing1Limit    = ToSoftJointLimit(p.limits.swing2);
                j.swing2Limit    = ToSoftJointLimit(p.limits.twist);

                j.anchor = Vector3.zero;
            }
        }
Beispiel #26
0
        public void DrawHandle()
        {
            this.m_SecondaryAxesMatrix = Handles.matrix;
            this.xHandleColor          = this.xHandleColor;
            this.yHandleColor          = this.yHandleColor;
            this.zHandleColor          = this.zHandleColor;
            ArcHandle arg_49_0 = this.m_XMinHandle;
            Color     clear    = Color.clear;

            this.m_XMinHandle.wireframeColor = clear;
            arg_49_0.fillColor = clear;
            ArcHandle arg_67_0 = this.m_XMaxHandle;

            clear = Color.clear;
            this.m_XMaxHandle.wireframeColor = clear;
            arg_67_0.fillColor = clear;
            ArcHandle arg_85_0 = this.m_YMinHandle;

            clear = Color.clear;
            this.m_YMinHandle.wireframeColor = clear;
            arg_85_0.fillColor = clear;
            ArcHandle arg_A3_0 = this.m_YMaxHandle;

            clear = Color.clear;
            this.m_YMaxHandle.wireframeColor = clear;
            arg_A3_0.fillColor = clear;
            ArcHandle arg_C1_0 = this.m_ZMinHandle;

            clear = Color.clear;
            this.m_ZMinHandle.wireframeColor = clear;
            arg_C1_0.fillColor = clear;
            ArcHandle arg_DF_0 = this.m_ZMaxHandle;

            clear = Color.clear;
            this.m_ZMaxHandle.wireframeColor = clear;
            arg_DF_0.fillColor = clear;
            Color b     = new Color(1f, 1f, 1f, this.fillAlpha);
            bool  flag  = false;
            bool  flag2 = false;
            bool  flag3 = false;
            ConfigurableJointMotion xMotion = this.xMotion;

            if (xMotion != ConfigurableJointMotion.Free)
            {
                if (xMotion != ConfigurableJointMotion.Limited)
                {
                    if (xMotion == ConfigurableJointMotion.Locked)
                    {
                        using (new Handles.DrawingScope(Handles.color * Color.Lerp(this.xHandleColor, JointAngularLimitHandle.s_LockedColor, JointAngularLimitHandle.s_LockedColorAmount)))
                        {
                            Handles.DrawWireDisc(Vector3.zero, Vector3.right, this.radius);
                        }
                    }
                }
                else
                {
                    flag = true;
                    this.m_SecondaryAxesMatrix *= Matrix4x4.TRS(Vector3.zero, Quaternion.AngleAxis((this.xMin + this.xMax) * 0.5f, Vector3.left), Vector3.one);
                    if (this.yMotion == ConfigurableJointMotion.Limited)
                    {
                        this.DrawMultiaxialFillShape();
                    }
                    else
                    {
                        using (new Handles.DrawingScope(Handles.matrix * JointAngularLimitHandle.s_XHandleOffset))
                        {
                            this.DrawArc(this.m_XMinHandle, this.m_XMaxHandle, this.xHandleColor * b, JointAngularLimitHandle.ArcType.Solid);
                        }
                    }
                }
            }
            else
            {
                using (new Handles.DrawingScope(Handles.color * this.xHandleColor))
                {
                    Handles.DrawWireDisc(Vector3.zero, Vector3.right, this.radius);
                    Handles.color *= b;
                    Handles.DrawSolidDisc(Vector3.zero, Vector3.right, this.radius);
                }
            }
            using (new Handles.DrawingScope(this.m_SecondaryAxesMatrix))
            {
                ConfigurableJointMotion yMotion = this.yMotion;
                if (yMotion != ConfigurableJointMotion.Free)
                {
                    if (yMotion != ConfigurableJointMotion.Limited)
                    {
                        if (yMotion == ConfigurableJointMotion.Locked)
                        {
                            using (new Handles.DrawingScope(Handles.color * Color.Lerp(this.yHandleColor, JointAngularLimitHandle.s_LockedColor, JointAngularLimitHandle.s_LockedColorAmount)))
                            {
                                Handles.DrawWireDisc(Vector3.zero, Vector3.up, this.radius);
                            }
                        }
                    }
                    else
                    {
                        flag2 = true;
                        if (this.xMotion != ConfigurableJointMotion.Limited)
                        {
                            this.DrawArc(this.m_YMinHandle, this.m_YMaxHandle, this.yHandleColor * b, JointAngularLimitHandle.ArcType.Solid);
                        }
                    }
                }
                else
                {
                    using (new Handles.DrawingScope(Handles.color * this.yHandleColor))
                    {
                        Handles.DrawWireDisc(Vector3.zero, Vector3.up, this.radius);
                        Handles.color *= b;
                        Handles.DrawSolidDisc(Vector3.zero, Vector3.up, this.radius);
                    }
                }
                ConfigurableJointMotion zMotion = this.zMotion;
                if (zMotion != ConfigurableJointMotion.Free)
                {
                    if (zMotion != ConfigurableJointMotion.Limited)
                    {
                        if (zMotion == ConfigurableJointMotion.Locked)
                        {
                            using (new Handles.DrawingScope(Handles.color * Color.Lerp(this.zHandleColor, JointAngularLimitHandle.s_LockedColor, JointAngularLimitHandle.s_LockedColorAmount)))
                            {
                                Handles.DrawWireDisc(Vector3.zero, Vector3.forward, this.radius);
                            }
                        }
                    }
                    else
                    {
                        using (new Handles.DrawingScope(Handles.matrix * JointAngularLimitHandle.s_ZHandleOffset))
                        {
                            this.DrawArc(this.m_ZMinHandle, this.m_ZMaxHandle, this.zHandleColor * b, JointAngularLimitHandle.ArcType.Solid);
                        }
                        flag3 = true;
                    }
                }
                else
                {
                    using (new Handles.DrawingScope(Handles.color * this.zHandleColor))
                    {
                        Handles.DrawWireDisc(Vector3.zero, Vector3.forward, this.radius);
                        Handles.color *= b;
                        Handles.DrawSolidDisc(Vector3.zero, Vector3.forward, this.radius);
                    }
                }
            }
            this.m_HandleFunctionDistances.Clear();
            this.m_XMinHandle.GetControlIDs();
            this.m_XMaxHandle.GetControlIDs();
            this.m_YMinHandle.GetControlIDs();
            this.m_YMaxHandle.GetControlIDs();
            this.m_ZMinHandle.GetControlIDs();
            this.m_ZMaxHandle.GetControlIDs();
            if (flag)
            {
                using (new Handles.DrawingScope(Handles.matrix * JointAngularLimitHandle.s_XHandleOffset))
                {
                    this.DrawArc(this.m_XMinHandle, this.m_XMaxHandle, this.xHandleColor, JointAngularLimitHandle.ArcType.Wire);
                    this.m_HandleFunctionDistances.Add(new KeyValuePair <Action, float>(new Action(this.DrawXMinHandle), JointAngularLimitHandle.GetSortingDistance(this.m_XMinHandle)));
                    this.m_HandleFunctionDistances.Add(new KeyValuePair <Action, float>(new Action(this.DrawXMaxHandle), JointAngularLimitHandle.GetSortingDistance(this.m_XMaxHandle)));
                }
            }
            using (new Handles.DrawingScope(this.m_SecondaryAxesMatrix))
            {
                if (flag2)
                {
                    this.DrawArc(this.m_YMinHandle, this.m_YMaxHandle, this.yHandleColor, JointAngularLimitHandle.ArcType.Wire);
                    this.m_HandleFunctionDistances.Add(new KeyValuePair <Action, float>(new Action(this.DrawYMinHandle), JointAngularLimitHandle.GetSortingDistance(this.m_YMinHandle)));
                    this.m_HandleFunctionDistances.Add(new KeyValuePair <Action, float>(new Action(this.DrawYMaxHandle), JointAngularLimitHandle.GetSortingDistance(this.m_YMaxHandle)));
                }
                if (flag3)
                {
                    using (new Handles.DrawingScope(Handles.matrix * JointAngularLimitHandle.s_ZHandleOffset))
                    {
                        this.DrawArc(this.m_ZMinHandle, this.m_ZMaxHandle, this.zHandleColor, JointAngularLimitHandle.ArcType.Wire);
                        this.m_HandleFunctionDistances.Add(new KeyValuePair <Action, float>(new Action(this.DrawZMinHandle), JointAngularLimitHandle.GetSortingDistance(this.m_ZMinHandle)));
                        this.m_HandleFunctionDistances.Add(new KeyValuePair <Action, float>(new Action(this.DrawZMaxHandle), JointAngularLimitHandle.GetSortingDistance(this.m_ZMaxHandle)));
                    }
                }
            }
            List <KeyValuePair <Action, float> > arg_72E_0 = this.m_HandleFunctionDistances;

            if (JointAngularLimitHandle.< > f__mg$cache0 == null)
            {
                JointAngularLimitHandle.< > f__mg$cache0 = new Comparison <KeyValuePair <Action, float> >(JointAngularLimitHandle.CompareHandleFunctionsByDistance);
            }
            arg_72E_0.Sort(JointAngularLimitHandle.< > f__mg$cache0);
            foreach (KeyValuePair <Action, float> current in this.m_HandleFunctionDistances)
            {
                current.Key();
            }
        }
Beispiel #27
0
 static void SetPositionLock(ConfigurableJoint joint, ConfigurableJointMotion motion)
 {
     joint.xMotion = joint.yMotion = joint.zMotion = motion;
 }
Beispiel #28
0
        // Initiate this Muscle
        public virtual void Initiate(Muscle[] colleagues)
        {
            initiated = false;
            if (!IsValid(true))
            {
                return;
            }

            name = joint.name;

            state = State.Default;

            if (joint.connectedBody != null)
            {
                for (int i = 0; i < colleagues.Length; i++)
                {
                    if (colleagues[i].joint.GetComponent <Rigidbody>() == joint.connectedBody)
                    {
                        connectedBodyTarget = colleagues[i].target;
                    }
                }
            }


            transform = joint.transform;

            rigidbody             = transform.GetComponent <Rigidbody>();
            rigidbody.isKinematic = false;

            UpdateColliders();
            if (_colliders.Length == 0)
            {
                Vector3 size     = Vector3.one * 0.1f;
                var     renderer = transform.GetComponent <Renderer>();
                if (renderer != null)
                {
                    size = renderer.bounds.size;
                }

                rigidbody.inertiaTensor = CalculateInertiaTensorCuboid(size, rigidbody.mass);
            }

            targetParent = connectedBodyTarget != null? connectedBodyTarget: target.parent;

            rebuildConnectedBody  = joint.connectedBody;
            rebuildTargetParent   = target.parent;
            rebuildParent         = joint.transform.parent;
            rebuildPosition       = joint.transform.position;
            rebuildRotation       = joint.transform.rotation;
            rebuildTargetPosition = target.position;
            rebuildTargetRotation = target.rotation;
            rebuildAngularXMotion = joint.angularXMotion;
            rebuildAngularYMotion = joint.angularYMotion;
            rebuildAngularZMotion = joint.angularZMotion;

            defaultLocalRotation = localRotation;

            // Joint space
            Vector3 forward = Vector3.Cross(joint.axis, joint.secondaryAxis).normalized;
            Vector3 up      = Vector3.Cross(forward, joint.axis).normalized;

            if (forward == up)
            {
                Debug.LogError("Joint " + joint.name + " secondaryAxis is in the exact same direction as it's axis. Please make sure they are not aligned.");
                return;
            }

            rotationRelativeToTarget = Quaternion.Inverse(target.rotation) * transform.rotation;

            Quaternion toJointSpace = Quaternion.LookRotation(forward, up);

            toJointSpaceInverse = Quaternion.Inverse(toJointSpace);
            toJointSpaceDefault = defaultLocalRotation * toJointSpace;

            toParentSpace = Quaternion.Inverse(targetParentRotation) * parentRotation;

            localRotationConvert = Quaternion.Inverse(targetLocalRotation) * localRotation;

            // Anchoring
            if (joint.connectedBody != null)
            {
                joint.autoConfigureConnectedAnchor = false;
                connectedBodyTransform             = joint.connectedBody.transform;

                directTargetParent = target.parent == connectedBodyTarget;
            }

            // Default angular motions and limits
            angularXMotionDefault = joint.angularXMotion;
            angularYMotionDefault = joint.angularYMotion;
            angularZMotionDefault = joint.angularZMotion;

            // Mapping
            if (joint.connectedBody == null)
            {
                props.mapPosition = true;
            }
            targetRotationRelative = Quaternion.Inverse(rigidbody.rotation) * target.rotation;

            // Resetting
            if (joint.connectedBody == null)
            {
                defaultPosition = transform.localPosition;
                defaultRotation = transform.localRotation;
            }
            else
            {
                defaultPosition = joint.connectedBody.transform.InverseTransformPoint(transform.position);
                defaultRotation = Quaternion.Inverse(joint.connectedBody.transform.rotation) * transform.rotation;
            }

            // Fix target Transforms
            defaultTargetLocalPosition = target.localPosition;
            defaultTargetLocalRotation = target.localRotation;

            // Set necessary joint params
            joint.rotationDriveMode = RotationDriveMode.Slerp;

            if (!joint.gameObject.activeInHierarchy)
            {
                Debug.LogError("Can not initiate a puppet that has deactivated muscles.", joint.transform);
                return;
            }

            joint.configuredInWorldSpace = false;
#if UNITY_5_2
            slerpDrive.mode = JointDriveMode.PositionAndVelocity;
#endif
            joint.projectionMode = JointProjectionMode.None;             //Other projection modes will cause sliding

            if (joint.anchor != Vector3.zero)
            {
                Debug.LogError("PuppetMaster joint anchors need to be Vector3.zero. Joint axis on " + transform.name + " is " + joint.anchor, transform);
                return;
            }

            //rigidbody.maxDepenetrationVelocity = 1f;

            targetAnimatedPosition      = target.position;
            targetAnimatedWorldRotation = target.rotation;
            targetAnimatedRotation      = targetLocalRotation * localRotationConvert;

            Read();
            lastReadTime       = Time.time;
            lastWriteTime      = Time.time;
            lastMappedPosition = target.position;
            lastMappedRotation = target.rotation;

            initiated = true;
        }
Beispiel #29
0
        private void Initialize()
        {
            if (showDebug)
            {
                Debug.Log(base.name + " OnEnable ");
            }
            if (GetComponent <NetIdentity>() == null)
            {
                Debug.LogError("Missing NetIdentity", this);
            }
            Vector3[] handlePositions = GetHandlePositions();
            if (handlePositions.Length < 2)
            {
                return;
            }
            Vector3 vector  = handlePositions[0];
            Vector3 vector2 = handlePositions[handlePositions.Length - 1];
            float   num     = 0f;

            for (int i = 0; i < handlePositions.Length - 1; i++)
            {
                num += (handlePositions[i] - handlePositions[i + 1]).magnitude;
            }
            num              *= lengthMultiplier;
            boneLen           = num / (float)rigidSegments;
            bones             = new Transform[rigidSegments];
            boneSleep         = new NetBodySleep[rigidSegments];
            boneRight         = new Vector3[rigidSegments];
            boneUp            = new Vector3[rigidSegments];
            originalPositions = new Vector3[bones.Length];
            boneForward       = new Vector3[rigidSegments];
            Vector3    a        = (vector2 - vector) / rigidSegments;
            Quaternion rotation = Quaternion.LookRotation(a.normalized);
            Vector3    zero     = Vector3.zero;

            for (int j = 0; j < rigidSegments; j++)
            {
                zero = vector + a * (0.5f + (float)j);
                GameObject gameObject = base.gameObject;
                gameObject = new GameObject("bone" + j);
                gameObject.transform.SetParent(base.transform, worldPositionStays: true);
                originalPositions[j]          = zero;
                gameObject.transform.position = zero;
                gameObject.transform.rotation = rotation;
                bones[j]         = gameObject.transform;
                gameObject.tag   = "Target";
                gameObject.layer = 14;
                Rigidbody rigidbody = gameObject.AddComponent <Rigidbody>();
                rigidbody.mass        = segmentMass;
                rigidbody.drag        = 0.1f;
                rigidbody.angularDrag = 0.1f;
                CapsuleCollider capsuleCollider = gameObject.AddComponent <CapsuleCollider>();
                capsuleCollider.direction      = 2;
                capsuleCollider.radius         = radius;
                capsuleCollider.height         = boneLen + radius * 2f;
                capsuleCollider.sharedMaterial = ropeMaterial;
                if (j != 0)
                {
                    ConfigurableJoint configurableJoint = gameObject.AddComponent <ConfigurableJoint>();
                    configurableJoint.connectedBody = bones[j - 1].GetComponent <Rigidbody>();
                    ConfigurableJointMotion configurableJointMotion2 = configurableJoint.zMotion = ConfigurableJointMotion.Locked;
                    configurableJointMotion2 = (configurableJoint.xMotion = (configurableJoint.yMotion = configurableJointMotion2));
                    configurableJointMotion2 = (configurableJoint.angularZMotion = ConfigurableJointMotion.Limited);
                    configurableJointMotion2 = (configurableJoint.angularXMotion = (configurableJoint.angularYMotion = configurableJointMotion2));
                    configurableJoint.angularXLimitSpring = new SoftJointLimitSpring
                    {
                        spring = 100f,
                        damper = 10f
                    };
                    configurableJoint.angularYZLimitSpring = new SoftJointLimitSpring
                    {
                        spring = 100f,
                        damper = 10f
                    };
                    configurableJoint.lowAngularXLimit = new SoftJointLimit
                    {
                        limit = -20f
                    };
                    configurableJoint.highAngularXLimit = new SoftJointLimit
                    {
                        limit = 20f
                    };
                    configurableJoint.angularYLimit = new SoftJointLimit
                    {
                        limit = 20f
                    };
                    configurableJoint.angularZLimit = new SoftJointLimit
                    {
                        limit = 20f
                    };
                    configurableJoint.angularXDrive = new JointDrive
                    {
                        positionSpring = 50f
                    };
                    configurableJoint.angularYZDrive = new JointDrive
                    {
                        positionSpring = 50f
                    };
                    configurableJoint.axis          = new Vector3(0f, 0f, 1f);
                    configurableJoint.secondaryAxis = new Vector3(1f, 0f, 0f);
                    configurableJoint.autoConfigureConnectedAnchor = false;
                    configurableJoint.anchor             = new Vector3(0f, 0f, (0f - boneLen) / 2f);
                    configurableJoint.connectedAnchor    = new Vector3(0f, 0f, boneLen / 2f);
                    configurableJoint.projectionMode     = JointProjectionMode.PositionAndRotation;
                    configurableJoint.projectionDistance = 0.02f;
                }
                boneSleep[j] = new NetBodySleep(rigidbody);
            }
            float num2 = (0f - boneLen) / 2f / lengthMultiplier;
            int   num3 = -1;

            zero = Vector3.zero;
            Vector3 vector3 = Vector3.zero;

            for (int k = 0; k < rigidSegments; k++)
            {
                Vector3 vector4;
                for (; num2 <= 0f; num2 += vector4.magnitude)
                {
                    num3++;
                    vector4  = handlePositions[num3 + 1] - handlePositions[num3];
                    vector3  = vector4.normalized;
                    rotation = Quaternion.LookRotation(vector3);
                    zero     = handlePositions[num3] - num2 * vector3;
                }
                bones[k].transform.position = zero;
                bones[k].transform.rotation = rotation;
                zero += vector3 * boneLen / lengthMultiplier;
                num2 -= boneLen / lengthMultiplier;
            }
            if (fixStart && bones != null && bones.Length > 0)
            {
                if (showDebug)
                {
                    Debug.Log(base.name + " Fix Start ");
                }
                ConfigurableJoint       configurableJoint2       = bones[0].gameObject.AddComponent <ConfigurableJoint>();
                ConfigurableJointMotion configurableJointMotion2 = configurableJoint2.zMotion = ConfigurableJointMotion.Locked;
                configurableJointMotion2              = (configurableJoint2.xMotion = (configurableJoint2.yMotion = configurableJointMotion2));
                configurableJoint2.projectionMode     = JointProjectionMode.PositionAndRotation;
                configurableJoint2.projectionDistance = 0.02f;
                if (fixStartDir)
                {
                    configurableJointMotion2 = (configurableJoint2.angularZMotion = ConfigurableJointMotion.Locked);
                    configurableJointMotion2 = (configurableJoint2.angularXMotion = (configurableJoint2.angularYMotion = configurableJointMotion2));
                }
                configurableJoint2.anchor = new Vector3(0f, 0f, (0f - boneLen) / 2f);
                configurableJoint2.autoConfigureConnectedAnchor = false;
                if (startBody != null)
                {
                    configurableJoint2.connectedBody   = startBody;
                    configurableJoint2.connectedAnchor = startBody.transform.InverseTransformPoint(vector);
                }
                else
                {
                    configurableJoint2.connectedAnchor = vector;
                }
            }
            if (fixEnd)
            {
                if (showDebug)
                {
                    Debug.Log(base.name + " Fix End ");
                }
                ConfigurableJoint       configurableJoint3       = bones[bones.Length - 1].gameObject.AddComponent <ConfigurableJoint>();
                ConfigurableJointMotion configurableJointMotion2 = configurableJoint3.zMotion = ConfigurableJointMotion.Locked;
                configurableJointMotion2              = (configurableJoint3.xMotion = (configurableJoint3.yMotion = configurableJointMotion2));
                configurableJoint3.projectionMode     = JointProjectionMode.PositionAndRotation;
                configurableJoint3.projectionDistance = 0.02f;
                if (fixEndDir)
                {
                    configurableJointMotion2 = (configurableJoint3.angularZMotion = ConfigurableJointMotion.Locked);
                    configurableJointMotion2 = (configurableJoint3.angularXMotion = (configurableJoint3.angularYMotion = configurableJointMotion2));
                }
                configurableJoint3.anchor = new Vector3(0f, 0f, boneLen / 2f);
                configurableJoint3.autoConfigureConnectedAnchor = false;
                if (endBody != null)
                {
                    configurableJoint3.connectedBody   = endBody;
                    configurableJoint3.connectedAnchor = endBody.transform.InverseTransformPoint(vector2);
                }
                else
                {
                    configurableJoint3.connectedAnchor = vector2;
                }
            }
            if (bonePos == null || bonePos.Length != rigidSegments)
            {
                bonePos = new Vector3[rigidSegments];
                boneRot = new Vector3[rigidSegments];
            }
            else
            {
                for (int l = 0; l < rigidSegments; l++)
                {
                    bones[l].transform.position = base.transform.TransformPoint(bonePos[l]);
                    bones[l].transform.rotation = base.transform.rotation * Quaternion.Euler(boneRot[l]);
                }
            }
            base.OnEnable();
            originalState = NetStream.AllocStream();
            CollectState(originalState);
        }
Beispiel #30
0
 static void SetRotationLock(ConfigurableJoint joint, ConfigurableJointMotion motion)
 {
     joint.angularXMotion = joint.angularYMotion = joint.angularZMotion = motion;
 }