rt300@0
|
1 //
|
rt300@0
|
2 // globalForces.cpp
|
rt300@0
|
3 // wablet
|
rt300@0
|
4 //
|
rt300@0
|
5 // Created by Robert Tubb on 18/07/2011.
|
rt300@0
|
6 // Copyright 2011 __MyCompanyName__. All rights reserved.
|
rt300@0
|
7 //
|
rt300@0
|
8
|
rt300@0
|
9 #include "globalForces.h"
|
rt300@0
|
10
|
rt300@0
|
11
|
rt300@0
|
12 //-----------------------------------------------------------
|
rt300@0
|
13 GlobalForces::GlobalForces(): grav(0.0,0.0){
|
rt300@0
|
14 //construct
|
rt300@0
|
15 gravityOn = true;
|
rt300@0
|
16
|
rt300@0
|
17 speedLimit = 100.0;
|
rt300@0
|
18 wallBounce = 0.5;
|
rt300@0
|
19
|
rt300@0
|
20 pressureAmt = 0.0;
|
rt300@0
|
21 gravityAmt = 0.2;
|
rt300@0
|
22 touchStrength = 1.0;
|
rt300@0
|
23 homingAmt = 0.0;
|
rt300@0
|
24 avFilterAmt = 0.0;
|
rt300@0
|
25
|
rt300@0
|
26 pressure = 0.0;
|
rt300@1
|
27 volume = 2.0;
|
rt300@0
|
28
|
rt300@0
|
29 maxForcePoints = 11;
|
rt300@0
|
30 excitationType = POSITION;
|
rt300@0
|
31 excitationShape = NOISE;
|
rt300@0
|
32
|
rt300@0
|
33 forceTouchPoints = new forceTouchPoint[maxForcePoints];
|
rt300@0
|
34 for(int i = 0; i< 11; i++){
|
rt300@0
|
35 forceTouchPoints[i].x = 0.0;
|
rt300@0
|
36 forceTouchPoints[i].y = 0.0;
|
rt300@0
|
37 forceTouchPoints[i].tid = -1;
|
rt300@0
|
38
|
rt300@0
|
39 }
|
rt300@0
|
40
|
rt300@0
|
41
|
rt300@0
|
42 }
|
rt300@0
|
43
|
rt300@0
|
44 //-----------------------------------------------------------
|
rt300@0
|
45 GlobalForces::~GlobalForces(){
|
rt300@0
|
46 //destruct
|
rt300@0
|
47
|
rt300@0
|
48 }
|
rt300@0
|
49 void GlobalForces::update(){
|
rt300@0
|
50 // gravity
|
rt300@0
|
51 grav.setCoord(ofxAccelerometer.getForce().y * 0.015 * gravityAmt, ofxAccelerometer.getForce().x * 0.015 * gravityAmt);
|
rt300@0
|
52 // time step
|
rt300@0
|
53 delt = 1/ofGetFrameRate();
|
rt300@0
|
54
|
rt300@0
|
55 }
|
rt300@0
|
56 //-----------------------------------------------------------
|
rt300@0
|
57 // this is actually only called from dropletmesh.update
|
rt300@0
|
58 void GlobalForces::setPressure(double aP){
|
rt300@0
|
59
|
rt300@0
|
60 pressure = pressureAmt*aP;
|
rt300@0
|
61 }
|
rt300@0
|
62 //-----------------------------------------------------------
|
rt300@0
|
63 void GlobalForces::createForceTouchPoint(double ax,double ay, int touchId){
|
rt300@0
|
64
|
rt300@0
|
65 forceTouchPoints[touchId].x = ax;
|
rt300@0
|
66 forceTouchPoints[touchId].y = ay;
|
rt300@0
|
67 forceTouchPoints[touchId].tid = touchId;
|
rt300@0
|
68
|
rt300@0
|
69
|
rt300@0
|
70
|
rt300@0
|
71 }
|
rt300@0
|
72 //-----------------------------------------------------------
|
rt300@0
|
73 void GlobalForces::moveForceTouchPoint(double ax,double ay, int touchId){
|
rt300@0
|
74 //
|
rt300@0
|
75 for(int i = 0;i<maxForcePoints; i++){
|
rt300@0
|
76 if(forceTouchPoints[touchId].tid == touchId){
|
rt300@0
|
77 forceTouchPoints[touchId].x = ax;
|
rt300@0
|
78 forceTouchPoints[touchId].y = ay;
|
rt300@0
|
79 }
|
rt300@0
|
80 }
|
rt300@0
|
81
|
rt300@0
|
82 }
|
rt300@0
|
83 //-----------------------------------------------------------
|
rt300@0
|
84 void GlobalForces::removeForceTouchPoint(int touchId){
|
rt300@0
|
85 //
|
rt300@0
|
86 for(int i = 0;i<maxForcePoints; i++){
|
rt300@0
|
87 if(forceTouchPoints[i].tid == touchId){
|
rt300@0
|
88 forceTouchPoints[i].tid = -1;
|
rt300@0
|
89 }
|
rt300@0
|
90 }
|
rt300@0
|
91 }
|
rt300@0
|
92 //-----------------------------------------------------------
|
rt300@0
|
93 TwoVector GlobalForces::getAllForceAt(double ax, double ay){
|
rt300@0
|
94 //this is called for every mass point so work out most stuff in update()
|
rt300@0
|
95 TwoVector force(0.0,0.0);
|
rt300@0
|
96 double fmag, r;
|
rt300@0
|
97 if(gravityOn){
|
rt300@0
|
98
|
rt300@0
|
99 force.x += grav.x;
|
rt300@0
|
100 force.y += grav.y;
|
rt300@0
|
101 }
|
rt300@0
|
102
|
rt300@0
|
103 // now touch points
|
rt300@0
|
104 for(int i = 0;i<maxForcePoints; i++){
|
rt300@0
|
105 // work out distance to force point and then use 1/r^2 rule
|
rt300@0
|
106 if(forceTouchPoints[i].tid != -1){
|
rt300@0
|
107 TwoVector diff(forceTouchPoints[i].x - ax, forceTouchPoints[i].y - ay );
|
rt300@0
|
108 r = diff.norm();
|
rt300@0
|
109 if (r < 0.03){ // try avoid very small distances giving ridiculous forces
|
rt300@0
|
110 fmag = 0.0;
|
rt300@0
|
111 }else{
|
rt300@0
|
112 fmag = touchStrength*1/(1000*(r*r));
|
rt300@0
|
113 }
|
rt300@0
|
114 force.x -= fmag*diff.x/r;
|
rt300@0
|
115 force.y -= fmag*diff.y/r;
|
rt300@0
|
116
|
rt300@0
|
117 }
|
rt300@0
|
118 }
|
rt300@0
|
119
|
rt300@0
|
120 return force;
|
rt300@0
|
121 }
|
rt300@9
|
122 //-----------------------------------------------------------
|
rt300@9
|
123
|
rt300@9
|
124 Json::Value GlobalForces::convertToJsonForSaving(){
|
rt300@9
|
125
|
rt300@9
|
126 Json::Value jgf;
|
rt300@9
|
127 jgf["gravityOn"] = gravityOn;
|
rt300@9
|
128 jgf["forceTouchOn"] = forceTouchOn;
|
rt300@9
|
129 jgf["pressureOn"] = pressureOn;
|
rt300@9
|
130 jgf["pressureAmt"] = pressureAmt;
|
rt300@9
|
131 jgf["gravityAmt"] = gravityAmt;
|
rt300@9
|
132 jgf["touchStrength"] = touchStrength;
|
rt300@9
|
133 jgf["excitationStrength"] = excitationStrength;
|
rt300@9
|
134 jgf["homingAmt"] = homingAmt;
|
rt300@9
|
135 jgf["avFilterAmt"] = avFilterAmt;
|
rt300@9
|
136 jgf["exciteShapeX"] = exciteShapeX;
|
rt300@9
|
137 jgf["exciteShapeY"] = exciteShapeY;
|
rt300@9
|
138 jgf["speedLimit"] = speedLimit;
|
rt300@9
|
139 jgf["wallBounce"] = wallBounce;
|
rt300@9
|
140
|
rt300@9
|
141 return jgf;
|
rt300@9
|
142 }
|
rt300@9
|
143 //-----------------------------------------------------------
|
rt300@9
|
144
|
rt300@9
|
145 void GlobalForces::setFromJson(Json::Value jgf){
|
rt300@9
|
146
|
rt300@9
|
147 gravityOn = jgf["gravityOn"].asBool();
|
rt300@9
|
148 forceTouchOn = jgf["forceTouchOn"].asBool();
|
rt300@9
|
149 pressureOn = jgf["pressureOn"].asBool();
|
rt300@9
|
150 pressureAmt = jgf["pressureAmt"].asDouble();
|
rt300@9
|
151 gravityAmt = jgf["gravityAmt"].asDouble();
|
rt300@9
|
152 touchStrength = jgf["touchStrength"].asDouble();
|
rt300@9
|
153 excitationStrength = jgf["excitationStrength"].asDouble();
|
rt300@9
|
154 homingAmt = jgf["homingAmt"].asDouble();
|
rt300@9
|
155 avFilterAmt = jgf["avFilterAmt"].asDouble();
|
rt300@9
|
156 exciteShapeX = jgf["exciteShapeX"].asInt();
|
rt300@9
|
157 exciteShapeY = jgf["exciteShapeY"].asInt();
|
rt300@9
|
158 speedLimit = jgf["speedLimit"].asDouble();
|
rt300@9
|
159 wallBounce = jgf["wallBounce"].asDouble();
|
rt300@9
|
160
|
rt300@9
|
161 }
|