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 › Help to improve this script - 3D rotating scene
Page Index Toggle Pages: 1
Help to improve this script - 3D rotating scene (Read 709 times)
Help to improve this script - 3D rotating scene
May 1st, 2007, 4:15am
 
Hello.
I still havent clear how does the camera() function work: in particular the xUp yUp zUp parameters and how do I make my camera roate along the Y-up axis in order to see how's my object.

ANYWAY:
here's my program... it's pretty simple and is a very rough sketch of what I could end up with.
- It creates a series of segments in 3D
- Rotates the scene so it's posasible to understand the object drawn.

Please help me to improve the script as I will have to substitute the lines with complex polygons ! :-S

Code:

int[][] p = new int[5001][4];
int a = 0;
int[][] knot = new int[500][4];
int k = 1;



void setup() {
size(500,500,P3D);
calcP();
}


void draw()
{

//APPLY A TRANSFORMATION SUCH THAT THE SCENE IS 'CENTERED' (point[0,0,0] is at the middle of the window)
translate(width/2, height/2, 0);
//EVERYTIME THE draw() FUNCTION IS CALLED, ROTATES EVERYTHING BY AN INCREASING AMOUNT 'a'
rotateY(radians(a));
a++;

//CLEAR EVERYTHING
background(200);

/* Draw a yellow box at the center of the 'world'... useful only for debug
fill(255,255,0);
box(100);
/*


/* This part is to plot all the points present in the array p[][]
for(int i = 1; i< 4000; i++)
{
point(p[i][1], p[i][2], p[i][3]);
}
*/

//DRAWS A SHAPE MADE OF CONNECTED LINES WITH VERTICES CORRESPONDING TO THE KNOTS (knot[][])
beginShape(LINES);
for(int i = 1; i <= k-1; i++)
{
vertex(knot[i][1], knot[i][2], knot[i][3]);
vertex(knot[i+1][1], knot[i+1][2], knot[i+1][3]);
}
endShape();



}

void calcP()
{
int dirX, dirY, dirZ, sgmSteps, dirXold, dirYold, dirZold;
int posX, posY, posZ;
posX = posY = posZ = sgmSteps = dirX = dirY = dirZ = dirXold = dirYold = dirZold = 0;

for(int i = 1; i< 5000; i++) //Each loop calculates one point and puts the coordinates to an array with the points previously calculated
{

//WHEN THE SEGMENT HAS BEEN COMPLETED (sgmSteps == 0) CALCULATE A NEW KNOT WITH A DIFFERENT SET OF DIRECTIONS (X,Y,Z) AND SEGMENT LENGTH (steps)
if(sgmSteps == 0)
{
dirXold = dirX;
dirYold = dirY;
dirZold = dirZ;

sgmSteps = round(random(10,100));

while(dirX == dirXold && dirY == dirYold && dirZ == dirZold || dirX == -dirXold && dirY == -dirYold && dirZ == -dirZold)
{
dirX = round(pow(-1, round(random(1,2))));
dirY = round(pow(-1, round(random(1,2))));
dirZ = round(pow(-1, round(random(1,2))));
}

if(dirX == dirXold && dirY == dirYold && dirZ == dirZold || dirX == -dirXold && dirY == -dirYold && dirZ == -dirZold)
{
println("DUPLICATE");
}

println("-- NEW KNOT --");
println(dirX + " | " + dirY + " | " + dirZ);
knot[k][1] = posX;
knot[k][2] = posY;
knot[k][3] = posZ;
k++;
}
/////////////////////////////////////////////////////////////////////////////////

//NEW POINT POSITION
posX = posX + dirX;
posY = posY + dirY;
posZ = posZ + dirZ;

//SERIES OF CHECKINGS TO SEE IT THE NEW POINT GOES BEYOND THE SCREEN BOUNDRAIS
if(posX >= width/2 -1 | posX <= -width/2 +1)
{
dirX = -1 * dirX;
println("-- NEW KNOT --");
println(dirX + " | " + dirY + " | " + dirZ);
knot[k][1] = posX;
knot[k][2] = posY;
knot[k][3] = posZ;
k++;
}

if(posY >= height/2 -1| posY <= -height/2 +1)
{
dirY = -1 * dirY;
println("-- NEW KNOT --");
println(dirX + " | " + dirY + " | " + dirZ);
knot[k][1] = posX;
knot[k][2] = posY;
knot[k][3] = posZ;
k++;
}


if(posZ >= width/2 | posZ <= -width/2)
{
posZ = -1 * posZ;
println("-- NEW KNOT --");
println(dirX + " | " + dirY + " | " + dirZ);
knot[k][1] = posX;
knot[k][2] = posY;
knot[k][3] = posZ;
k++;
}
/////////////////////////////////////////////////////////////////////////////////

sgmSteps--; //decreases the length of the current segment because one more 'point' has been calculated

//ADDS THE POINT CALCULATED TO THE ARRAY CONTAING ALL THE POINTS (p[][])
p[i][1] = posX;
p[i][2] = posY;
p[i][3] = posZ;
}
}
Re: Help to improve this script - 3D rotating scen
Reply #1 - May 1st, 2007, 8:07pm
 
Um, so what's the question?  Is it just "how to set up a moving camera position that mimics this model rotation?"  Not sure why you'd want to make it more difficult than it has to be, but... replace your call to rotateY() with something like:

Code:

float camdist = 433f; // some value (but if too close you'll get ugly clip reflections w/ opengl)
camera(camdist*sin(radians(-a)), 0, camdist*cos(radians(-a)), 0,0,0, 0,1,0);


Those values should approximate your rotateY() call.  "a" will be backwards compared to rotateY() (to make the object look like its rotating clockwise, either rotate the object clockwise, or rotate the camera counter-clickwise, make sense?)  sin/cos are probably backwards from where you might expect them only to align the camera on Z at 0 rotation. (otherwise when a==0 puts camera on X axis)
Re: Help to improve this script - 3D rotating scen
Reply #2 - May 1st, 2007, 11:54pm
 
Thanks.
When I tried to make the camera rotate I had troubles because I was both too close to the object and because the center of view was wrong.

What I'm asking here anyway are some tricks on how to speed up the drawing / animation of the scene
Page Index Toggle Pages: 1