Ejemplo n.º 1
0
        protected override bool Initialize()
        {
            if (Native != null)
            {
                return(true);
            }

            RigidBody rb = Parent != null?Parent.GetInitializedComponentInParent <RigidBody>() : null;

            agx.Vec3 position = rb != null && Type == Cable.NodeType.BodyFixedNode ?
                                CalculateLocalPosition(rb.gameObject).ToHandedVec3() :
                                Position.ToHandedVec3();

            agx.Quat rotation = rb != null && Type == Cable.NodeType.BodyFixedNode ?
                                CalculateLocalRotation(rb.gameObject).ToHandedQuat() :
                                Rotation.ToHandedQuat();

            if (Type == Cable.NodeType.BodyFixedNode)
            {
                Native = new agxCable.BodyFixedNode(rb != null ? rb.Native : null, new agx.AffineMatrix4x4(rotation, position));
            }
            else if (Type == Cable.NodeType.FreeNode)
            {
                Native = new agxCable.FreeNode(position);
                Native.getRigidBody().setRotation(Rotation.ToHandedQuat());
            }
            else
            {
                return(false);
            }

            foreach (var attachment in m_attachments)
            {
                var attachmentRb       = attachment.Parent?.GetInitializedComponentInParent <RigidBody>();
                var attachmentPosition = attachmentRb != null?CalculateLocalPosition(attachmentRb.gameObject).ToHandedVec3() : attachment.Position.ToHandedVec3();

                var attachmentRotation = attachmentRb != null?CalculateLocalRotation(attachmentRb.gameObject).ToHandedQuat() : attachment.Rotation.ToHandedQuat();

                agxCable.SegmentAttachment nativeAttachment = null;
                if (attachment.Type == CableAttachment.AttachmentType.Ball)
                {
                    nativeAttachment = new agxCable.PointSegmentAttachment(attachmentRb?.Native, attachmentPosition);
                }
                else if (attachment.Type == CableAttachment.AttachmentType.Rigid)
                {
                    nativeAttachment = new agxCable.RigidSegmentAttachment(attachmentRb?.Native, new agx.AffineMatrix4x4(attachmentRotation, attachmentPosition));
                }

                if (nativeAttachment == null)
                {
                    Debug.LogWarning("Unknown cable node attachment type. Ignored attachment.");
                }
                else
                {
                    Native.add(nativeAttachment);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates native instance given current properties.
        /// </summary>
        /// <returns>Native instance of this node.</returns>
        protected override bool Initialize()
        {
            RigidBody rb = null;

            Collide.Shape shape = null;
            if (Parent != null)
            {
                rb    = Parent.GetInitializedComponentInParent <RigidBody>();
                shape = Parent.GetInitializedComponentInParent <Collide.Shape>();
            }

            // We don't know if the parent is the rigid body.
            // It could be a mesh, or some other object.
            // Also - use world position if Type == FreeNode.
            agx.Vec3 point = rb != null && Type != Wire.NodeType.FreeNode ?
                             CalculateLocalPosition(rb.gameObject).ToHandedVec3() :
                             Position.ToHandedVec3();

            agx.RigidBody nativeRb = rb != null ? rb.Native : null;
            if (Type == Wire.NodeType.BodyFixedNode)
            {
                Native = new agxWire.BodyFixedNode(nativeRb, point);
            }
            // Create a free node if type is contact and shape == null.
            else if (Type == Wire.NodeType.FreeNode || (Type == Wire.NodeType.ContactNode && shape == null))
            {
                Native = new agxWire.FreeNode(point);
            }
            else if (Type == Wire.NodeType.ConnectingNode)
            {
                Native = new agxWire.ConnectingNode(nativeRb, point, double.PositiveInfinity);
            }
            else if (Type == Wire.NodeType.EyeNode)
            {
                Native = new agxWire.EyeNode(nativeRb, point);
            }
            else if (Type == Wire.NodeType.ContactNode)
            {
                Native = new agxWire.ContactNode(shape.NativeGeometry, CalculateLocalPosition(shape.gameObject).ToHandedVec3());
            }
            else if (Type == Wire.NodeType.WinchNode)
            {
                if (m_winch == null)
                {
                    throw new AGXUnity.Exception("No reference to a wire winch component in the winch node.");
                }

                m_winch.GetInitialized <WireWinch>();

                Native = m_winch.Native != null?m_winch.Native.getStopNode() : null;
            }

            return(Native != null);
        }
Ejemplo n.º 3
0
        private void ShowForces(EventType eventType)
        {
            if (m_textLabelStyle == null)
            {
                m_textLabelStyle           = new GUIStyle(GUI.Skin.label);
                m_textLabelStyle.alignment = TextAnchor.MiddleLeft;

                var fonts = Font.GetOSInstalledFontNames();
                foreach (var font in fonts)
                {
                    if (font == "Consolas")
                    {
                        m_textLabelStyle.font = Font.CreateDynamicFontFromOSFont(font, 24);
                        break;
                    }
                }
            }

            var textColor  = Color.Lerp(Color.black, Color.white, 1.0f);
            var valueColor = Color.Lerp(Color.green, Color.white, 0.45f);
            Func <string, agx.Vec3, GUIContent> Vec3Content = (name, v) =>
            {
                return(GUI.MakeLabel(string.Format("{0} [{1}, {2}, {3}] kN",
                                                   GUI.AddColorTag(name, textColor),
                                                   GUI.AddColorTag(v.x.ToString("0.00").PadLeft(7, ' '), valueColor),
                                                   GUI.AddColorTag(v.y.ToString("0.00").PadLeft(7, ' '), valueColor),
                                                   GUI.AddColorTag(v.z.ToString("0.00").PadLeft(7, ' '), valueColor))));
            };

            var shovel            = m_shovels[0].Native;
            var penetrationForce  = new agx.Vec3();
            var penetrationTorque = new agx.Vec3();

            Native.getPenetrationForce(shovel, ref penetrationForce, ref penetrationTorque);
            var separationForce = -Native.getSeparationContactForce(shovel);
            var deformerForce   = -Native.getDeformationContactForce(shovel);
            var contactForce    = -Native.getContactForce(shovel);

            GUILayout.Label(Vec3Content("Penetration force:", 1.0E-3 * penetrationForce), m_textLabelStyle);
            GUILayout.Space(4);
            GUILayout.Label(Vec3Content("Separation force: ", 1.0E-3 * separationForce), m_textLabelStyle);
            GUILayout.Space(4);
            GUILayout.Label(Vec3Content("Deformer force:   ", 1.0E-3 * deformerForce), m_textLabelStyle);
            GUILayout.Space(4);
            GUILayout.Label(Vec3Content("Contact force:    ", 1.0E-3 * contactForce), m_textLabelStyle);
        }
Ejemplo n.º 4
0
 /*-----------------------------------------------Mathematical operations----------------------------------------------*/
 /**----------------------------------------From agx.Vec3 to UnityEngine.Vector3---------------------------------------*/
 public static Vector3 FromAgxVec3(agx.Vec3 vec3)
 {
     return(new Vector3((double)vec3.x, (double)vec3.y, (double)vec3.z));
 }