Exemplo n.º 1
0
 private Node GetOrCreateAssembly(agx.Frame frame)
 {
     return(GetOrCreateNode(NodeType.Assembly,
                            frame.getUuid(),
                            true,
                            () => m_assemblies.Add(frame.getUuid(), frame)));
 }
Exemplo n.º 2
0
        private void SynchronizeNativeFramesWithAttachmentPair()
        {
            // NOTE: It's not possible to update the constraint frames given the current
            //       transforms since the actual constraint direction will change with the
            //       violation.
            //RigidBody rb1 = AttachmentPair.ReferenceObject.GetComponentInParent<RigidBody>();
            //if ( rb1 == null )
            //  return;
            //
            //agx.Frame f1 = Native.getAttachment( 0 ).getFrame();
            //f1.setLocalTranslate( AttachmentPair.ReferenceFrame.CalculateLocalPosition( rb1.gameObject ).ToHandedVec3() );
            //f1.setLocalRotate( AttachmentPair.ReferenceFrame.CalculateLocalRotation( rb1.gameObject ).ToHandedQuat() );

            if (ConnectedFrameNativeSyncEnabled)
            {
                RigidBody rb2 = AttachmentPair.ConnectedObject != null?AttachmentPair.ConnectedObject.GetComponentInParent <RigidBody>() : null;

                agx.Frame f2 = Native.getAttachment(1).getFrame();

                if (rb2 != null)
                {
                    f2.setLocalTranslate(AttachmentPair.ConnectedFrame.CalculateLocalPosition(rb2.gameObject).ToHandedVec3());
                    f2.setLocalRotate(AttachmentPair.ConnectedFrame.CalculateLocalRotation(rb2.gameObject).ToHandedQuat());
                }
                else
                {
                    f2.setLocalTranslate(AttachmentPair.ConnectedFrame.Position.ToHandedVec3());
                    f2.setLocalRotate(AttachmentPair.ConnectedFrame.Rotation.ToHandedQuat());
                }
            }
        }
