Esempio n. 1
0
        /// <summary>
        /// Patches primary and secondary elementary constraints to the current version of AGX.
        /// </summary>
        /// <param name="native">If given, the native configuration may be used.</param>
        /// <returns>True if modification were applied - otherwise false.</returns>
        public bool VerifyImplementation()
        {
            if (Type == ConstraintType.Hinge)
            {
                var swing = m_elementaryConstraints.FirstOrDefault(ec => ec.NativeName == "SW");
                // Already created with swing - hinge is up to date.
                if (swing != null)
                {
                    return(false);
                }

                var ecUn = m_elementaryConstraints.FirstOrDefault(ec => ec.NativeName == "D1_UN");
                var ecVn = m_elementaryConstraints.FirstOrDefault(ec => ec.NativeName == "D1_VN");
                // Not swing nor dot1's - this is an unknown configuration.
                if (ecUn == null || ecVn == null)
                {
                    Debug.LogWarning("Trying to patch hinge but the elementary constraint configuration is undefined.", this);
                    return(false);
                }

                using (var nativeHinge = new TemporaryNative(NativeType)) {
                    swing = ElementaryConstraint.Create(gameObject, nativeHinge.Instance.getElementaryConstraintGivenName("SW"));
                }

                if (swing == null)
                {
                    Debug.LogWarning("Unable to find elementary constraint \"SW\" in native hinge implementation.", this);
                    return(false);
                }

                swing.Enable = ecUn.Enable || ecVn.Enable;
                swing.RowData[0].CopyFrom(ecUn.RowData[0]);
                swing.RowData[1].CopyFrom(ecVn.RowData[0]);

                m_elementaryConstraints.Insert(m_elementaryConstraints.IndexOf(ecUn), swing);
                m_elementaryConstraints.Remove(ecUn);
                m_elementaryConstraints.Remove(ecVn);

                DestroyImmediate(ecUn);
                DestroyImmediate(ecVn);

                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Internal method which constructs this constraint given elementary constraints
        /// in the native instance. Throws if an elementary constraint fails to initialize.
        /// </summary>
        /// <param name="native">Native instance.</param>
        /// <param name="onObjectCreated">Optional callback when elementary constraint has been created.</param>
        public void TryAddElementaryConstraints(agx.Constraint native,
                                                Action <UnityEngine.Object> onObjectCreated = null)
        {
            if (native == null)
            {
                throw new ArgumentNullException("native", "Native constraint is null.");
            }

            m_elementaryConstraints.Clear();

            for (uint i = 0; i < native.getNumElementaryConstraints(); ++i)
            {
                if (native.getElementaryConstraint(i).getName() == "")
                {
                    throw new Exception("Native elementary constraint doesn't have a name.");
                }

                var ec = ElementaryConstraint.Create(gameObject, native.getElementaryConstraint(i));
                if (ec == null)
                {
                    throw new Exception("Failed to configure elementary constraint with name: " + native.getElementaryConstraint(i).getName() + ".");
                }

                onObjectCreated?.Invoke(ec);

                m_elementaryConstraints.Add(ec);
            }

            for (uint i = 0; i < native.getNumSecondaryConstraints(); ++i)
            {
                if (native.getSecondaryConstraint(i).getName() == "")
                {
                    throw new Exception("Native secondary constraint doesn't have a name.");
                }

                var sc = ElementaryConstraint.Create(gameObject, native.getSecondaryConstraint(i));
                if (sc == null)
                {
                    throw new Exception("Failed to configure elementary controller constraint with name: " + native.getElementaryConstraint(i).getName() + ".");
                }

                onObjectCreated?.Invoke(sc);

                m_elementaryConstraints.Add(sc);
            }
        }