Creating a trail of moving object in Processing

From dftwiki3
Revision as of 10:12, 17 June 2012 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut 08:41, 17 June 2012 (EDT)


This tutorial page explores various techniques for marking the path of animated objects in Processing. Many of the ideas and techniques presented here are taken from the [Processing.org] site, which remains one of the best resource for Processing. Don't hesitate to browse its pages for good ideas!


Unlimited Trail

Here we just start with the basic idea which we already played with in [Processing Skeleton Project and Simple Exercises | the previous tutorial in this section].


package tutorial1;
 
import processing.core.*;
 
public class Main4 extends PApplet {
 
	public void setup() {
		// define the window size, make graphics softer, and make
		// the background white
		size(600, 600);
		smooth();
		background( 0xeeeeff );
	}
 
	public void draw() {
 
		// change color of circle paint depending on mouse button
		if (mousePressed)  {
			stroke( 255 );
			fill(0);
		}
		else { 
			stroke( 0 );
			fill(255);
		}
 
                // draw a circle where the mouse is located
		ellipse(mouseX, mouseY, 80, 80);
	}
}


  • Try it out!
  • Let's play some with this simple example.

Growing and shrinking circles

  • One way to make the circle grow and shrink as the animation goes on is to control its radii with a sine function whose angle is simply the number of frames since the beginning of the animation.
  • The trick here relies on knowing that Processing counts the number of frames it displays and keeps the count in a variable named frameCount. The counter starts with 0, and every time draw() is called by Processing, frameCount is incremented by 1.
  • Modify the program above, and add a new variable inside the draw() function:


                 float radius = 50 + 30 * sin( frameCount * 0.05f );


  • Because the sin() function oscillates between -1 and 1 as the angle increases, radius will oscillate between 50 - 30, and 50 + 30, or 20 and 80.
  • Change the call to ellipse() and replace the constant 80 by the variable radius.


                // draw a circle where the mouse is located
		ellipse(mouseX, mouseY, radius, radius);


  • Try your new code!

Exercise #1

QuestionMark1.jpg


  • Make the color change with the growing and shrinking of the circle. You may find the information in the processing page on color very useful!
  • Keep one of the radius values constant, and the other one equal to the variable radius.
  • Use two different variables, radius1 and radius2 which are initialized the same way radius is, but use the sin() function for one, and the cos() function for the other one. Use radius1 and radius2 as the two radii value in the ellipse() function...




Fading Trail

One way to make the trail disappear is to make whatever is drawn in the applet become more and more transparent as it ages. In this case, objects that have been drawn in the current frame are fully opaque, while objects that have been drawn in earlier frames become more and more transparent, to the point where they disappear.

One way to accomplish this is to paint a transparent rectangle over the whole applet before we draw new objects in the window. In this case we use these three lines:

		noStroke();
		fill( 0xee, 0xee, 0xff, 50);
		rect(0, 0, width, height);


inside the draw() function, as the first thing we do. The magic number here is 50 which is the amount of transparency we give the whole applet before we draw a new circle.

The new draw() function looks something like this:


	public void draw() {
		//Fade everything which is drawn
		noStroke();
		fill( 0xee, 0xee, 0xff, 50);
		rect(0, 0, width, height);	

		float radius = 50 + 30 * sin( frameCount * 0.05f );
		// change color of circle paint depending on mouse button
		if (mousePressed)  {
			stroke( 255, 255, 255 );
			fill( 0,0,0 );
		}
		else { 
			stroke( 0, 0, 0 );
			fill( 255, 255, 255 );
		}
 
		// draw a circle where the mouse is located
		ellipse(mouseX, mouseY, radius, radius);
	}


  • Try it!


Exercise #2

  • Change the value of the transparency (50) and try different values to see how they affect the applet and the trail.
  • Try adding a transparency value to the circles, to see how they are affected. Example:


                if (mousePressed)  {
			stroke( 255, 255, 255, 100 );
			fill( 0,0,0, 100 );
		}


  • Change the background color to white, or black, and, again, play with different values of transparencies to better understand how the applet is affected.
QuestionMark1.jpg