Esempio n. 1
0
    void Update()
    {
        float dt = Time.deltaTime;

        bodies.ForEach(body => body.Step(dt));
        bodies.ForEach(body => Integrator.ExplicitEuler(body, dt));

        bodies.ForEach(body => body.force        = Vector2.zero);
        bodies.ForEach(body => body.acceleration = Vector2.zero);
    }
Esempio n. 2
0
    void Update()
    {
        float dt = Time.deltaTime;

        fps           = 1.0f / dt;
        fpsAverage    = (fpsAverage * smoothing) + (fps * (1.0f - smoothing));
        fpsText.value = "FPS: " + fpsAverage.ToString("F1");

        if (!simulate.value)
        {
            return;
        }

        GravitationalForce.ApplyForce(bodies, gravitation.value);
        springs.ForEach(spring => spring.ApplyForce());

        timeAccumulator += dt;
        while (timeAccumulator > fixedDeltaTime)
        {
            bodies.ForEach(body => body.Step(fixedDeltaTime));
            bodies.ForEach(body => Integrator.ExplicitEuler(body, fixedDeltaTime));

            bodies.ForEach(body => body.shape.color = Color.white);

            if (collision == true)
            {
                Collision.CreateContacts(bodies, out List <Contact> contacts);
                contacts.ForEach(contact => { contact.bodyA.shape.color = Color.red; contact.bodyB.shape.color = Color.red; });
                ContactSolver.Resolve(contacts);
            }

            timeAccumulator = timeAccumulator - fixedDeltaTime;
        }

        if (wrap)
        {
            bodies.ForEach(body => body.position = Utilities.Wrap(body.position, -size, size));
        }

        bodies.ForEach(body => body.Step(dt));
        bodies.ForEach(body => Integrator.SemiImplicitEuler(body, dt));

        bodies.ForEach(body => body.force        = Vector2.zero);
        bodies.ForEach(body => body.acceleration = Vector2.zero);
    }