Exemplo n.º 3
0
        private Node TryGetOrCreateAssembly(agx.Frame child)
        {
            agx.Frame parent = child?.getParent();
            // If parent has a rigid body 'child' is probably a geometry.
            if (parent == null || parent.getRigidBody() != null)
            {
                return(null);
            }

            return(GetOrCreateAssembly(parent));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create native friction model given solve type and friction model type.
        /// </summary>
        /// <param name="type">Friction model type.</param>
        /// <param name="solveType">Solve type.</param>
        /// <returns>New native instance.</returns>
        public agx.FrictionModel CreateNative(EType type,
                                              ESolveType solveType)
        {
            agx.Frame referenceFrame = null;
            if (m_orientedFrictionReferenceObject != null)
            {
                referenceFrame = m_orientedFrictionReferenceObject.GetComponent <RigidBody>() != null?
                                 m_orientedFrictionReferenceObject.GetComponent <RigidBody>().Native.getFrame() :
                                     m_orientedFrictionReferenceObject.GetComponent <Collide.Shape>() != null?
                                     m_orientedFrictionReferenceObject.GetComponent <Collide.Shape>().NativeGeometry.getFrame() :
                                         null;
            }

            agx.FrictionModel frictionModel = null;
            if (type == EType.IterativeProjectedFriction)
            {
                frictionModel = referenceFrame != null ?
                                new agx.OrientedIterativeProjectedConeFrictionModel(referenceFrame,
                                                                                    Convert(m_orientedFrictionPrimaryDirection).ToHandedVec3(),
                                                                                    Convert(solveType)) :
                                new agx.IterativeProjectedConeFriction(Convert(solveType));
            }
            else if (type == EType.ScaleBoxFriction)
            {
                frictionModel = referenceFrame != null ?
                                new agx.OrientedScaleBoxFrictionModel(referenceFrame,
                                                                      Convert(m_orientedFrictionPrimaryDirection).ToHandedVec3(),
                                                                      Convert(solveType)) :
                                new agx.ScaleBoxFrictionModel(Convert(solveType));
            }
            else if (type == EType.BoxFriction)
            {
                frictionModel = referenceFrame != null ?
                                new agx.OrientedBoxFrictionModel(referenceFrame,
                                                                 Convert(m_orientedFrictionPrimaryDirection).ToHandedVec3(),
                                                                 Convert(solveType)) :
                                new agx.BoxFrictionModel(Convert(solveType));
            }
            else if (type == EType.ConstantNormalForceBoxFriction)
            {
                frictionModel = new agx.ConstantNormalForceOrientedBoxFrictionModel(NormalForceMagnitude,
                                                                                    referenceFrame,
                                                                                    Convert(m_orientedFrictionPrimaryDirection).ToHandedVec3(),
                                                                                    Convert(solveType),
                                                                                    ScaleNormalForceWithDepth);
            }

            return(frictionModel);
        }
Exemplo n.º 5
0
    private void Gripper(bool grip)
    {
        if (GrippingBody == null || FollowBody == null || waitForKeyUp)
        {
            return;
        }

        if (grip)
        {
            GrippedBody.SyncNativeTransform();
            GrippingBody.SyncNativeTransform(); // Is this needed...?
            if (Vector3.Distance(GrippedBody.transform.position, GrippingBody.transform.position) > MaxGripDistance)
            {
                Debug.Log("Couldn't reach to grip: " + (GrippedBody.transform.position - GrippingBody.transform.position).magnitude + ", pos1: " + GrippedBody.transform.position + ", pos2: " + GrippingBody.transform.position);
                return;
            }

            if (lockJoint == null)
            {
                previouslyClose = false;

                agx.Frame grippedFrame  = new agx.Frame();
                agx.Frame grippingFrame = new agx.Frame();

                Vector3 offset = GrippingBody.transform.rotation * grippingPositionOffset;
                grippedFrame.setLocalTranslate(GrippedBody.Native.getCmLocalTranslate());
                grippingFrame.setLocalTranslate(new agx.Vec3(offset.x, offset.y, offset.z));
                grippedFrame.setLocalRotate(new agx.EulerAngles(grippingRotationOffset.x * Mathf.Deg2Rad, grippingRotationOffset.y * Mathf.Deg2Rad, grippingRotationOffset.z * Mathf.Deg2Rad));

                lockJoint = new agx.LockJoint(GrippedBody.Native, grippedFrame, GrippingBody.Native, grippingFrame);
                lockJoint.setCompliance(0.00001f);
                lockJoint.setForceRange(new agx.RangeReal(-200, 200));
                lockJoint.setDamping(1);
                GetSimulation().add(lockJoint);
            }
        }
        else
        {
            if (lockJoint != null)
            {
                lockJoint.setEnable(false);
                lockJoint = null;
                VeryCloseEvent(false);
            }
        }

        gripped = !gripped;
    }
Exemplo n.º 6
0
            public TemporaryNative(Type nativeType, AttachmentPair attachmentPair = null)
            {
                m_rb1 = new agx.RigidBody();
                m_f1  = new agx.Frame();
                m_f2  = new agx.Frame();

                if (attachmentPair != null)
                {
                    // Some constraints, e.g., Distance Joints depends on the constraint angle during
                    // creation so we feed the frames with the world transform of the reference and
                    // connecting frame.
                    attachmentPair.Synchronize();

                    m_f1.setLocalTranslate(attachmentPair.ReferenceFrame.Position.ToHandedVec3());
                    m_f1.setLocalRotate(attachmentPair.ReferenceFrame.Rotation.ToHandedQuat());

                    m_f2.setLocalTranslate(attachmentPair.ConnectedFrame.Position.ToHandedVec3());
                    m_f2.setLocalRotate(attachmentPair.ConnectedFrame.Rotation.ToHandedQuat());
                }

                m_native = (agx.Constraint)Activator.CreateInstance(nativeType, new object[] { m_rb1, m_f1, null, m_f2 });
            }
Exemplo n.º 7
0
        private void Generate(Node node)
        {
            if (node == null)
            {
                return;
            }

            // TODO: Skip if node.GameObject != null means "use Unity configuration".

            switch (node.Type)
            {
            case Node.NodeType.Assembly:
                agx.Frame frame = m_tree.GetAssembly(node.Uuid);
                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName("", node.Type.ToString());

                node.GameObject.transform.position = frame.getTranslate().ToHandedVector3();
                node.GameObject.transform.rotation = frame.getRotate().ToHandedQuaternion();

                node.GameObject.GetOrCreateComponent <Assembly>();

                break;

            case Node.NodeType.RigidBody:
                agx.RigidBody nativeRb = m_tree.GetRigidBody(node.Uuid);
                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeRb.getName(), node.Type.ToString());


                node.GameObject.transform.position = nativeRb.getPosition().ToHandedVector3();
                node.GameObject.transform.rotation = nativeRb.getRotation().ToHandedQuaternion();

                node.GameObject.GetOrCreateComponent <RigidBody>().RestoreLocalDataFrom(nativeRb);


                break;

            case Node.NodeType.Geometry:
                // Ignoring geometries - handling Shape == Geometry.
                // The shapes are children to this node.
                break;

            case Node.NodeType.Shape:
                var nativeGeometry  = m_tree.GetGeometry(node.Parent.Uuid);
                var nativeShape     = m_tree.GetShape(node.Uuid);
                var nativeShapeType = (agxCollide.Shape.Type)nativeShape.getType();

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeGeometry.getName() +
                                                "_" +
                                                nativeShapeType.ToString().ToLower().FirstCharToUpperCase(),
                                                node.Type.ToString());

                node.GameObject.transform.position = nativeShape.getTransform().getTranslate().ToHandedVector3();
                node.GameObject.transform.rotation = nativeShape.getTransform().getRotate().ToHandedQuaternion();

                if (!CreateShape(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Material:
                var nativeMaterial = m_tree.GetMaterial(node.Uuid);
                node.Asset      = ScriptAsset.Create <ShapeMaterial>().RestoreLocalDataFrom(nativeMaterial);
                node.Asset.name = FindName(nativeMaterial.getName(), node.Type.ToString());

                node.Asset = AddOrReplaceAsset(node.Asset as ShapeMaterial, node, AGXUnity.IO.AssetType.ShapeMaterial);

                break;

            case Node.NodeType.ContactMaterial:
                var nativeContactMaterial = m_tree.GetContactMaterial(node.Uuid);
                var nativeFrictionModel   = nativeContactMaterial.getFrictionModel();

                var contactMaterial = ScriptAsset.Create <ContactMaterial>().RestoreLocalDataFrom(nativeContactMaterial);
                contactMaterial.name = FindName("ContactMaterial_" +
                                                nativeContactMaterial.getMaterial1().getName() +
                                                "_" +
                                                nativeContactMaterial.getMaterial2().getName(),
                                                node.Type.ToString());

                var materials = node.GetReferences(Node.NodeType.Material);
                if (materials.Length == 0)
                {
                    Debug.LogWarning("No materials referenced to ContactMaterial node.");
                }
                else if (materials.Length == 1)
                {
                    contactMaterial.Material1 = contactMaterial.Material2 = materials[0].Asset as ShapeMaterial;
                }
                else if (materials.Length > 1)
                {
                    contactMaterial.Material1 = materials[0].Asset as ShapeMaterial;
                    contactMaterial.Material2 = materials[1].Asset as ShapeMaterial;
                    if (materials.Length > 2)
                    {
                        Debug.LogWarning("More than two materials referenced to ContactMaterial (" + node.Asset.name + "). First two are used.");
                    }
                }

                if (nativeFrictionModel != null)
                {
                    var frictionModelAsset = ScriptAsset.Create <FrictionModel>().RestoreLocalDataFrom(nativeFrictionModel);
                    frictionModelAsset.name       = "FrictionModel_" + contactMaterial.name;
                    contactMaterial.FrictionModel = AddOrReplaceAsset(frictionModelAsset, node, AGXUnity.IO.AssetType.FrictionModel);
                }

                node.Asset = contactMaterial = AddOrReplaceAsset(contactMaterial, node, AGXUnity.IO.AssetType.ContactMaterial);

                break;

            case Node.NodeType.Constraint:
                var nativeConstraint = m_tree.GetConstraint(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeConstraint.getName(),
                                                "AGXUnity." + Constraint.FindType(nativeConstraint));

                if (!CreateConstraint(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Wire:
                var nativeWire = m_tree.GetWire(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeWire.getName(), "AGXUnity.Wire");

                if (!CreateWire(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Cable:
                var nativeCable = m_tree.GetCable(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeCable.getName(), "AGXUnity.Cable");

                if (!CreateCable(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;
            }

            foreach (var child in node.Children)
            {
                Generate(child);
            }
        }
Exemplo n.º 8
0
    private void handleKeyboard()
    {
        bool steering_key_down = false;

        // Handle keyboard
        if (Input.GetKey(KeyUp))
        {
            if (!m_motor_key_forward_down)
            {
                m_vehicle.forward();
            }

            m_motor_key_forward_down = true;
        }
        else
        {
            m_motor_key_forward_down = false;
        }

        if (Input.GetKey(KeyDown))
        {
            if (!m_motor_key_backward_down)
            {
                m_vehicle.reverse();
            }

            m_motor_key_backward_down = true;
        }
        else
        {
            m_motor_key_backward_down = false;
        }

        if (Input.GetKey(KeyLeft))
        {
            steering_key_down = true;
            updateForceRange();
            m_steeringHinge.getMotor1D().setSpeed(-SteeringSpeed);
        }

        if (Input.GetKey(KeyRight))
        {
            steering_key_down = true;
            updateForceRange();

            m_steeringHinge.getMotor1D().setSpeed(SteeringSpeed);
        }

        if (!steering_key_down)
        {
            m_steeringHinge.getMotor1D().setSpeed(0);
        }

        if (!m_motor_key_backward_down && !m_motor_key_forward_down)
        {
            m_vehicle.brake(true);
        }

        bool fast = Input.GetKey(KeyCode.LeftShift);

        if (m_motor_key_forward_down && fast)
        {
            m_vehicle.Gearbox.setGear(3);
        }

        if (Input.GetKey(KeyReset) && !m_resetKeyDown)
        {
            m_resetKeyDown = true;
            var pos = m_chassieRigidBody.getPosition();

            m_moveBody = new agx.RigidBody();
            GetSimulation().add(m_moveBody);

            m_moveBody.setMotionControl(agx.RigidBody.MotionControl.KINEMATICS);
            m_moveBody.setPosition(pos + new agx.Vec3(0, 2, 0));
            m_moveBody.setVelocity(0, 2, 0);

            var f1 = new agx.Frame();
            var f2 = new agx.Frame();
            agx.Constraint.calculateFramesFromWorld(pos, new agx.Vec3(0, 1, 0), m_moveBody, f1, m_chassieRigidBody, f2);
            m_moveConstraint = new agx.BallJoint(m_moveBody, f1, m_chassieRigidBody, f2);
            GetSimulation().add(m_moveConstraint);
        }
        else if (m_resetKeyDown)
        {
            GetSimulation().remove(m_moveBody);
            GetSimulation().remove(m_moveConstraint);
            m_resetKeyDown = false;
        }
    }
Exemplo n.º 9
0
        /// <summary>
        /// Creates native instance and adds it to the simulation if this constraint
        /// is properly configured.
        /// </summary>
        /// <returns>True if successful.</returns>
        protected override bool Initialize()
        {
            if (AttachmentPair.ReferenceObject == null)
            {
                Debug.LogError("Unable to initialize constraint - reference object must be valid and contain a rigid body component.", this);
                return(false);
            }

            // Synchronize frames to make sure connected frame is up to date.
            AttachmentPair.Synchronize();

            // TODO: Disabling rigid body game object (activeSelf == false) and will not be
            //       able to create native body (since State == Constructed and not Awake).
            //       Do: GetComponentInParent<RigidBody>( true <- include inactive ) and wait
            //           for the body to become active?
            //       E.g., rb.AwaitInitialize += ThisConstraintInitialize.
            RigidBody rb1 = AttachmentPair.ReferenceObject.GetInitializedComponentInParent <RigidBody>();

            if (rb1 == null)
            {
                Debug.LogError("Unable to initialize constraint - reference object must contain a rigid body component.", AttachmentPair.ReferenceObject);
                return(false);
            }

            // Native constraint frames.
            agx.Frame f1 = new agx.Frame();
            agx.Frame f2 = new agx.Frame();

            // Note that the native constraint want 'f1' given in rigid body frame, and that
            // 'ReferenceFrame' may be relative to any object in the children of the body.
            f1.setLocalTranslate(AttachmentPair.ReferenceFrame.CalculateLocalPosition(rb1.gameObject).ToHandedVec3());
            f1.setLocalRotate(AttachmentPair.ReferenceFrame.CalculateLocalRotation(rb1.gameObject).ToHandedQuat());

            RigidBody rb2 = AttachmentPair.ConnectedObject != null?AttachmentPair.ConnectedObject.GetInitializedComponentInParent <RigidBody>() : null;

            if (rb1 == rb2)
            {
                Debug.LogError("Unable to initialize constraint - reference and connected rigid body is the same instance.", this);
                return(false);
            }

            if (rb2 != null)
            {
                // Note that the native constraint want 'f2' given in rigid body frame, and that
                // 'ReferenceFrame' may be relative to any object in the children of the body.
                f2.setLocalTranslate(AttachmentPair.ConnectedFrame.CalculateLocalPosition(rb2.gameObject).ToHandedVec3());
                f2.setLocalRotate(AttachmentPair.ConnectedFrame.CalculateLocalRotation(rb2.gameObject).ToHandedQuat());
            }
            else
            {
                f2.setLocalTranslate(AttachmentPair.ConnectedFrame.Position.ToHandedVec3());
                f2.setLocalRotate(AttachmentPair.ConnectedFrame.Rotation.ToHandedQuat());
            }

            try {
                Native = (agx.Constraint)Activator.CreateInstance(NativeType, new object[] { rb1.Native, f1, (rb2 != null ? rb2.Native : null), f2 });

                // Assigning native elementary constraints to our elementary constraint instances.
                foreach (ElementaryConstraint ec in ElementaryConstraints)
                {
                    if (!ec.OnConstraintInitialize(this))
                    {
                        throw new Exception("Unable to initialize elementary constraint: " + ec.NativeName + " (not present in native constraint). ConstraintType: " + Type);
                    }
                }

                bool added = GetSimulation().add(Native);
                Native.setEnable(IsEnabled);

                // Not possible to handle collisions if connected frame parent is null/world.
                if (CollisionsState != ECollisionsState.KeepExternalState && AttachmentPair.ConnectedObject != null)
                {
                    string     groupName            = gameObject.name + "_" + gameObject.GetInstanceID().ToString();
                    GameObject go1                  = null;
                    GameObject go2                  = null;
                    bool       propagateToChildren1 = false;
                    bool       propagateToChildren2 = false;
                    if (CollisionsState == ECollisionsState.DisableReferenceVsConnected)
                    {
                        go1 = AttachmentPair.ReferenceObject;
                        go2 = AttachmentPair.ConnectedObject;
                    }
                    else
                    {
                        go1 = rb1.gameObject;
                        propagateToChildren1 = true;
                        go2 = rb2 != null ? rb2.gameObject : AttachmentPair.ConnectedObject;
                        propagateToChildren2 = true;
                    }

                    go1.GetOrCreateComponent <CollisionGroups>().GetInitialized <CollisionGroups>().AddGroup(groupName, propagateToChildren1);
                    go2.GetOrCreateComponent <CollisionGroups>().GetInitialized <CollisionGroups>().AddGroup(groupName, propagateToChildren2);
                    CollisionGroupsManager.Instance.GetInitialized <CollisionGroupsManager>().SetEnablePair(groupName, groupName, false);
                }

                Native.setName(name);

                bool valid = added && Native.getValid();
                Simulation.Instance.StepCallbacks.PreSynchronizeTransforms += OnPreStepForwardUpdate;

                // It's not possible to check which properties an animator
                // is controlling, for now we update all properties in the
                // controllers if we have an animator.
                m_isAnimated = GetComponent <Animator>() != null;

                return(valid);
            }
            catch (System.Exception e) {
                Debug.LogException(e, gameObject);
                return(false);
            }
        }
Exemplo n.º 10
0
        protected override bool Initialize()
        {
            if (!agx.Runtime.instance().isModuleEnabled("AgX-DriveTrain"))
            {
                Debug.LogWarning("WheelLoader requires a valid license for the AGX Dynamics module: AgX-DriveTrain", this);
            }

            PowerLine = new agxPowerLine.PowerLine();
            PowerLine.setName(name);
            Engine          = new agxDriveTrain.CombustionEngine(InletVolume);
            TorqueConverter = new agxDriveTrain.TorqueConverter();
            GearBox         = new agxDriveTrain.GearBox();
            Differentials[(int)DifferentialLocation.Rear]   = new agxDriveTrain.Differential();
            Differentials[(int)DifferentialLocation.Center] = new agxDriveTrain.Differential();
            Differentials[(int)DifferentialLocation.Front]  = new agxDriveTrain.Differential();

            m_actuators[(int)WheelLocation.LeftFront]  = new agxPowerLine.RotationalActuator(LeftFrontHinge.GetInitialized <Constraint>().Native.asHinge());
            m_actuators[(int)WheelLocation.RightFront] = new agxPowerLine.RotationalActuator(RightFrontHinge.GetInitialized <Constraint>().Native.asHinge());
            m_actuators[(int)WheelLocation.LeftRear]   = new agxPowerLine.RotationalActuator(LeftRearHinge.GetInitialized <Constraint>().Native.asHinge());
            m_actuators[(int)WheelLocation.RightRear]  = new agxPowerLine.RotationalActuator(RightRearHinge.GetInitialized <Constraint>().Native.asHinge());

            foreach (var wheelHinge in WheelHinges)
            {
                wheelHinge.GetController <TargetSpeedController>().Enable = false;
            }

            var engineTorqueConverterShaft    = new agxDriveTrain.Shaft();
            var torqueConverterGearBoxShaft   = new agxDriveTrain.Shaft();
            var gearBoxCenterDiffShaft        = new agxDriveTrain.Shaft();
            var centerDiffRearDiffShaft       = new agxDriveTrain.Shaft();
            var centerDiffFrontDiffShaft      = new agxDriveTrain.Shaft();
            var frontDiffFrontLeftWheelShaft  = new agxDriveTrain.Shaft();
            var frontDiffFrontRightWheelShaft = new agxDriveTrain.Shaft();
            var rearDiffRearLeftWheelShaft    = new agxDriveTrain.Shaft();
            var rearDiffRearRightWheelShaft   = new agxDriveTrain.Shaft();

            PowerLine.setSource(Engine);

            Engine.connect(engineTorqueConverterShaft);
            engineTorqueConverterShaft.connect(TorqueConverter);
            TorqueConverter.connect(torqueConverterGearBoxShaft);
            torqueConverterGearBoxShaft.connect(GearBox);
            GearBox.connect(gearBoxCenterDiffShaft);
            gearBoxCenterDiffShaft.connect(Differentials[(int)DifferentialLocation.Center]);

            Differentials[(int)DifferentialLocation.Center].connect(centerDiffFrontDiffShaft);
            centerDiffFrontDiffShaft.connect(Differentials[(int)DifferentialLocation.Front]);
            Differentials[(int)DifferentialLocation.Front].connect(frontDiffFrontLeftWheelShaft);
            Differentials[(int)DifferentialLocation.Front].connect(frontDiffFrontRightWheelShaft);
            frontDiffFrontLeftWheelShaft.connect(m_actuators[(int)WheelLocation.LeftFront]);
            frontDiffFrontRightWheelShaft.connect(m_actuators[(int)WheelLocation.RightFront]);

            Differentials[(int)DifferentialLocation.Center].connect(centerDiffRearDiffShaft);
            centerDiffRearDiffShaft.connect(Differentials[(int)DifferentialLocation.Rear]);
            Differentials[(int)DifferentialLocation.Rear].connect(rearDiffRearLeftWheelShaft);
            Differentials[(int)DifferentialLocation.Rear].connect(rearDiffRearRightWheelShaft);
            rearDiffRearLeftWheelShaft.connect(m_actuators[(int)WheelLocation.LeftRear]);
            rearDiffRearRightWheelShaft.connect(m_actuators[(int)WheelLocation.RightRear]);

            var munu = new agx.RealPairVector(new agx.RealPair[]
            {
                new agx.RealPair(-0.0001, 0.00),
                new agx.RealPair(0.00001, 0.50),
                new agx.RealPair(0.00011, 2.00),
                new agx.RealPair(0.00100, 2.00),
                new agx.RealPair(0.20000, 1.10),
                new agx.RealPair(0.40000, 1.15),
                new agx.RealPair(0.60000, 1.05),
                new agx.RealPair(0.80000, 1.01),
                new agx.RealPair(0.90000, 0.99),
                new agx.RealPair(1.00000, 0.98),
                new agx.RealPair(1.00100, 0.98)
            });

            TorqueConverter.setMuTable(munu);
            TorqueConverter.setMaxMultiplication(2.0);
            TorqueConverter.setPumpTorqueReferenceRPM(1000.0);

            GearBox.setGearRatios(new agx.RealVector(new double[] { GearRatios.x, GearRatios.y }));
            GearBox.gearUp();

            GetSimulation().add(PowerLine);

            var f1 = new agx.Frame();
            var f2 = new agx.Frame();

            agx.Constraint.calculateFramesFromBody(new agx.Vec3(),
                                                   agx.Vec3.X_AXIS(),
                                                   gearBoxCenterDiffShaft.getRotationalDimension().getOrReserveBody(),
                                                   f1,
                                                   null,
                                                   f2);
            BrakeHinge = new agx.Hinge(gearBoxCenterDiffShaft.getRotationalDimension().getOrReserveBody(),
                                       f1,
                                       null,
                                       f2);
            GetSimulation().add(BrakeHinge);

            try {
                GetOrCreateTireModelProperties();
                foreach (WheelLocation location in Enum.GetValues(typeof(WheelLocation)))
                {
                    GetOrCreateTireModel(location)?.GetInitialized <TwoBodyTire>();
                }
            }
            catch (Exception e) {
                Debug.LogWarning("Unable to initialize tire models: " + e.Message);
            }

            return(true);
        }
Exemplo n.º 11
0
        private void Generate(Node node)
        {
            if (node == null)
            {
                return;
            }

            switch (node.Type)
            {
            case Node.NodeType.Assembly:
                agx.Frame frame = m_tree.GetAssembly(node.Uuid);
                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName("", node.Type.ToString());

                node.GameObject.transform.position = frame.getTranslate().ToHandedVector3();
                node.GameObject.transform.rotation = frame.getRotate().ToHandedQuaternion();

                node.GameObject.GetOrCreateComponent <Assembly>();

                break;

            case Node.NodeType.RigidBody:
                agx.RigidBody nativeRb = m_tree.GetRigidBody(node.Uuid);
                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeRb.getName(), node.Type.ToString());


                node.GameObject.transform.position = nativeRb.getPosition().ToHandedVector3();
                node.GameObject.transform.rotation = nativeRb.getRotation().ToHandedQuaternion();

                node.GameObject.GetOrCreateComponent <RigidBody>().RestoreLocalDataFrom(nativeRb);

                break;

            case Node.NodeType.Geometry:
                // Ignoring geometries - handling Shape == Geometry.
                // The shapes are children to this node.
                break;

            case Node.NodeType.Shape:
                var nativeGeometry  = m_tree.GetGeometry(node.Parent.Uuid);
                var nativeShape     = m_tree.GetShape(node.Uuid);
                var nativeShapeType = (agxCollide.Shape.Type)nativeShape.getType();

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeGeometry.getName() +
                                                "_" +
                                                nativeShapeType.ToString().ToLower().FirstCharToUpperCase(),
                                                node.Type.ToString());

                node.GameObject.transform.position = nativeShape.getTransform().getTranslate().ToHandedVector3();
                node.GameObject.transform.rotation = nativeShape.getTransform().getRotate().ToHandedQuaternion();

                if (!CreateShape(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Material:
                // Ignoring materials - the referenced materials should have been restored
                // by now using RestoreShapeMaterial.
                break;

            case Node.NodeType.ContactMaterial:
                var           materialNodes = node.GetReferences(Node.NodeType.Material);
                Func <string> contactMaterialMaterialNames = () =>
                {
                    return(m_tree.GetMaterial(materialNodes[0].Uuid).getName() + " <-> " +
                           m_tree.GetMaterial(materialNodes[1].Uuid).getName());
                };
                if (materialNodes.Length == 0)
                {
                    Debug.LogWarning("No materials referenced to ContactMaterial node - ignoring contact material.");
                    break;
                }
                else if (materialNodes.Length > 2)
                {
                    Debug.LogWarning("More than two materials referenced to contact material - first two will be used: " +
                                     contactMaterialMaterialNames());
                }

                var materials = new ShapeMaterial[2]
                {
                    materialNodes[0].Asset as ShapeMaterial,
                    materialNodes.Length > 1 ?
                    materialNodes[1].Asset as ShapeMaterial :
                    materialNodes[0].Asset as ShapeMaterial
                };

                var nativeContactMaterial = m_tree.GetContactMaterial(node.Uuid);
                var nativeFrictionModel   = nativeContactMaterial.getFrictionModel();
                if (materials[0] == null || materials[1] == null)
                {
                    Debug.LogWarning($"Unreferenced ShapeMaterial(s) in ContactMaterial: {contactMaterialMaterialNames()} - ignoring contact material.");
                    break;
                }
                Predicate <ContactMaterial> materialsMatcher = cm => (cm.Material1 == materials[0] && cm.Material2 == materials[1]) ||
                                                               (cm.Material2 == materials[0] && cm.Material1 == materials[1]);
                var contactMaterial = FileInfo.ObjectDb.GetOrCreateAsset(materialsMatcher,
                                                                         FindName($"ContactMaterial_{materials[ 0 ].name}_{materials[ 1 ].name}",
                                                                                  node.Type.ToString()),
                                                                         cm =>
                {
                    cm.Material1 = materials[0];
                    cm.Material2 = materials[1];
                    cm.RestoreLocalDataFrom(nativeContactMaterial);
                });
                if (nativeFrictionModel != null)
                {
                    contactMaterial.FrictionModel = FileInfo.ObjectDb.GetOrCreateAsset(contactMaterial.FrictionModel,
                                                                                       $"FrictionModel_{contactMaterial.name}",
                                                                                       fm => fm.RestoreLocalDataFrom(nativeFrictionModel));
                }

                node.Asset = contactMaterial;

                break;

            case Node.NodeType.Constraint:
                var nativeConstraint = m_tree.GetConstraint(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeConstraint.getName(),
                                                "AGXUnity." + Constraint.FindType(nativeConstraint));

                if (!CreateConstraint(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Wire:
                var nativeWire = m_tree.GetWire(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeWire.getName(), "AGXUnity.Wire");

                if (!CreateWire(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Cable:
                var nativeCable = m_tree.GetCable(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeCable.getName(), "AGXUnity.Cable");

                if (!CreateCable(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.ObserverFrame:
                var nativeObserverFrame = m_tree.GetObserverFrame(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeObserverFrame.getName(), "AGXUnity.ObserverFrame");

                var rbNode = node.GetReferences(Node.NodeType.RigidBody).FirstOrDefault();
                node.GameObject.GetOrCreateComponent <ObserverFrame>().RestoreLocalDataFrom(nativeObserverFrame,
                                                                                            rbNode != null ? rbNode.GameObject : null);

                break;
            }

            foreach (var child in node.Children)
            {
                Generate(child);
            }
        }
Exemplo n.º 12
0
        protected override bool Initialize()
        {
            RemoveInvalidRollers();

            var tracks = FindTracks();

            if (tracks.Length == 0)
            {
                Debug.LogWarning("Belt: No tracks found.", this);
                return(false);
            }
            if (tracks.Any(track => track.GetInitialized <Track>() == null) ||
                // Tracks has zero nodes when the license isn't loaded or
                // doesn't include the tracks module.
                tracks.Any(Track => Track.Native.getNumNodes() == 0))
            {
                Debug.LogError("Belt: One or more tracks failed to initialize.", this);
                return(false);
            }

            m_tracks = tracks;
            if (m_tracks.Length > 1)
            {
                ulong numNodes = m_tracks.First().Native.getNumNodes();
                ulong stride   = System.Math.Min(System.Math.Max((ulong)ConnectingConstraintStride, 1),
                                                 numNodes);
                while (stride > 1 && (numNodes % stride) != 0)
                {
                    --stride;
                }
                if ((int)stride != ConnectingConstraintStride)
                {
                    Debug.Log($"Belt: Connecting constraint stride changed from {ConnectingConstraintStride} to {stride} " +
                              $"to match node count: {m_tracks.First().Native.getNumNodes()}.", this);
                }

                for (int i = 0; i < m_tracks.Length; ++i)
                {
                    m_tracks[i].Native.addGroup(GetGroupName(i));
                }

                ConnectingConstraints = new agx.Constraint[m_tracks.Length - 1, (int)(numNodes / stride)];

                for (int i = 1; i < m_tracks.Length; ++i)
                {
                    var prev = m_tracks[i - 1];
                    var curr = m_tracks[i];

                    GetSimulation().getSpace().setEnablePair(GetGroupName(i - 1),
                                                             GetGroupName(i),
                                                             false);
                    var connectingConstraintIndex = 0;
                    for (ulong nodeIndex = 0; nodeIndex < prev.Native.getNumNodes(); ++nodeIndex)
                    {
                        if ((nodeIndex % stride) != 0)
                        {
                            continue;
                        }

                        var prevTrackNode    = prev.Native.getNode(nodeIndex);
                        var currTrackNode    = curr.Native.getNode(nodeIndex);
                        var constraintCenter = 0.5 * (prevTrackNode.getCenterPosition() +
                                                      currTrackNode.getCenterPosition());
                        var constraintAxis = prevTrackNode.getDirection().normal();
                        var otherAxis      = (constraintCenter - prevTrackNode.getCenterPosition()).normal();

                        // Constraint frame we're about to create:
                        //   x or TRANSLATIONAL_1: Along otherAxis, i.e., separation of the two nodes.
                        //   y or TRANSLATIONAL_2: Up/down between the two nodes.
                        //   z or TRANSLATIONAL_3: Along the track.

                        var prevTrackNodeFrame = new agx.Frame();
                        var currTrackNodeFrame = new agx.Frame();
                        agx.Constraint.calculateFramesFromWorld(constraintCenter,
                                                                constraintAxis,
                                                                otherAxis,
                                                                prevTrackNode.getRigidBody(),
                                                                prevTrackNodeFrame,
                                                                currTrackNode.getRigidBody(),
                                                                currTrackNodeFrame);
                        ConnectingConstraints[i - 1, connectingConstraintIndex++] = new agx.Hinge(prevTrackNode.getRigidBody(),
                                                                                                  prevTrackNodeFrame,
                                                                                                  currTrackNode.getRigidBody(),
                                                                                                  currTrackNodeFrame);
                    }
                }

                foreach (var constraint in ConnectingConstraints)
                {
                    if (constraint == null)
                    {
                        Debug.LogWarning("Unexpected null connecting constraint.", this);
                        continue;
                    }

                    constraint.setName("connecting");

                    // Properties will be applied during BeltProperties.Register
                    // when all properties are synchronized for this belt after
                    // Initialize is done.
                    GetSimulation().add(constraint);
                }
            }

            return(true);
        }
Exemplo n.º 13
0
        public void InitializeOrientedFriction(bool isOriented,
                                               GameObject referenceObject,
                                               FrictionModel.PrimaryDirection primaryDirection)
        {
            if (!isOriented || referenceObject == null || FrictionModel == null)
            {
                return;
            }

            if (!Application.isPlaying)
            {
                Debug.LogError("Oriented friction: Invalid to initialize oriented friction in edit mode.", this);
                return;
            }

            if (GetInitialized <ContactMaterial>() == null)
            {
                return;
            }

            if (FrictionModel.GetInitialized <FrictionModel>() == null)
            {
                return;
            }

            var rb    = referenceObject.GetComponent <RigidBody>();
            var shape = rb == null?
                        referenceObject.GetComponent <Collide.Shape>() :
                            null;

            var observer = rb == null && shape == null?
                           referenceObject.GetComponent <ObserverFrame>() :
                               null;

            agx.Frame referenceFrame = null;
            if (rb != null && rb.GetInitialized <RigidBody>() != null)
            {
                referenceFrame = rb.Native.getFrame();
            }
            else if (shape != null && shape.GetInitialized <Collide.Shape>() != null)
            {
                referenceFrame = shape.NativeGeometry.getFrame();
            }
            else if (observer != null && observer.GetInitialized <ObserverFrame>() != null)
            {
                referenceFrame = observer.Native.getFrame();
            }

            if (referenceFrame == null)
            {
                Debug.LogWarning($"Oriented friction: Unable to find reference frame from {referenceObject.name}.", referenceObject);
                return;
            }

            if (rb != null)
            {
                FrictionModel.InitializeOriented(rb, primaryDirection);
            }
            else
            {
                FrictionModel.InitializeOriented(shape, primaryDirection);
            }
        }