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 › Program of a model 3D of train
Page Index Toggle Pages: 1
Program of a model 3D of train (Read 459 times)
Program of a model 3D of train
Nov 13th, 2008, 2:13am
 
I have a project to conduct a 3D model train and then allow the steering of the model from the network using the protocol osc.
Here is the beginning of my code, I want to know how to make a cylinder to represent the wheels of tran.
Thank you.

###################

int sval, x;

void setup()
{
size(600, 600, P3D);
noStroke();
}
void draw()
{
lights();
background(388);
pushMatrix();
translate(500, 300, 0);
box(60);
translate(-20, 0, 0);
box(30);
translate(-45, 0, 0);
box(60);
translate(-20, 0, 0);
box(30);
translate(-45, 0, 0);
box(60);
translate(-20, 0, 0);
box(30);
translate(-45, 0, 0);
box(60);
translate(-20, 0, 0);
box(30);
translate(-45, 0, 0);
box(60);
translate(0, -20, 0);
box(30,80,30);
popMatrix();
sval+=1;
}

###################
Re: Program of a model 3D of train
Reply #1 - Nov 13th, 2008, 11:51pm
 
The search field is a good start.

http://processing.org/hacks/hacks:cylinder?s=cylinder
Re: Program of a model 3D of train
Reply #2 - Nov 30th, 2008, 2:05pm
 
Thank you for your help.
Now, I'm looking how to do the circuit of the train, I thought of making circles or curves, but I think it can not give good results, someone can give me an idea to make the circuit?

%%%%%%
int sval;
float x;
void setup()
{
 

size(800, 600, P3D);
noStroke();


}
void draw(){

lights();
background(388);

train();
roue();
circuit();


sval+=1;
}

void train(){
color c1 = color(288, 333, 0);
translate(700,300,0);  

fill(c1);
box(50);
translate(-20, 0,0 );
box(40,20,30);
translate(-45, 0, 0);
box(50);
translate(-20, 0,0 );
box(40,20,30);
translate(-45, 0, 0);
box(50);
translate(-20, 0,0 );
box(40,20,30);
translate(-45, 0, 0);
box(50);
translate(-20, 0,0 );
box(40,20,30);
translate(-45, 0, 0);
box(50);
translate(0, -38,0 );
box(25,25,15);

}

void roue(){
translate(0,73,15);
pushMatrix();
rotateZ(PI/2);
rotateX(PI/2);
color c1 = color(204, 22, 0);
fill(c1);

pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,-30,0);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,-65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,-65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,-65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,-65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
translate(0,0,-65);
pushMatrix();
rotateY(sval*0.01);
cylinder(10,10,15);
popMatrix();
popMatrix();
}

void circuit(){
 

}

void cylinder(float w, float h, int sides)
{
 float angle;
 float[] x = new float[sides+1];
 float[] z = new float[sides+1];

 //get the x and z position on a circle for all the sides
 for(int i=0; i < x.length; i++){
   angle = TWO_PI / (sides) * i;
   x[i] = sin(angle) * w;
   z[i] = cos(angle) * w;
 }

 //draw the top of the cylinder
 beginShape(TRIANGLE_FAN);
 
 vertex(0,   -h/2,    0);

 for(int i=0; i < x.length; i++){
   vertex(x[i], -h/2, z[i]);
 }

 endShape();

 //draw the center of the cylinder
 beginShape(QUAD_STRIP);

 for(int i=0; i < x.length; i++){
   vertex(x[i], -h/2, z[i]);
   vertex(x[i], h/2, z[i]);
 }

 endShape();
 
 //draw the bottom of the cylinder
 beginShape(TRIANGLE_FAN);
 
 vertex(0,   h/2,    0);

 for(int i=0; i < x.length; i++){
   vertex(x[i], h/2, z[i]);
 }

 endShape();
}

%%
Re: Program of a model 3D of train
Reply #3 - Dec 26th, 2008, 11:45am
 
I finished the design part of the train, now I want to use the protcole OSC as a remote control, I want to have a small example using the OSC and apply it to my example. Here is my code:

int sval,sval1, sval2,sval3,test, cnd=0, cnd1=0, cnd2=0, cnd3=0,cnd4=0,cnd5=0;
float x;

void setup()
{
size(1250, 600, P3D);
noStroke();
}
void draw(){
 lights();
background(22,111,303);
perspective();


if (keyPressed){
 if (key=='q')  {cnd2=1;cnd3=0;}
 if (key=='s')  {cnd2=0;cnd3=1;}
 if (key == 'd')  {cnd2=0;cnd3=0;}
 }
if (cnd2==1 && cnd3==0) sval2+=1;
rotateX(sval2*0.003);  

if (cnd2==0 && cnd3==1) sval3+=1;
rotateX(-sval3*0.003);  
plan();
circuit();

if (keyPressed)
 {
   if (key == 'a')  cnd=0;
   if (key == 'z' && cnd1!=1 ) {cnd=1;cnd1=0;  }  
  if (key == 'a' ) {cnd1=0;cnd4=0;cnd5=0;}  
  if (key == 'e' && cnd!=1 ) {cnd1=1;cnd=0; }
  if (key == 'r' ) {cnd4=1;cnd5=0;}
  if (key == 't' ) {cnd5=1;cnd4=0;}
 
       
 }
if ((cnd==1 && cnd1==0)|(cnd==1 && cnd1==0 && cnd5==1)) sval+=1;
if (cnd==1 && cnd1==0 && cnd4==1) sval+=2;

rotate(sval*0.01);

if ((cnd1==1 && cnd==0)|(cnd1==1 && cnd==0 && cnd5==1)) sval1+=1;
if (cnd1==1 && cnd==0 && cnd4==1) sval1+=2;
rotate(-sval1*0.01);



 

trainpartie1();
trainpartie2();
trainpartie3();
roue();


}

