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.
Page Index Toggle Pages: 1
P3D problem: (Read 928 times)
P3D problem:
Nov 5th, 2006, 5:43am
 
Getting into Processing, putting together a program to draw and sketch images. I know I will have to begin deliniating some of these operations into separate methods, but for now I have a few syntax/layout problems that if answered could really help me on my way. Looking at the following code, which compiles, why are the squares not drawing? Following are two code samples. It draws if taken alone like this, and please notice that I have removed P3D capabilty:

//Let's get this started

void setup() {
 background(255);
 size(500,500);
 frameRate(10);
}

void draw() {
 
  if(keyPressed==true) {       //a drawing 'method'
   switch(key) {
   case 'a' :
     stroke(30,20,55,30);
     rect(mouseX,mouseY,30,30);
   }
 }

 println(frameCount);

}

***********************************************************
But as soon as I introduce a rotation feature, it ceases to draw. What's the deal? I have seen that strokeWeight(),and stroke() is un-available in P3D, so I'm using the beginshape() method. Where's my logical error?
***********************************************************

//Let's get this finished

float xRot = 0.0;   //storage for current rotation values
float zRot = 0.0;
float bang;         //storage for mouse velocity values
float buck;
float bizz;

void setup() {
 background(255);
 size(500,500);
 frameRate(10);
}

void draw() {
 
 bang = mouseX - pmouseX;      //calculate mouse velocity
 buck = mouseY - pmouseY;
 
  if(keyPressed==true) {       //a drawing 'method'
   switch(key) {
   case 'a' ;
     
     translate(mouseX,mouseY);
     beginShape();
     vertex(30, 20);
     vertex(85, 20);
     vertex(85, 75);
     vertex(30, 75);
     endShape();
   }
 }

if(mousePressed) {              //change rotation value
 xRot = xRot + buck;           //based on mouse velocity
 zRot = zRot + bang;
}

 rotateX(xRot);                //apply current rotations
 rotateZ(zRot);
 
 println(frameCount);
 println(xRot);
 println(zRot);

}

Re: P3D problem:
Reply #1 - Nov 5th, 2006, 12:17pm
 
I can see one error, your rotate commands happen after the draw, so are completely useless. draw is completely self contained, every time it's run every previous translate/rotate command is cleared, and everything starts from scratch. So you have to do your translate/rotate in full, before drawing, every frame.
Re: P3D problem:
Reply #2 - Nov 5th, 2006, 8:16pm
 
ImageBurn,

Here's a simple example that may help clarify some of these issues.
Code:

void setup() {
background(255);
size(500,500, P3D);
fill(0, 10);
noStroke();
}

void draw() {
translate(mouseX, mouseY);
rotateX(radians(frameCount));
rotateY(radians(frameCount*2));
rotateZ(radians(frameCount*1.25));

if(keyPressed) {
switch(key) {
case 's' :
square();
break;
case 'e' :
ellipse(0, 0, 44, 44);
break;
case 't' :
triangle(-27, 27, 0, -27, 27, 27);
}
}
}

void square(){
beginShape();
vertex(-27, -27);
vertex(27, -27);
vertex(27, 27);
vertex(-27, 27);
endShape(CLOSE);
}



Obviously the square() function isn't really necessary [you could more simply use rect()], but I thought seeing the implemention might help.

Sorry for the case statement formatting-seems to be a YaBB thing.
Re: P3D problem:
Reply #3 - Nov 5th, 2006, 10:34pm
 
Okay, that code you sent me does help clarify, but I ask you to look at this problem again as the program above doesn't draw. Also, I'm still confused about the rotate issue. It is contained in the draw() method in the original code, and the println output shows that is is keeping the value (rotX,etc) current. Thank You!
Re: P3D problem:
Reply #4 - Nov 5th, 2006, 10:44pm
 
The following code DOES work, but please notice it is lacking the P3D setting. Keeping the code the same as below, with the only addition of the P3D environment, causes it to cease drawing. How can I rectify this with the P3D set so I can rotate it?

void setup() {
 background(255);
 size(500,500);              //it will cease to draw if P3D
 fill(0, 10);                //is added
 noStroke();
}

void draw() {
 
 
 if(keyPressed) {
   switch(key) {
   case 's' :
square();
break;
   case 'e' :
   translate(mouseX,mouseY);
ellipse(0, 0, 44, 44);
break;
   case 't' :
   translate(mouseX,mouseY);
triangle(-27, 27, 0, -27, 27, 27);
   }
 }
}  

void square(){
 translate(mouseX,mouseY);
 beginShape();
 vertex(-27, -27);
 vertex(27, -27);
 vertex(27, 27);
 vertex(-27, 27);
 endShape(CLOSE);
}

Re: P3D problem:
Reply #5 - Nov 5th, 2006, 11:02pm
 
I've tried your code, and changing to P3D mode, and it seems to work in both modes. The result is different however.

If you move the background(255); to after size() (which should ALWAYS be the first thing in setup()) you get the same results from P3D and normal modes.
Re: P3D problem:
Reply #6 - Nov 6th, 2006, 5:53am
 
"...move the background(255); to after size()..."

oops. Thanks JohnG, I should have caught that when I messed with the original code.

ImageBurn, does the rotation issue make more sense now? As JohnG pointed out, you just need the rotation call prior to the drawing calls. For example, in the following the 1st rect won't be rotated, but the 2nd will be.

size(400, 400);
translate(width/2, height/2);
rect(-100, -100, 200, 200);
rotate(PI/4);
rect(-70, -70, 140, 140);
Re: P3D problem:
Reply #7 - Nov 6th, 2006, 6:16am
 
Iraq, JohnG, it works now; and tidier code will follow. That size() thing is something that I'm not sure I ever heard before. . . but looking at the reference now I see it most explicitly states such. I learned to write code on Processing Alpha 56 thru 64, and a lot has changed. . . especially in the reference.

-Thanks
Page Index Toggle Pages: 1