Chromium Code Reviews| 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; // m^s-2 | 7 const double kGravity = -0.980; // m^s-2 |
| 8 | 8 |
| 9 abstract class System { | 9 abstract class System { |
| 10 void update(double deltaT); | 10 void update(double deltaT); |
| 11 } | 11 } |
| 12 | 12 |
| 13 class Particle extends System { | 13 class Particle extends System { |
| 14 final double mass; | 14 final double mass; |
| 15 double velocity; | 15 double velocity; |
| 16 double position; | 16 double position; |
| 17 | 17 |
| 18 Particle({this.mass: 1.0, this.velocity: 0.0, this.position: 0.0}); | 18 Particle({this.mass: 1.0, this.velocity: 0.0, this.position: 0.0}); |
| 19 | 19 |
| 20 void applyImpulse(double impulse) { | 20 void applyImpulse(double impulse) { |
| 21 velocity += impulse / mass; | 21 velocity += impulse / mass; |
| 22 } | 22 } |
| 23 | 23 |
| 24 void update(double deltaT) { | 24 void update(double deltaT) { |
| 25 position += velocity * deltaT; | 25 position += velocity * deltaT; |
| 26 } | 26 } |
| 27 | 27 |
| 28 double get energy => 0.5 * mass * velocity * velocity; | 28 void setVelocityFromEnergy({double energy, double direction}) { |
|
eseidel
2015/04/21 19:52:33
Why the {}? Doesn't that make them optional named
Hixie
2015/04/21 20:18:52
It makes them named, yes. (They're always "optiona
| |
| 29 set energy(double e) { // J | 29 assert(direction == -1.0 || direction == 1.0); |
| 30 assert(e >= 0.0); | 30 assert(energy >= 0.0); |
| 31 velocity = math.sqrt(2.0 * e / mass); | 31 velocity = math.sqrt(2.0 * energy / mass) * direction; |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 | 34 |
| 35 abstract class Box { | 35 abstract class Box { |
| 36 void confine(Particle p); | 36 void confine(Particle p); |
| 37 } | 37 } |
| 38 | 38 |
| 39 class ClosedBox extends Box { | 39 class ClosedBox extends Box { |
| 40 final double min; // m | 40 final double min; // m |
| 41 final double max; // m | 41 final double max; // m |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 assert(theta > 0.0); | 167 assert(theta > 0.0); |
| 168 assert(theta < math.PI / 2.0); | 168 assert(theta < math.PI / 2.0); |
| 169 double deltaPosition = targetPosition - particle.position; | 169 double deltaPosition = targetPosition - particle.position; |
| 170 double tanTheta = math.tan(theta); | 170 double tanTheta = math.tan(theta); |
| 171 // We need to give the particle exactly as much (kinetic) energy | 171 // We need to give the particle exactly as much (kinetic) energy |
| 172 // as it needs to get to the top of the slope and stop with | 172 // as it needs to get to the top of the slope and stop with |
| 173 // energy=0. This is exactly the same amount of energy as the | 173 // energy=0. This is exactly the same amount of energy as the |
| 174 // potential energy at the top of the slope, which is g*h*m. | 174 // potential energy at the top of the slope, which is g*h*m. |
| 175 // If the slope's horizontal component is delta P long, then | 175 // If the slope's horizontal component is delta P long, then |
| 176 // the height is delta P times tan theta. | 176 // the height is delta P times tan theta. |
| 177 particle.energy = -kGravity * (deltaPosition * tanTheta) * particle.mass; | 177 particle.setVelocityFromEnergy( |
| 178 energy: (kGravity * (deltaPosition * tanTheta) * particle.mass).abs(), | |
| 179 direction: deltaPosition > 0.0 ? 1.0 : -1.0 | |
| 180 ); | |
| 178 box.confine(particle); | 181 box.confine(particle); |
| 179 } | 182 } |
| 180 | 183 |
| 181 void update(double deltaT) { | 184 void update(double deltaT) { |
| 182 particle.update(deltaT); | 185 particle.update(deltaT); |
| 183 // Note that we apply the impulse from gravity after updating the particle's | 186 // Note that we apply the impulse from gravity after updating the particle's |
| 184 // position so that we overestimate the distance traveled by the particle. | 187 // position so that we overestimate the distance traveled by the particle. |
| 185 // That ensures that we actually hit the edge of the box and don't wind up | 188 // That ensures that we actually hit the edge of the box and don't wind up |
| 186 // reversing course. | 189 // reversing course. |
| 187 particle.applyImpulse(particle.mass * kGravity * _sinTheta * deltaT); | 190 particle.applyImpulse(particle.mass * kGravity * _sinTheta * deltaT); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 201 | 204 |
| 202 void update(double deltaT) { | 205 void update(double deltaT) { |
| 203 _currentSystem.update(deltaT); | 206 _currentSystem.update(deltaT); |
| 204 } | 207 } |
| 205 | 208 |
| 206 void transitionToSystem(System system) { | 209 void transitionToSystem(System system) { |
| 207 assert(system != null); | 210 assert(system != null); |
| 208 _currentSystem = system; | 211 _currentSystem = system; |
| 209 } | 212 } |
| 210 } | 213 } |
| OLD | NEW |