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 & HelpPrograms › beginner question about draw()
Page Index Toggle Pages: 1
beginner question about draw() (Read 463 times)
beginner question about draw()
Mar 9th, 2008, 1:59am
 
In the following code, the mouse/key events work as desired; but when the key or mouse is released, draw() seems to be returning to its original state, i. e., using the initial value of theta of 0. The current (incremented) value is stored, however. Why does this happen, any thoughts?

import processing.opengl.*;

float a = 11.0;
float b = 12.0;
float r = 400.0;
float x, y, z;
float theta = 0.0;
float delta = 0.01;



void setup() {
 size(1000, 1000, OPENGL);
}

void draw()
{
 background(0);
 //lights();
 translate(width / 2, height / 2);
 if(mousePressed)
 {
   rotateX(theta);
   rotateY(theta);
   rotateZ(theta);
   theta += delta;
 }
 
 if(keyPressed)
 {
   if(key == 'x')
   {
     rotateX(theta);
     theta += delta;
   }
   if(key == 'y')
   {
     rotateY(theta);
     theta += delta;
   }
   if(key == 'z')
   {
     rotateZ(theta);
     theta += delta;
   }
 }
 stroke(255, 64, 128);
 Plot();
}

void Plot()
{
 noFill();
 beginShape();
 for(float t = 0.0; t < 2.0 * PI; t += 0.005)
 {
   x = r * sin(a*t) * cos(b*t);
   y = r * sin(a*t) * sin(b*t);
   z = r * cos(a*t);
   vertex(x, y, z);
 }
 endShape(CLOSE);
}



Re: beginner question about draw()
Reply #1 - Mar 9th, 2008, 3:04am
 
It happens because when you are not pressing the mouse or any key, you are plotting the original graph.

I believe you should better RotateX,Y,Z inside the Plot function, and just pass the theta value to it.

This way, it will always draw the last rotation value. When you press the mouse you should then only increase the theta value.

Something like this

Code:

import processing.opengl.*;

float a = 11.0;
float b = 12.0;
float r = 400.0;
float x, y, z;
float theta = 0.0;
float delta = 0.01;



void setup() {
size(1000, 1000, OPENGL);
}

void draw()
{
background(0);
translate(width / 2, height / 2);

if(mousePressed)
{
theta+=delta;
}
stroke(255, 64, 128);
Plot(theta);
}

void Plot(float th)
{
noFill();
rotateX(th);
rotateY(th);
rotateZ(th);

beginShape();
for(float t = 0.0; t < 2.0 * PI; t += 0.005)
{
x = r * sin(a*t) * cos(b*t);
y = r * sin(a*t) * sin(b*t);
z = r * cos(a*t);
vertex(x, y, z);
}
endShape(CLOSE);

}


I deleted the keyPresses so it would be easier. But that's the way you can do it.
Re: beginner question about draw()
Reply #2 - Mar 9th, 2008, 1:32pm
 
Thanks very much, it works just as I need.
Page Index Toggle Pages: 1