| 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 | 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 applyImpluse(double impluse) { | 20 void applyImpulse(double impulse) { |
| 21 velocity += impluse / 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 double get energy => 0.5 * mass * velocity * velocity; |
| 29 set energy(double e) { | 29 set energy(double e) { |
| 30 assert(e >= 0.0); | 30 assert(e >= 0.0); |
| 31 velocity = math.sqrt(2 * e / mass); | 31 velocity = math.sqrt(2 * e / mass); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 class ParticleInBoxWithFriction extends ParticleInBox { | 71 class ParticleInBoxWithFriction extends ParticleInBox { |
| 72 final double friction; | 72 final double friction; |
| 73 final double _sign; | 73 final double _sign; |
| 74 | 74 |
| 75 ParticleInBoxWithFriction({Particle particle, Box box, this.friction}) | 75 ParticleInBoxWithFriction({Particle particle, Box box, this.friction}) |
| 76 : super(particle: particle, box: box), | 76 : super(particle: particle, box: box), |
| 77 _sign = particle.velocity.sign; | 77 _sign = particle.velocity.sign; |
| 78 | 78 |
| 79 void update(double deltaT) { | 79 void update(double deltaT) { |
| 80 double force = -_sign * friction; | 80 double force = -_sign * friction; |
| 81 particle.applyImpluse(force * deltaT); | 81 particle.applyImpulse(force * deltaT); |
| 82 if (particle.velocity.sign != _sign) | 82 if (particle.velocity.sign != _sign) |
| 83 particle.velocity = 0.0; | 83 particle.velocity = 0.0; |
| 84 super.update(deltaT); | 84 super.update(deltaT); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 class Spring { | 88 class Spring { |
| 89 final double k; | 89 final double k; |
| 90 double displacement; | 90 double displacement; |
| 91 | 91 |
| 92 Spring(this.k, {this.displacement: 0.0}); | 92 Spring(this.k, {this.displacement: 0.0}); |
| 93 | 93 |
| 94 double get force => -k * displacement; | 94 double get force => -k * displacement; |
| 95 } | 95 } |
| 96 | 96 |
| 97 class ParticleAndSpringInBox extends System { | 97 class ParticleAndSpringInBox extends System { |
| 98 final Particle particle; | 98 final Particle particle; |
| 99 final Spring spring; | 99 final Spring spring; |
| 100 final Box box; | 100 final Box box; |
| 101 | 101 |
| 102 ParticleAndSpringInBox({this.particle, this.spring, this.box}) { | 102 ParticleAndSpringInBox({this.particle, this.spring, this.box}) { |
| 103 _applyInvariants(); | 103 _applyInvariants(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void update(double deltaT) { | 106 void update(double deltaT) { |
| 107 particle.applyImpluse(spring.force * deltaT); | 107 particle.applyImpulse(spring.force * deltaT); |
| 108 particle.update(deltaT); | 108 particle.update(deltaT); |
| 109 _applyInvariants(); | 109 _applyInvariants(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 void _applyInvariants() { | 112 void _applyInvariants() { |
| 113 box.confine(particle); | 113 box.confine(particle); |
| 114 spring.displacement = particle.position; | 114 spring.displacement = particle.position; |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 class ParticleClimbingRamp extends System { | 118 class ParticleClimbingRamp extends System { |
| 119 final Particle particle; | 119 final Particle particle; |
| 120 final Box box; | 120 final Box box; |
| 121 final double slope; | 121 final double slope; |
| 122 | 122 |
| 123 ParticleClimbingRamp({ | 123 ParticleClimbingRamp({ |
| 124 this.particle, | 124 this.particle, |
| 125 this.box, | 125 this.box, |
| 126 this.slope, | 126 this.slope, |
| 127 double targetPosition}) { | 127 double targetPosition}) { |
| 128 double deltaPosition = targetPosition - particle.position; | 128 double deltaPosition = targetPosition - particle.position; |
| 129 particle.energy = -kGravity * slope * deltaPosition * particle.mass; | 129 particle.energy = -kGravity * slope * deltaPosition * particle.mass; |
| 130 box.confine(particle); | 130 box.confine(particle); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void update(double deltaT) { | 133 void update(double deltaT) { |
| 134 particle.update(deltaT); | 134 particle.update(deltaT); |
| 135 // Note that we apply the impluse from gravity after updating the particle's | 135 // Note that we apply the impulse from gravity after updating the particle's |
| 136 // position so that we overestimate the distance traveled by the particle. | 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 | 137 // That ensures that we actually hit the edge of the box and don't wind up |
| 138 // reversing course. | 138 // reversing course. |
| 139 particle.applyImpluse(kGravity * slope * deltaT); | 139 particle.applyImpulse(kGravity * slope * deltaT); |
| 140 box.confine(particle); | 140 box.confine(particle); |
| 141 } | 141 } |
| 142 } | 142 } |
| OLD | NEW |