void ttRopeBasic::update(ofPoint AccFrc,ofPoint StartPos){ accFrc = AccFrc; startPos = StartPos; endPos = StartPos; if(num_char == 0){ if (accFrc.y<-0.15){ endPos.y += ofMap(accFrc.y, -0.15, -0.6, 0, 1024-endPos.y, true); length = StartPos.distance(endPos); bFixedMove = true; }else{ endPos.y = StartPos.y; length = StartPos.distance(endPos); bFixedMove = false; } } if (num_char == 1) { if (accFrc.y>0.15) { endPos.y -= ofMap(accFrc.y, 0.15, 0.6, 0, endPos.y,true); length = StartPos.distance(endPos); bFixedMove = true; }else{ endPos.y = StartPos.y; length = StartPos.distance(endPos); bFixedMove = false; } } getPos = startPos; getLength = length; }
WalkVect ofApp::createWalkVect(ofPoint velocity, ofPoint vector, IsLine line) { ofPoint dir = vector.normalize(); if(velocity.y < 0) dir *= -1; WalkVect vect; vect.dir = dir; vect.spd = velocity.length(); vect.line = line; return vect; }
void ofApp::update(){ mouse = ofPoint(mouseX, mouseY); mouse -= center; m = mouse.length(); cout << m << " is the magnitude of m" <<endl; //unit vectors mouse.normalize(); mouse*=50; }
//-------- void ofApp::getLineEndPoints(ofxCvBlob blob, ofPoint &start, ofPoint &end) { start = blob.pts[0]; end = blob.pts[1]; float maxDist = start.distance(end); for(int i=0; i < blob.nPts; i++){ if(blob.pts[i].distance(start) > maxDist) { end = blob.pts[i]; maxDist = start.distance(end); } } }
//---------------------------------------------------------- // http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/ static ofPoint getClosestPointUtil(const ofPoint& p1, const ofPoint& p2, const ofPoint& p3, float* normalizedPosition) { // if p1 is coincident with p2, there is no line if(p1 == p2) { if(normalizedPosition != NULL) { *normalizedPosition = 0; } return p1; } float u = (p3.x - p1.x) * (p2.x - p1.x); u += (p3.y - p1.y) * (p2.y - p1.y); // perfect place for fast inverse sqrt... float len = (p2 - p1).length(); u /= (len * len); // clamp u if(u > 1) { u = 1; } else if(u < 0) { u = 0; } if(normalizedPosition != NULL) { *normalizedPosition = u; } return p1.getInterpolated(p2, u); }
void mousePressed(int x, int y, int button) { x /= globalScale; y /= globalScale; canvas.mousePressed(x, y, button); side.mousePressed(x, y, button); // cout << btnHelpPos.distance(ofPoint(x,y)) << endl; if (btnHelpPos.distance(ofPoint(x,y))<btnHelp.width/2) showHelp(); if (btnNew.hitTest(x,y)) { files.cur=-1; canvas.clear(); files.unloadFile(); } if (btnSave.hitTest(x,y)) files.save(); if (btnLoadPrevious.hitTest(x,y)) files.loadPrevious(); if (btnLoadNext.hitTest(x,y)) files.loadNext(); if (btnPrint.hitTest(x,y)) print(); if (btnStop.hitTest(x,y)) stop(); if (btnOops.hitTest(x,y)) { btnOops.selected=true; } if (btnZoomIn.hitTest(x,y)) btnZoomIn.selected=true; if (btnZoomOut.hitTest(x,y)) btnZoomOut.selected=true; if (btnHigher.hitTest(x,y)) btnHigher.selected=true; if (btnLower.hitTest(x,y)) btnLower.selected=true; if (btnTwistLeft.hitTest(x,y)) btnTwistLeft.selected=true; if (btnTwistRight.hitTest(x,y)) btnTwistRight.selected=true; if (shapeButtons.inside(x,y)) { int index = ofNormalize(x,shapeButtons.x,shapeButtons.x+shapeButtons.width) * shapeString.size(); side.setShape(shapeString.at(index)); } }
ofPoint SuperGlyph::getGetClosePath(ofPoint _pos){ ofPoint normal; ofPoint target; float minDist = 1000000; for (int i = 0; i < insidePath.getVertices().size()-1; i++) { ofPoint a = insidePath.getVertices()[i]; ofPoint b = insidePath.getVertices()[i+1]; ofPoint normalPoint = getNormalPoint(_pos, a, b); if (normalPoint.x < a.x || normalPoint.x > b.x) { normalPoint = b; } float distance = _pos.distance(normalPoint); if (distance < minDist) { minDist = distance; normal = normalPoint; ofPoint dir = b - a; dir.normalize(); dir *= 10; target = normalPoint; target += dir; } } return target; }
ofFloatColor getColor(ofPoint point) { float max = point.lengthSquared(); double r,g,b; double color = (max / (maxDistance/13)); if (color < 1) { r = (1 - color) ; g = color; b = 0; } else if (color < 2) { color--; r = 0; g = 1 - color; b = color; } else { r = 0; g = 0; b = 1; } return ofFloatColor(r,g,b); }
//-------------------------------------------------------------- void testApp:: drawPointOnLine(ofPoint p1, ofPoint p2, float t){ t = ofClamp(t, 0, 1); ofPoint circleCenter = p1.getInterpolated(p2, t); ofCircle(circleCenter, 5); ofLine(p1, p2); ofCircle(p1, 3); ofCircle(p2, 3); }
void ofxMarchingCubes::vertexInterp(float threshold,const ofPoint& p1,const ofPoint& p2, float valp1, float valp2, ofPoint& theVertice){ float mu; if (ABS(threshold-valp1) < 0.00001){ theVertice.set(p1.x, p1.y, p1.z); return; } if (ABS(threshold-valp2) < 0.00001){ theVertice.set(p2.x, p2.y, p2.z); return; } if (ABS(valp1-valp2) < 0.00001){ theVertice.set(p1.x, p1.x, p1.z); return; } mu = (threshold - valp1) / (valp2 - valp1); theVertice.x = p1.x + mu * (p2.x - p1.x); theVertice.y = p1.y + mu * (p2.y - p1.y); theVertice.z = p1.z + mu * (p2.z - p1.z); }
bool findClosestIntersectionLineAndPoly( ofPoint a, ofPoint b, vector<ofPoint> pts, ofPoint & closestPoint, int & sideId ) { if(pts.size() <= 0 ) return false; vector<float> dist; vector<ofPoint> ipts; vector<int> ids; //if( pts[0].x != pts[ pts.size()-1].x && pts[0].y != pts[ pts.size()-1].y) pts.push_back(pts[0]); for( int i = 1; i < pts.size(); i++) { ofPoint iPt; if ( intersectionTwoLines( pts[i-1], pts[i], a, b, &iPt ) ) { dist.push_back( ( (a.x-iPt.x) *(a.x-iPt.x) + (a.y-iPt.y)*(a.y-iPt.y) ) ); ipts.push_back( iPt ); ids.push_back(i-1); } } closestPoint.set(0,0); if( ipts.size() <= 0 ) return false; ofPoint p = ofPoint(0,0); float cdist = 0; for( int i = 0; i < dist.size(); i++) { if( i == 0 || dist[i] < cdist ) { cdist = dist[i]; p.set( ipts[i].x,ipts[i].y ); sideId = ids[i]; } } closestPoint.set(p.x,p.y); return true; }
void update(){ Hands= ofPoint(x,y); //location of the hands in the air ( ir sensors) dir= ofPoint(Hands - location); dir.normalize(); dir *=0.01; accel= dir; velocity += accel; //speed location += velocity; // where is + the movement speed velocity.limit(maxspeed); //verctor doesnt get bigger then 3 //collision target if(ofDist(Target.x,Target.y, location.x, location.y)<rectSize){ points ++; Target=ofPoint(ofRandom(20,ofGetWidth()-20), ofRandom(20,ofGetHeight()-20)); hitsound.play(); } }
//-------------------------------------------------------------- void ofApp::setup(){ ofSetWindowShape(1024,1024); ofSetBackgroundAuto(true); //ofBackground(0,0,0); // background ofBackground(128,128,128); // background imageCrystal.loadImage("crystal.jpg"); //load initial image point.set(10,10); ofEnableBlendMode(OF_BLENDMODE_SUBTRACT); }
//-------------------------------------------------------------- void testApp::drawGravity(ofPoint p, ofPoint gravity) { float angle = (atan2(gravity.y, gravity.x) * 180 / PI) - 90; float len = MIN(200, gravity.length()*10); // scale it up a bit ofPushMatrix(); ofTranslate(p.x, p.y); ofRotate(angle); ofLine(0, 0, 0, len); ofTriangle(0, len, -5, len-10, 5, len-10); ofPopMatrix(); }
//Update vars based on Acceleration = Force/Mass void Particle::refresh() { accel.x = force.x/mass; accel.y = force.y/mass; vel+= accel; pos += vel; //Restrict pos to window bounds //Find glancing direction by adding 180 - 2theta degrees //Left/Right border collision if(pos.x <= 0 || pos.x >= ofGetWindowWidth()) { float theta = vel.angle(ofPoint(0, 1)); vel.rotate(180, ofPoint(0,1)); } //Top/Bottom border collision if(pos.y <= 0 || pos.y >= ofGetWindowHeight()) { float theta = vel.angle(ofPoint(1, 0)); vel.rotate(180, ofPoint(1,0)); } accel = ofPoint(0,0); }
void ofApp::drawVector(ofPoint v, ofPoint loc, float scayl){ ofPushMatrix(); float arrowsize = 4; // Translate to location to render vector ofTranslate(loc); ofColor(255); // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate float angle = (float)atan2(-v.y, v.x); float theta = -1.0*angle; float heading2D = ofRadToDeg(theta); ofRotateZ(heading2D); // Calculate length of vector & scale it to be bigger or smaller if necessary float len = v.length()*scayl; // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction) ofDrawLine(0,0,len,0); ofDrawLine(len,0,len-arrowsize,+arrowsize/2); ofDrawLine(len,0,len-arrowsize,-arrowsize/2); ofPopMatrix(); }
void Communitas::addParticle(ofPoint _pos, int _n) { if ( universe.inside( _pos ) ){ timer = 0; int nLnk = 0; for (int i = pAct.size()-1; i >= 0 ; i--){ if (nLnk == 0) if (pAct[i]->idN == _n) nLnk = i; if ( _pos.distance(pAct[i]->loc) <= 40) pAct[i]->alpha = 255; } LineDot * dot = new LineDot(_n, &universe, _pos ,false); if ( nLnk > 0) dot->setLnk(pAct[nLnk]); pAct.push_back(dot); } }
bool ofxDraggableNode::isInside(ofPoint p) const { ofPoint screenCenter = cam->worldToScreen(this->getGlobalPosition()); ofPoint screenCenterRadius = cam->worldToScreen(this->getGlobalPosition() + cam->getUpDir() * radius); float r = screenCenter.distance(screenCenterRadius); //radius on screen return p.distance(screenCenter) < r; }
float ofxVoronoi::getDistance(ofPoint p1, ofPoint p2){ return p1.squareDistance(p2); }
//-------------------------------------------------------------- void testApp::setup(){ fadeActiveVal = 0; fadeToBlack = 0; // --------------- setup the gui window GUI.setup(ofRectangle(0,0,500,800), "my sample gui"); GUI.setEventInterface(this); GUI.addButton("repeatSound", "repeat sound", ofRectangle(10,10,120,30)); GUI.addButton("newColor", "new color", ofRectangle(10,50,120,30)); GUI.addTextEntry("textBox", "please enter text here", ofRectangle(10,120,180,90)); GUI.addSlider("volSmth", ofRectangle(10,260,180,30), 0, 1, 0.8, &ACM.volumeSmoothing); GUI.addSlider("hueRange", ofRectangle(10,300,180,30), 0,400,35, &ACM.hueRange); GUI.addSlider("hueShiftValStart", ofRectangle(10,340,180,30), 0,1,0.2, &ACM.hueShiftValStart); GUI.addSlider("darkness", ofRectangle(10,340+40,180,30), 0,1,0.5, &ACM.darkness); GUI.addSlider("hueFadeRate", ofRectangle(10,340+80,180,30), 0,1,0.96, &ACM.hueFadeRate); GUI.addSlider("fadeToBlack", ofRectangle(10,340+120,180,30), 0,1,1, &fadeToBlack); GUI.addSlider("delay", ofRectangle(10,340+160,180,30), 0,200,10, &XBC.delay); GUI.addSlider("balloonFade", ofRectangle(10,340+200,180,30), 0,255,10, &XBC.balloonFade); left = new float[256]; AA.setup(44100); // sample rate! ACM.setup(); bPlaying = false; ofSoundStreamSetup(2, 0, this, 44100, 256, 4); ofSetFrameRate(30); // ------- midi // print input ports to console midiIn.listPorts(); // open port by number midiIn.openPort(0); //midiIn.openPort("IAC Pure Data In"); // by name //midiIn.openVirtualPort("ofxMidiIn Input"); // open a virtual port // don't ignore sysex, timing, & active sense messages, // these are ignored by default midiIn.ignoreTypes(false, false, false); // add testApp as a listener midiIn.addListener(this); // print received messages to the console midiIn.setVerbose(true); midiColor.set(0,0,0); }
void testApp::draw() { // clear depth buffer (but not color buffer) glClear(GL_DEPTH_BUFFER_BIT); // enable blending glEnable(GL_BLEND); // choose semi-transparent black color myObj.setColor(0, 0, 0, 0.05f); // draw a black semi-transparent rectangle across whole screen to fade it out a bit myObj.drawRect(0, 0, ofGetWidth(), ofGetHeight()); // disable blending glDisable(GL_BLEND); // get current time (could use ofGetElapsedTimef() but I DONT want it to be fps independant // cos when I'm saving frames and it's running slow I want it to behave the same float curTime = ofGetFrameNum() * 1.0f/60.0f; // choose start rotation based on curTime float theta = sin(curTime * 0.17f) * TWO_PI; // set start position offset from center of screen float startOffset = -50.0f * sin(curTime * 0.09f); pos.set(ofGetWidth()/2 + cos(theta) * startOffset, ofGetHeight()/2 + sin(theta) * startOffset, 0); // begin a triangle strip myObj.begin(GL_TRIANGLE_STRIP); for(int i=0; i<5000; i++) { // calculate and set colors // RGB components modulate sinusoidally, frequency increases with iteration count myObj.setColor(sin(curTime * 0.8f + i * 0.0011f) * 0.5f + 0.5f, sin(curTime * 0.7f + i * 0.0013f) * 0.5f + 0.5f, sin(curTime * 0.3f + i * 0.0017f) * 0.5f + 0.5f); // do some maths to calculate vertex positions // modulate theta (offset rotation) sinusoidally, frequency increases with iteration count // use two modulations with different frequencies to create complex harmonic motion theta += sin(curTime * 0.1f + i * 0.00062f) * 2.0f * DEG_TO_RAD * i * 0.0004 + sin(curTime*0.2f + i * 0.0009f) * 3.0f * DEG_TO_RAD; float cos_t = cos(theta); float sin_t = sin(theta); // x, y position cumulatively rotates // z position modulates sinusoidally, frequency increases with iteration count pos += ofPoint(cos_t, sin_t, sin(curTime * 0.5f + i*0.002f) * 0.5); // send vertex data to myObj myObj.addVertex(pos.x, pos.y, pos.z); // do some math to calculate another vertex position // perpendicular to rotation // thickness modulates sinusoidally, frequency increases with iteration count // also modulation frequency sinusoidally ofPoint pos2 = pos + ofPoint(sin_t, -cos_t) * ofMap(sin(curTime * 0.4f * ( 1.0f + i * 0.001f) + i * 0.06f + sin(i*0.001f) * 0.2f), -1, 1, 5, 10 + i * 0.01f); // place second vertex (can also pass pointer (array) to coordinates) myObj.addVertex2v(pos2.v); } // end vertices and draw to screen myObj.end(); }
//-------------------------------------------------------------- void camApp::draw(){ //hitTest(); ofSetupScreen(); float width = ofGetWidth(); float height = ofGetHeight(); //TODO: check why the background was fading weird on rise background - OpenGL related bug? ofEnableAlphaBlending(); ofSetColor(255, 255, 255, 255); background.draw(ofGetWidth()/2, ofGetHeight()/2, ofGetWidth(), ofGetHeight() ); ofSetColor(255, 255, 255, ofMap(riseFallPct, 0.49, 0.0, 0, 255, true)); background2.draw(ofGetWidth()/2, ofGetHeight()/2, ofGetWidth(), ofGetHeight()); world.setScale(ofGetWidth(), ofGetHeight(), ofGetWidth()); world.setOrigin(ofPoint(ofGetWidth()/2, ofGetHeight()/2, 0.0)); world.begin(); world.ourCameraSetup(width/2, height/2, 0.0, width, height, fov ); //world.drawDebugGrid(); glDisable(GL_DEPTH_TEST); sceneGraph.updateCameraPos(world.camPos + ofPoint(0, 0, world.camDist)); sceneGraph.sortZ(); ofRectangle clipRect; clipRect.width = 2.0 * ofGetWidth(); clipRect.height = 2.2 * ofGetHeight(); // lets give us some extra room! clipRect.x = world.camPos.x - clipRect.width/2; clipRect.y = world.camPos.y - clipRect.height/2; //TODO: MAKE PLANT CLIPPING BETTER sceneGraph.renderWithClipping(clipRect, -180+world.camPos.z, 100 + world.camPos.z, world.camDist, fov ); hand.updateCamToScreenCoords(); GLdouble model_view[16]; glGetDoublev(GL_MODELVIEW_MATRIX, model_view); GLdouble projection[16]; glGetDoublev(GL_PROJECTION_MATRIX, projection); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); GLdouble X, Y, Z = 0; gluProject(myPerson.pos.x, myPerson.pos.y, myPerson.pos.z, model_view, projection, viewport, &X, &Y, &Z); personScreenPos.set(X, ofGetHeight()-Y, Z); world.end(); ofSetupScreen(); //this node draws in screen space. hand.draw(); if( percentOver > 0.0 ){ ofSetColor(255, 255, 255, 255*percentOver*1.2); if( endCreatedInRise ){ backgroundEnd.draw(ofGetWidth()/2, ofGetHeight()/2, ofGetWidth(), ofGetHeight()); }else{ backgroundEnd2.draw(ofGetWidth()/2, ofGetHeight()/2, ofGetWidth(), ofGetHeight()); } world.begin(); world.ourCameraSetup(width/2, height/2, 0.0, width, height, fov ); endGraphic.draw(); myPerson.draw(); world.end(); ofSetupScreen(); } if( howTo.state != NODE_INACTIVE ){ howTo.draw(); myPerson.drawAtScreenPos(personScreenPos.x, personScreenPos.y, 0); } ofSetColor(10, 50, 150); ofDrawBitmapString(hitsString, 10, 50); ofEnableAlphaBlending(); }