コード例 #1
0
        /// <summary>
        /// Integrates the entire system for a single time step. This method applies forces to objects, performs collision detection, and solves
        /// all constraints. Finally, the object positions are updated.
        /// </summary>
        /// <param name="timeStep">The amount of time, in seconds, to step the simulation forward.</param>
        public void Integrate(float timeStep)
        {
            _dt    = Math.Min(timeStep, _maxTimeStep);
            _dtInv = 1f / _dt;

            _islandAlloc.Recycle(_islands);
            _islands.Clear();
            _contacts.Clear();

            // apply gravity and other forces
            for (int i = 0; i < _generators.Count; i++)
            {
                _generators[i].Generate(_bodies);
            }

            // integrate forces and prepare bodies for collision detection
            for (int i = 0; i < _bodies.Count; i++)
            {
                _bodies[i].Contacts.Clear();
                _bodies[i].IntegrateForce(_dt);
            }

            // detect collisions
            _broadPhase.Execute(_contacts);
            _contacts.PropagateContacts();
            _contacts.ProcessSeparations(this.Bodies);

            // organize all objects into islands and solve each island
            BuildIslands();
            ProcessIslands();

            // cache contact data for the next frame
            if (_isSolverWarmStarted)
            {
                _contacts.PopulateCache(_contactCache);
            }
        }