void plan(){
 pushMatrix();
 noStroke();

 rotateX(PI/4);
  translate(630,0,-198);
 fill(11,330,44);
 box(850,850,20);
 
 
 translate(-330,370,60);
 rotateX(PI/2);
 fill(#8B4513);
 cylinder(10,100,300);
 translate(0,40,0);
   fill(11,130,44);
 sphere(40);
 translate(640,0,0);
 sphere(40);
 translate(0,-40,0);
 fill(#8B4513);
 cylinder(10,100,300);
 translate(0,0,680);
 cylinder(10,100,300);
 translate(0,40,0);
   fill(11,130,44);
 sphere(40);
 translate(-640,0,0);
 sphere(40);
 translate(0,-40,0);
 fill(#8B4513);
 cylinder(10,100,300);
 popMatrix();
 
 }
 
 void circuit(){
   translate(670,280,15);
 rotateX(PI/4);
 translate(-40,-180,0);
 noFill();
stroke(0);
ellipse(0, 0, 760, 760);
ellipse(0, 0, 740, 740);
ellipse(0, 0, 710, 710);
ellipse(0, 0, 690, 690);

for (int i = 0; i < 63;  i++)
{
 
rotate( 0.05);
fill(0);
rect(0,355, 5, 15);
rotateZ(PI);
rect(0,355, 5, 15);
}
}

void trainpartie1(){
 
 pushMatrix();
 noStroke();
 fill(#FF0000);
 
translate(0,360,45);
 box(50);



popMatrix();
rotate(0.2);
pushMatrix();
 noStroke();
 fill(#FF4500);
 
translate(0,360,45);
 box(50);



popMatrix();

rotate(0.2);
pushMatrix();
 noStroke();
 fill(#1E90FF);
 
translate(0,360,45);
 box(50);



popMatrix();

rotate(0.2);
pushMatrix();
 noStroke();
 fill(#FFFF00);
 
translate(0,360,45);
 box(50);



popMatrix();

rotate(0.2);
pushMatrix();
 noStroke();
 fill(#4B0082);
 
translate(0,360,45);
 box(50);



popMatrix();
 }
 
 void trainpartie2(){
   rotate(-1);
   for (int i = 0; i < 4;  i++)
{
  rotate(0.2);
   pushMatrix();
 noStroke();
 fill(#000000);
translate(-40,360,45);
box(40,20,30);
popMatrix();
}
}

void trainpartie3(){
rotate(0.2);
pushMatrix();
noStroke();
fill(#FFA500);
translate(0,360,85);
box(25,25,30);
popMatrix();

rotate(-0.8);
pushMatrix();
noStroke();
fill(#FFA500);
translate(0,360,85);
box(25,25,30);
popMatrix();
 }

void roue(){
 rotate(-0.2);
 
 test+=1;
 for (int i = 0; i < 5;  i++)
{
  rotate(0.2);
 pushMatrix();
 fill(#C0C0C0);

translate(0,350,10);


rotateY(sval*0.1);
rotateY(-sval1*0.1);


cylinder(10,10,15);
translate(0,25,0);
cylinder(10,10,15);
popMatrix();
 
 }
 }
 


void cylinder(float w, float h, int sides)
{
 float angle;
 float[] x = new float[sides+1];
 float[] z = new float[sides+1];

 //get the x and z position on a circle for all the sides
 for(int i=0; i < x.length; i++){
   angle = TWO_PI / (sides) * i;
   x[i] = sin(angle) * w;
   z[i] = cos(angle) * w;
 }

 //draw the top of the cylinder
 beginShape(TRIANGLE_FAN);
 
 vertex(0,   -h/2,    0);

 for(int i=0; i < x.length; i++){
   vertex(x[i], -h/2, z[i]);
 }

 endShape();

 //draw the center of the cylinder
 beginShape(QUAD_STRIP);

 for(int i=0; i < x.length; i++){
   vertex(x[i], -h/2, z[i]);
   vertex(x[i], h/2, z[i]);
 }

 endShape();
 
 //draw the bottom of the cylinder
 beginShape(TRIANGLE_FAN);
 
 vertex(0,   h/2,    0);

 for(int i=0; i < x.length; i++){
   vertex(x[i], h/2, z[i]);
 }

 endShape();
}

 
 void perspective() {
 lights();
 
 float cameraY = height/2.0;
 float fov = mouseX/float(width) * PI/2;
 float cameraZ = cameraY / tan(fov / 2.0);
 float aspect = float(width)/float(height);
 
 perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
 
 
 
}
Page Index Toggle Pages: 1