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
Z-axis in P3D (Read 1172 times)
Z-axis in P3D
Jan 14th, 2010, 11:46am
 
Hi,
I'm working on making a pair of eyes using a 3D renderer. And I made it so when the mouse is clicked, the eyes bulge out. Yet before the mouse is pressed the spheres are translated back 100. So I was able to draw ellipses on top of the spheres as pupils, yet how can I draw the pupils now that the sphere is bulging out in front of the spheres(whites)?

Code:

float x;
float y;
float x2;
float y2;
float easing = 0.07;

void setup(){
 size(640, 480, P3D);
 noStroke();
 background(0);
 //noCursor();
}

void draw(){
 if(pmouseX != 0 && pmouseY != 0){
 
 background(0);
   
 //moving pupils  
 float targetX = mouseX;
 float targetY = mouseY;
 x += (targetX - x) * easing;
 y += (targetY - y) * easing;
 fill(0);
 ellipse(x, y, 30, 30);
 x = constrain(x, 175, 275);
 y = constrain(y, 180, 300);
 
 //other pupil
 x2 += (targetX - x2) * easing;
 y2 += (targetY - y2) * easing;
 fill(0);
 ellipse(x2, y2, 30, 30);
 x2 = constrain(x2, 365, 470);
 y2 = constrain(y2, 180, 300);

 //whites
 float dirY = (mouseY / float(height) - 0.5) * 2;
 float dirX = (mouseX / float(width) - 0.5) * 2;
 directionalLight(204, 204, 204, -dirX, -dirY, -1);
 
 if(mousePressed && (mouseButton == LEFT)){
   fill(255);
   pushMatrix();
   translate(430, 240, random(100, 110)); //bulge!
   sphere(100); //65 w/o random
   popMatrix();
   
   pushMatrix();
   translate(210, 260, random(40, 50));
   sphere(100);
   popMatrix();
   
   //insert new constraints for ellipses
 }else{
   pushMatrix();
   fill(255);
   translate(210, 240, -100);
   sphere(100); //value = radius
   popMatrix();
   fill(255);
   translate(430, 240, -100);
   sphere(100);
   }
 }
}
Re: Z-axis in P3D
Reply #1 - Jan 15th, 2010, 11:19am
 
You are trying to mix 3D (sphere) and 2D (ellipse) and this will not work well since the ellipse will only be drawn on the z=0 plane so the eyeballs have to be moved back with a translate.

I suggest that you use spheres for the pupils as well as the eyeballs this gives you full control of their z positioning. Also don't use translate to make the eyes bulge, its like moving a book nearer to the eyes to make the print clearer when all you needed was a magnifying glass. The scale() function will allow you to increase the size of the eyballs without moving them. This is good because it make the math easier to calculate the coordinates of the pupil so its centre is on the surface of the eyeball.
Re: Z-axis in P3D
Reply #2 - Jan 16th, 2010, 11:54am
 
Actually, I think I'd prefer to use the translate(); function since if you scale something it automatically relocates it to the origin, so I'd have to translate anyway. But I'm just wondering: why is it that when I translate things forward on the z-axis the coordinates for the x and y axes seem altered? Like the coordinate points for x and y are thrown off =/
Re: Z-axis in P3D
Reply #3 - Jan 16th, 2010, 12:01pm
 
Quote:
But I'm just wondering: why is it that when I translate things forward on the z-axis the coordinates for the x and y axes seem altered?

The answer is perspective. Hold up 2 pencils 2-3cm apart at arms length, now translate them to 30cm from your nose they seem further apart i.e. translated on the x axis.

Quote:
I think I'd prefer to use the translate(); function since if you scale something it automatically relocates it to the origin, so I'd have to translate anyway.

If you perform the translate before the scale then you effectively scale the translation. If you scale before translate it should work OK.
Re: Z-axis in P3D
Reply #4 - Jan 18th, 2010, 2:19pm
 
Oh, i see, in that case it should work just fine. i can just scale the size of the actual sphere then translate that to where I wanted the eyes to be. Thanks for the help =]
Page Index Toggle Pages: 1