ofVec2f testApp::getVelocityForTime(float time){ // To get the velocity we'll look at the position of two points and get their difference. // We want this relative to time, so we'll choose an arbitrary amount of time to look back into. For this, I chose 0.09 // We use MAX so we're never asking for a point less than 0 (because our first point exists at 0, there is nothing before it!) ofPoint prevPt = getPositionForTime( MAX(time - 0.09f, 0)); // Where we were 0.09 seconds ago. ofPoint currPt = getPositionForTime(time); // Where we are now! ofPoint diff; diff.x = currPt.x - prevPt.x; diff.y = currPt.y - prevPt.y; return diff; }
ofPoint TimeLine::getVelocityForTime( float _time){ // to get the velcoity, look back a bit of time, and at the current time // and get the difference // veclocity = pos at time 1 - pos at time 0... ofPoint prevPt = getPositionForTime( MAX(_time - 0.09f, 0)); // check for where we were 0.05 seconds ago ofPoint currPt = getPositionForTime(_time); // check for where we are now. ofPoint diff; diff.x = currPt.x - prevPt.x; diff.y = currPt.y - prevPt.y; return diff; }
//-------------------------------------------------------------- void testApp::draw(){ // -------------------------- draw the line ofSetColor(0,0,0); ofNoFill(); ofBeginShape(); for (int i = 0; i < pointList.size(); i++){ ofVertex(pointList[i].x, pointList[i].y); } ofEndShape(); // -------------------------- draw the point at the current time ofPoint pos = getPositionForTime(ofGetElapsedTimef() - playbackStartTime); ofVec2f vel = getVelocityForTime(ofGetElapsedTimef() - playbackStartTime); float angle = atan2( vel.y, vel.x ); // remember that y comes before x. It also works because velocity has a direction. Pos does not. float vecLen = vel.length(); // the length of our velocity vector ofFill(); ofSetColor(255,0,0); ofSetRectMode( OF_RECTMODE_CENTER ); // draw a box aligned to the path we drew, with some stretch based on velocity ofPushMatrix();{ ofTranslate(pos.x, pos.y, 0); ofRotateZ( ofRadToDeg(angle) ); ofRect(0,0,50 + vecLen*0.5, 50); }ofPopMatrix(); }
//-------------------------------------------------------------- void testApp::draw(){ // we want to bounce back and forth, so we'll use something that cycles. How about sin? float sinVal = sin( ofGetElapsedTimef() ); float remappedSinPct = ofMap(sinVal, -1, 1, 0, 1); // we may have no drawn anything yet. Lets check here. float drawingDuration = 0.0; if( pointList.size() > 0 ){ drawingDuration = MAX(0, pointList[pointList.size()-1].t); // the last time point is the same as the end time of our drawing } float newTime = drawingDuration * remappedSinPct; ofPoint pos = getPositionForTime( newTime ); // -------------------------- map the background color to the height of the particle float bgcolor = ofMap( pos.y, 0, ofGetWindowHeight(), 0.0, 1.0); ofBackground( (1-bgcolor) * 255.0 ); // -------------------------- draw the line ofSetColor(0,0,0); ofNoFill(); ofBeginShape(); for (int i = 0; i < pointList.size(); i++){ ofVertex(pointList[i].x, pointList[i].y); } ofEndShape(); // -------------------------- draw the circle ofFill(); ofSetColor(255,0,0); ofCircle(pos.x, pos.y, 10); }
void TimeLine::draw(){ if (points.size() > 0){ // For making super smooth and gradient lines use ofMeshes. // Them let you add vertex asociated to colors. // OpenGL it's going to be in charge of making the gradient ; ) // ofMesh lineMesh; lineMesh.setMode(OF_PRIMITIVE_LINE_STRIP); // There are other modes like: points, triangles and quads for (int i = 0 ; i < points.size() ; i++){ // Map the position on the array with the alpha to geting alfa gradient // //float alpha = ofMap(i+1, 1,points.size(), 0.0,1.0); lineMesh.addColor(1.0); //ofFloatColor(_color, alpha*0.3) ); // We are using ofFloatColors that goes from 0.0 - 1.0 // instead of 0-255 that's way you have better quality lineMesh.addVertex(points[i]); } // Once this object call ofMesh it's done you can draw it. // ofSetColor(255); lineMesh.draw(); if ( isFinish() ){ // figure out what time we are at, and make sure we playback cyclically (!) // use the duration here and make sure our timeToCheck is in the range of 0 - duration float timeToCheck = ofGetElapsedTimef(); while (timeToCheck > getDuration() && getDuration() > 0){ timeToCheck -= getDuration(); } // get the position and velocity at timeToCheck ofPoint pos = getPositionForTime(timeToCheck); ofPushStyle(); ofSetColor(255, 0, 0); ofCircle(pos, 5); ofPopStyle(); } } }
//-------------------------------------------------------------- void testApp::draw() { // -------------------------- draw the line ofSetColor(0,0,0); ofNoFill(); ofBeginShape(); for (int i = 0; i < pts.size(); i++) { ofVertex(pts[i].x, pts[i].y); } ofEndShape(); // -------------------------- draw the point at the current time ofPoint pos = getPositionForTime(ofGetElapsedTimef() - playbackStartTime); ofFill(); ofSetColor(255,0,0); ofCircle(pos.x, pos.y, 10); }