Resetting Mesh after an amount of time


#1

–Hello!

I am working with one of the camera ribbon examples from OpenFrameworks and I’m trying to figure out a way to remove all vectors or points after a specific amount of time.

What would be the best way to write the if statement for that?

void ofApp::draw(){
  ofMesh mesh;
	mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
	for(unsigned int i = 1; i < points.size(); i++){
        
		//find this point and the next point
		ofVec3f thisPoint = points[i-1];
		ofVec3f nextPoint = points[i];
        
		//get the direction from one to the next.
		//the ribbon should fan out from this direction
		ofVec3f direction = (nextPoint - thisPoint);
        
		//get the distance from one point to the next
		float distance = direction.length();
        
		//get the normalized direction. normalized vectors always have a length of one
		//and are really useful for representing directions as opposed to something with length
		ofVec3f unitDirection = direction.normalized();
        
		//find both directions to the left and to the right
		ofVec3f toTheLeft = unitDirection.getRotated(-90, ofVec3f(0,0,1));
		ofVec3f toTheRight = unitDirection.getRotated(90, ofVec3f(0,0,1));
        
		//use the map function to determine the distance.
		//the longer the distance, the narrower the line.
		//this makes it look a bit like brush strokes
		float thickness = ofMap(distance, 0, 60, 20, 2, true);
        
		//calculate the points to the left and to the right
		//by extending the current point in the direction of left/right by the length
		ofVec3f leftPoint = thisPoint+toTheLeft*thickness;
		ofVec3f rightPoint = thisPoint+toTheRight*thickness;
        
		//add these points to the triangle strip
		mesh.addVertex(ofVec3f(leftPoint.x, leftPoint.y, leftPoint.z));
		mesh.addVertex(ofVec3f(rightPoint.x, rightPoint.y, rightPoint.z));
	}
    
	//end the shape
	mesh.draw();
}

#2

Basically, I think you will call mesh.clear() (http://openframeworks.cc/documentation/3d/ofMesh.html#show_clear). You need to set some kind of timer if you want it to clear out after a certain amount of time. You could clear it out after a certain time has elapsed, or every X number of frames.

http://www.openframeworks.cc/documentation/utils/ofUtils.html#show_ofGetElapsedTimeMillis

You might do something like:

if (ofGetFrameNum() % 1000)
{
   mesh.clear();
}

This will clear all mesh points every 1000 frames.


#3

–Thank you very much!


#4

How would I reset ofGetElapsedTimef() after the mesh is cleared so the program can continue running after?

if (ofGetElapsedTimef() >= 10)
{
points.clear();
//reset time
}


#5

It’s probably better not to reset the elapsed time counter. Instead, keep a variable that will tell you when the next reset should occur. Something like …

// These member variables would go in your class somewhere ... probably in your ofApp.h file.

float nextResetTime = 0;
float resetIntervalTime = 10;

/// This would go in your update or draw loop

float now = ofGetElapsedTimef();

if (now > nextResetTime)
{
  mesh.clear();
  nextResetTime = now + resetIntervalTime;
}

Basically, each time you reset, you set a new timer for the current time PLUS some interval that you want. Of course there are other ways to do the math, but this seems to be an easy one.


#6

–Awesome! Everything works perfectly, thanks again,