/// <summary>
        /// Advances the simulation a given amount of time. Note that once BeginStep has been called,
        /// Substep can be called multiple times.
        /// </summary>
        /// <param name="stepDeltaTime"> Duration (in seconds) of the substep.</param>
        protected void Substep(float substepDeltaTime)
        {
            using (m_SubstepPerfMarker.Auto())
            {
                // Necessary when using multiple substeps:
                ObiColliderBase.UpdateColliders();

                // Grab rigidbody info:
                ObiRigidbodyBase.UpdateAllRigidbodies();

                IntPtr stepSimulation = Oni.CreateEmpty();

                // Generate a task for each solver's step, and combine them all together:
                foreach (ObiSolver solver in solvers)
                {
                    if (solver != null)
                    {
                        Oni.AddChild(stepSimulation, solver.Substep(substepDeltaTime));
                    }
                }

                // Schedule the task for execution:
                Oni.Schedule(stepSimulation);

                // Wait the task to complete:
                Oni.Complete(stepSimulation);

                // Update rigidbody velocities:
                ObiRigidbodyBase.UpdateAllVelocities();
            }
        }
        /// <summary>
        /// Prepares all solvers to begin simulating a new physics step. This involves
        /// caching some particle data for interpolation, performing collision detection, among other things.
        /// </summary>
        /// <param name="stepDeltaTime"> Duration (in seconds) of the next step.</param>
        protected void BeginStep(float stepDeltaTime)
        {
            using (m_BeginStepPerfMarker.Auto())
            {
                // Update colliders right before collision detection:
                ObiColliderBase.UpdateColliders();

                IntPtr beginStep = Oni.CreateEmpty();

                // Generate a task for each solver's collision detection, and combine them all together:
                foreach (ObiSolver solver in solvers)
                {
                    if (solver != null)
                    {
                        Oni.AddChild(beginStep, solver.BeginStep(stepDeltaTime));
                    }
                }

                // Schedule the task for execution:
                Oni.Schedule(beginStep);

                // Wait the task to complete:
                Oni.Complete(beginStep);
            }
        }
Ejemplo n.º 3
0
        private void DisableAttachment(AttachmentType type)
        {
            if (isBound)
            {
                switch (type)
                {
                case AttachmentType.Dynamic:

                    if (pinBatch != null)
                    {
                        var pins = m_Actor.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints <ObiPinConstraintsBatch>;
                        if (pins != null)
                        {
                            pins.RemoveBatch(pinBatch);
                            if (actor.isLoaded)
                            {
                                m_Actor.SetConstraintsDirty(Oni.ConstraintType.Pin);
                            }
                        }

                        attachedCollider            = null;
                        pinBatch                    = null;
                        attachedColliderHandleIndex = -1;
                    }

                    break;

                case AttachmentType.Static:

                    var solver    = m_Actor.solver;
                    var blueprint = m_Actor.sourceBlueprint;

                    for (int i = 0; i < m_SolverIndices.Length; ++i)
                    {
                        int solverIndex = m_SolverIndices[i];
                        if (solverIndex >= 0 && solverIndex < solver.invMasses.count)
                        {
                            solver.invMasses[solverIndex] = blueprint.invMasses[i];
                        }
                    }

                    if (m_Actor.usesOrientedParticles)
                    {
                        for (int i = 0; i < m_SolverIndices.Length; ++i)
                        {
                            int solverIndex = m_SolverIndices[i];
                            if (solverIndex >= 0 && solverIndex < solver.invRotationalMasses.count)
                            {
                                solver.invRotationalMasses[solverIndex] = blueprint.invRotationalMasses[i];
                            }
                        }
                    }

                    m_Actor.UpdateParticleProperties();

                    break;
                }
            }
        }
Ejemplo n.º 4
0
 public void AddConstraint(int index1, ObiColliderBase body, Vector3 offset, float stiffness)
 {
     activeConstraints.Add(constraintCount);
     pinIndices.Add(index1);
     pinBodies.Add(body);
     pinOffsets.Add(offset);
     stiffnesses.Add(stiffness);
     pinBreakResistance.Add(float.MaxValue);
     constraintCount++;
 }
        public void AddConstraint(int index, ObiColliderBase body, Vector3 offset, Quaternion restDarboux)
        {
            RegisterConstraint();

            particleIndices.Add(index);
            pinBodies.Add(body != null ? body.OniCollider : IntPtr.Zero);
            offsets.Add(offset);
            restDarbouxVectors.Add(restDarboux);
            stiffnesses.Add(0);
            stiffnesses.Add(0);
            breakThresholds.Add(float.PositiveInfinity);
        }
        public void AddConstraint(int solverIndex, ObiColliderBase body, Vector3 offset, Quaternion restDarboux, float linearCompliance, float rotationalCompliance, float breakThreshold)
        {
            RegisterConstraint();

            particleIndices.Add(solverIndex);
            pinBodies.Add(body != null ? body.Handle : new ObiColliderHandle());
            colliderIndices.Add(body != null ? body.Handle.index : -1);
            offsets.Add(offset);
            restDarbouxVectors.Add(restDarboux);
            stiffnesses.Add(linearCompliance);
            stiffnesses.Add(rotationalCompliance);
            breakThresholds.Add(breakThreshold);
        }
 public void OnEnable()
 {
     collider = (ObiColliderBase)target;
 }
