| 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.01; | |
| 9 | 8 |
| 10 abstract class System { | 9 abstract class System { |
| 11 void update(double deltaT); | 10 void update(double deltaT); |
| 12 } | 11 } |
| 13 | 12 |
| 14 class Particle extends System { | 13 class Particle extends System { |
| 15 final double mass; | 14 final double mass; |
| 16 double velocity; | 15 double velocity; |
| 17 double position; | 16 double position; |
| 18 | 17 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 this.particle, | 124 this.particle, |
| 126 this.box, | 125 this.box, |
| 127 this.slope, | 126 this.slope, |
| 128 double targetPosition}) { | 127 double targetPosition}) { |
| 129 double deltaPosition = targetPosition - particle.position; | 128 double deltaPosition = targetPosition - particle.position; |
| 130 particle.energy = -kGravity * slope * deltaPosition * particle.mass; | 129 particle.energy = -kGravity * slope * deltaPosition * particle.mass; |
| 131 box.confine(particle); | 130 box.confine(particle); |
| 132 } | 131 } |
| 133 | 132 |
| 134 void update(double deltaT) { | 133 void update(double deltaT) { |
| 134 particle.update(deltaT); |
| 135 // Note that we apply the impluse from gravity after updating the particle's |
| 136 // position so that we overestimate the distance traveled by the particle. |
| 137 // That ensures that we actually hit the edge of the box and don't wind up |
| 138 // reversing course. |
| 135 particle.applyImpluse(kGravity * slope * deltaT); | 139 particle.applyImpluse(kGravity * slope * deltaT); |
| 136 // If we don't apply a min velocity, error terms in the simulation can | |
| 137 // prevent us from reaching the targetPosition before gravity overtakes our | |
| 138 // initial velocity and we start rolling down the hill. | |
| 139 particle.velocity = math.max(_kMinVelocity, particle.velocity); | |
| 140 particle.update(deltaT); | |
| 141 box.confine(particle); | 140 box.confine(particle); |
| 142 } | 141 } |
| 143 } | 142 } |
| OLD | NEW |