comparison examples/airharp/MassSpringDamper.cpp @ 300:dbeed520b014 prerelease

Renamed projects to examples
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 27 May 2016 13:58:20 +0100
parents projects/airharp/MassSpringDamper.cpp@40badaff5729
children
comparison
equal deleted inserted replaced
297:a3d83ebdf49b 300:dbeed520b014
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 }