Ejemplo n.º 8
0
        private void EnableAttachment(AttachmentType type)
        {
            if (enabled && m_Actor.isLoaded && isBound)
            {
                var solver = m_Actor.solver;

                switch (type)
                {
                case AttachmentType.Dynamic:

                    var pins = m_Actor.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiPinConstraintsData;
                    attachedCollider = m_Target.GetComponent <ObiColliderBase>();

                    if (pins != null && attachedCollider != null && pinBatch == null)
                    {
                        // create a new data batch with all our pin constraints:
                        pinBatch = new ObiPinConstraintsBatch(pins);
                        for (int i = 0; i < m_SolverIndices.Length; ++i)
                        {
                            pinBatch.AddConstraint(m_SolverIndices[i],
                                                   attachedCollider,
                                                   m_PositionOffsets[i],
                                                   m_OrientationOffsets[i],
                                                   m_Compliance,
                                                   constrainOrientation ? 0 : 10000,
                                                   m_BreakThreshold);

                            pinBatch.activeConstraintCount++;
                        }

                        // add the batch to the actor:
                        pins.AddBatch(pinBatch);

                        // store the attached collider's handle:
                        attachedColliderHandleIndex = -1;
                        if (attachedCollider.Handle != null)
                        {
                            attachedColliderHandleIndex = attachedCollider.Handle.index;
                        }

                        m_Actor.SetConstraintsDirty(Oni.ConstraintType.Pin);
                    }

                    break;

                case AttachmentType.Static:

                    for (int i = 0; i < m_SolverIndices.Length; ++i)
                    {
                        if (m_SolverIndices[i] >= 0 && m_SolverIndices[i] < solver.invMasses.count)
                        {
                            solver.invMasses[m_SolverIndices[i]] = 0;
                        }
                    }

                    if (m_Actor.usesOrientedParticles && m_ConstrainOrientation)
                    {
                        for (int i = 0; i < m_SolverIndices.Length; ++i)
                        {
                            if (m_SolverIndices[i] >= 0 && m_SolverIndices[i] < solver.invRotationalMasses.count)
                            {
                                solver.invRotationalMasses[m_SolverIndices[i]] = 0;
                            }
                        }
                    }

                    m_Actor.UpdateParticleProperties();

                    break;
                }
            }
        }
        private void Enable(AttachmentType type)
        {
            var solver    = m_Actor.solver;
            var blueprint = m_Actor.blueprint;

            if (isBound && blueprint != null && m_Actor.solver != null)
            {
                switch (type)
                {
                case AttachmentType.Dynamic:

                    var             pins             = m_Actor.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints <ObiPinConstraintsBatch>;
                    ObiColliderBase attachedCollider = m_Target.GetComponent <ObiColliderBase>();

                    if (pins != null && attachedCollider != null)
                    {
                        // create a new data batch with all our pin constraints:
                        pinBatch = new ObiPinConstraintsBatch();
                        for (int i = 0; i < m_PositionOffsets.Length; ++i)
                        {
                            pinBatch.AddConstraint(0, attachedCollider, m_PositionOffsets[i], m_OrientationOffsets[i]);
                            pinBatch.activeConstraintCount++;
                        }

                        // add the batch to the solver:
                        pins.AddBatch(pinBatch);
                        pinBatch.AddToSolver(pins);

                        // override the pin indices with the ones we got at bind time:
                        for (int i = 0; i < m_SolverIndices.Length; ++i)
                        {
                            pinBatch.particleIndices[i]     = m_SolverIndices[i];
                            pinBatch.stiffnesses[i * 2]     = m_Compliance;
                            pinBatch.stiffnesses[i * 2 + 1] = constrainOrientation?0:10000;
                            pinBatch.breakThresholds[i]     = m_BreakThreshold;
                        }

                        // enable the batch:
                        pinBatch.SetEnabled(true);
                    }

                    break;

                case AttachmentType.Static:

                    for (int i = 0; i < m_SolverIndices.Length; ++i)
                    {
                        solver.invMasses[m_SolverIndices[i]] = 0;
                    }

                    if (m_Actor.usesOrientedParticles && m_ConstrainOrientation)
                    {
                        for (int i = 0; i < m_SolverIndices.Length; ++i)
                        {
                            solver.invRotationalMasses[m_SolverIndices[i]] = 0;
                        }
                    }

                    m_Actor.UpdateParticleProperties();

                    break;
                }
            }
        }