We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Moving Sine Wave (Beginner)
Page Index Toggle Pages: 1
Moving Sine Wave (Beginner) (Read 1341 times)
Moving Sine Wave (Beginner)
Feb 16th, 2010, 6:01pm
 
Hi,

I'm having difficulties creating an oscillating sine wave.
(Actually, I haven't the faintest idea how to go about this, though I think I may have to create a function...) Advice would be greatly appreciated. Here's what I have so far--the static sine wave:

void setup (){
 size(400, 400);
 background(0);
 noStroke();
 fill(255,50);
}

void draw(){

 for (float x = 0; x < 3*TWO_PI; x+=0.02) {
   // Calculate value
   float y = sin(x);
   // Render wave using ellipse.
   ellipse(x*21,height/2+y*50,16,16);
 }
}

(I'm only dimly aware of arrays, so if possible I'd like to avoid using them.)
Re: Moving Sine Wave (Beginner)
Reply #1 - Feb 16th, 2010, 6:14pm
 
Having the static wave done, you're already almost there. The only thing needed to animate the wave is by adding a moving offset to your input phase value for the sin() function, so like:

Code:
float y = sin(x+frameCount*0.1); 


1) You also need to move the background() from setup() into draw() to get the correct effect...

2) You might also want to check out this:
http://toxiclibs.org/2009/12/additive-waves/
Re: Moving Sine Wave (Beginner)
Reply #2 - Feb 16th, 2010, 6:23pm
 
Thank you so much!
What exactly is 'frameCount', though? Since I didn't need to create a variable named 'frameCount', does that make it a pre-existing function?

(By the way, I looked at your website. It's rather...overwhelming. But very impressive. Smiley )
Re: Moving Sine Wave (Beginner)
Reply #3 - Feb 16th, 2010, 6:48pm
 
frameCount is what it said. the Count of the Frames...
it is often used if you need an increasing variable. instead of creating a variable X and count it up every frame, you can just use frameCount.
Re: Moving Sine Wave (Beginner)
Reply #4 - Feb 17th, 2010, 12:44am
 
Si_Long wrote on Feb 16th, 2010, 6:23pm:
does that make it a pre-existing function

No. That's a pre-existing... variable! Processing has lot of them, from width, height, to sketchPath or g.
Re: Moving Sine Wave (Beginner)
Reply #5 - Feb 17th, 2010, 2:27am
 
I love g.  Grin

Also, cool sin wave!
Re: Moving Sine Wave (Beginner)
Reply #6 - Feb 18th, 2010, 11:49pm
 
slightly out of topic, but I'm curious what is g.?
Re: Moving Sine Wave (Beginner)
Reply #7 - Feb 19th, 2010, 1:10am
 
g is the default PGraphics object. Your calls to things like fill() or beginDraw(), etc, are actually calls to g.fill() or g.beginDraw(), g.etc.
Page Index Toggle Pages: 1