| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 | 6 |
| 7 const double kGravity = -0.980; | 7 const double kGravity = -0.980; |
| 8 const double _kMinVelocity = 0.1; | 8 const double _kMinVelocity = 0.01; |
| 9 | 9 |
| 10 abstract class System { | 10 abstract class System { |
| 11 void update(double deltaT); | 11 void update(double deltaT); |
| 12 } | 12 } |
| 13 | 13 |
| 14 class Particle extends System { | 14 class Particle extends System { |
| 15 final double mass; | 15 final double mass; |
| 16 double velocity; | 16 double velocity; |
| 17 double position; | 17 double position; |
| 18 | 18 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 ParticleInBox({this.particle, this.box}) { | 62 ParticleInBox({this.particle, this.box}) { |
| 63 box.confine(particle); | 63 box.confine(particle); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void update(double deltaT) { | 66 void update(double deltaT) { |
| 67 particle.update(deltaT); | 67 particle.update(deltaT); |
| 68 box.confine(particle); | 68 box.confine(particle); |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 | 71 |
| 72 class ParticleInBoxWithFriction extends ParticleInBox { |
| 73 final double friction; |
| 74 final double _sign; |
| 75 |
| 76 ParticleInBoxWithFriction({Particle particle, Box box, this.friction}) |
| 77 : super(particle: particle, box: box), |
| 78 _sign = particle.velocity.sign; |
| 79 |
| 80 void update(double deltaT) { |
| 81 double force = -_sign * friction; |
| 82 particle.applyImpluse(force * deltaT); |
| 83 if (particle.velocity.sign != _sign) |
| 84 particle.velocity = 0.0; |
| 85 super.update(deltaT); |
| 86 } |
| 87 } |
| 88 |
| 72 class Spring { | 89 class Spring { |
| 73 final double k; | 90 final double k; |
| 74 double displacement; | 91 double displacement; |
| 75 | 92 |
| 76 Spring(this.k, {this.displacement: 0.0}); | 93 Spring(this.k, {this.displacement: 0.0}); |
| 77 | 94 |
| 78 double get force => -k * displacement; | 95 double get force => -k * displacement; |
| 79 } | 96 } |
| 80 | 97 |
| 81 class ParticleAndSpringInBox extends System { | 98 class ParticleAndSpringInBox extends System { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 void update(double deltaT) { | 134 void update(double deltaT) { |
| 118 particle.applyImpluse(kGravity * slope * deltaT); | 135 particle.applyImpluse(kGravity * slope * deltaT); |
| 119 // If we don't apply a min velocity, error terms in the simulation can | 136 // If we don't apply a min velocity, error terms in the simulation can |
| 120 // prevent us from reaching the targetPosition before gravity overtakes our | 137 // prevent us from reaching the targetPosition before gravity overtakes our |
| 121 // initial velocity and we start rolling down the hill. | 138 // initial velocity and we start rolling down the hill. |
| 122 particle.velocity = math.max(_kMinVelocity, particle.velocity); | 139 particle.velocity = math.max(_kMinVelocity, particle.velocity); |
| 123 particle.update(deltaT); | 140 particle.update(deltaT); |
| 124 box.confine(particle); | 141 box.confine(particle); |
| 125 } | 142 } |
| 126 } | 143 } |
| OLD | NEW |