Mercurial > hg > beaglert
comparison examples/10-Instruments/airharp/MassSpringDamper.cpp @ 464:8fcfbfb32aa0 prerelease
Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Mon, 20 Jun 2016 16:20:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
463:c47709e8b5c9 | 464:8fcfbfb32aa0 |
---|---|
1 /* | |
2 * | |
3 * Simple 1-Dimensional Mass Spring Damper | |
4 * | |
5 * Christian Heinrichs 04/2015 | |
6 * | |
7 */ | |
8 | |
9 #include "MassSpringDamper.h" | |
10 | |
11 MassSpringDamper::MassSpringDamper(float mass, float spring, float damp) { | |
12 | |
13 _dt = 1.0/44100.0; | |
14 _mass = mass; | |
15 _spring = spring; | |
16 _damp = damp; | |
17 _position = 0; | |
18 _velocity = 0; | |
19 | |
20 } | |
21 | |
22 void MassSpringDamper::setup() { | |
23 | |
24 } | |
25 | |
26 double MassSpringDamper::update(float inForce) { | |
27 | |
28 // 1. calculate spring/damper forces using current position and velocity | |
29 | |
30 double out = (_position * (double)_spring * -1) + (_velocity * (double)_damp * -1); | |
31 | |
32 // 2. apply external force | |
33 | |
34 out += inForce; | |
35 | |
36 // 3. derive acceleration (a = f/m) | |
37 | |
38 out /= (double)_mass; | |
39 | |
40 // 4. derive velocity (v = a*dt) | |
41 | |
42 out *= _dt; | |
43 | |
44 // 5. apply previous velocity | |
45 | |
46 out += _velocity; | |
47 | |
48 // 6. save current velocity state for next iteration | |
49 | |
50 _velocity = out; | |
51 | |
52 // 7. derive new position (x[n] = x[n-1] + v[n]) and save for next iteration | |
53 | |
54 out += _position; | |
55 _position = out; | |
56 | |
57 return out; | |
58 | |
59 } |