void Update() { //Debug.Log(1.0f / Time.deltaTime); if (!simulate) { return; } //Another way to do fps float dt = Time.deltaTime; fps = (1.0f / dt); fpsAverage = (fpsAverage * smoothing) + (fps * (1.0f - smoothing)); fpsText.value = "FPS: " + fpsAverage.ToString("F1"); //Debug.Log(fps); timeAccumulator += Time.deltaTime; GravitationalForce.ApplyForce(bodies, gravitation.value); //bodies.ForEach(body => body.shape.color = Color.red); while (timeAccumulator >= fixedDeltaTime) { bodies.ForEach(body => body.Step(fixedDeltaTime)); bodies.ForEach(body => Integrator.ImplicitEuler(body, fixedDeltaTime)); bodies.ForEach(body => body.shape.color = Color.cyan); if (collision) { 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.force = Vector2.zero); bodies.ForEach(body => body.acceleration = Vector2.zero); }
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); }
void Update() { Debug.Log($"system fps:{1.0f / Time.deltaTime}"); Debug.Log($"fixed fps:{1.0f / fixedDT}"); fpsText.value = $"FPS: {(1.0f / Time.deltaTime).ToString("F1")}"; if (!simulate) { return; } float dt = Time.deltaTime; timeAccumulator += dt; GravitationalForce.ApplyForce(bodies, gravitation); springs.ForEach(spring => spring.applyForce()); while (timeAccumulator >= fixedDT) { bodies.ForEach(body => body.step(fixedDT)); bodies.ForEach(body => Integrator.semiImplicitEuler(body, fixedDT)); bodies.ForEach(body => body.shape.color = Color.red); if (collision) { Collision.createContacts(bodies, out List <Contact> contacts); ContactResolver.resolve(contacts); contacts.ForEach(contact => contact.bodyA.shape.color = Color.cyan); contacts.ForEach(contact => contact.bodyB.shape.color = Color.cyan); } if (wrap) { bodies.ForEach(body => body.position = Utilities.wrap(body.position, -size, size)); } timeAccumulator -= fixedDT; } bodies.ForEach(body => body.force = Vector2.zero); bodies.ForEach(body => body.acceleration = Vector2.zero); }
void Update() { springs.ForEach(spring => spring.Draw()); if (!simulate.value) { return; } fps = 1.0f / Time.deltaTime; fpsAverage = (fpsAverage * smoothing) + (fps * (1.0f - smoothing)); fpsText.value = string.Format("FPS: {0:F}", fpsAverage); GravitationalForce.ApplyForce(bodies, gravitation.value); forces.ForEach(force => bodies.ForEach(body => force.ApplyForce(body))); springs.ForEach(spring => spring.ApplyForce()); timeAccumulator += Time.deltaTime; while (timeAccumulator > fixedDeltaTime) { bodies.ForEach(body => body.Step(fixedDeltaTime)); bodies.ForEach(body => Integrator.SemiImplicitEuler(body, fixedDeltaTime)); bodies.ForEach(body => body.shape.color = Color.white); if (collision) { 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 -= fixedDeltaTime; } if (wrap) { bodies.ForEach(body => body.position = Utilities.Wrap(body.position, -size, size)); } bodies.ForEach(body => body.force = Vector2.zero); bodies.ForEach(body => body.acceleration = Vector2.zero); }