Creating a trail of moving object in Processing

From dftwiki3
Revision as of 08:39, 17 June 2012 by Thiebaut (talk | contribs) (Growing and shrinking circles)
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...