annotate globalForces.mm @ 15:d5758530a039 tip

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