2025年3月13日 星期四

week04-12750113

 week04-1_ atan2_dy_dx_cos_sin

// week04_1_atan_atan2_dy_dx_cos_sin
void setup(){
  size(600,300);
}
void draw(){
  background(#C0FFEE); // 粉清咖啡色
  ellipse(150,150,100,100); // 大眼睛
  float dx = mouseX-150,dy = mouseY-150; 
  float a = atan2(dy,dx); // 算出角度
  ellipse(150+cos(a)*25,150+sin(a)*25,50,50);
  // ellipse(150+25,150,50,50);
}
中間的圓圈會跟著鼠標轉動(像是一直看著鼠標一樣)

week04_2_atan2_for_x_dx_dy_cos_sin
// week04_2_atan2_for_x_dx_dy_cos_sin
void setup(){
  size(600,300);
}
void draw(){
  background(#C0FFEE); // 粉清咖啡色
  for(int x =150;x<=450;x+=300){ // 迴圈,做出2個大眼睛
    ellipse(x,150,100,100); // 大眼睛
    float dx = mouseX-x,dy = mouseY-150; 
    float a = atan2(dy,dx); // 算出角度
    ellipse(x+cos(a)*25, 150+sin(a)*25,50,50);
  }
  
}
變成2個眼睛在跟著鼠標轉動

week03-3_rotateX_radians_mouseY
// week04-3_rotateX_radians_mouseY
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2,height/2);
  // rotateY(radians(mouseX));// 上周的左右轉
  rotateX(radians(-mouseY)); // 本週的上下轉,加負號是因為要跟著鼠標轉動,鼠標往下就要向下轉
  box(200);
}

會跟著鼠標上下轉動的方塊

week04-4_rotateZ
// week04_4_rotateZ
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2);
  rotateZ(radians(mouseX));
  ellipse(0,0,100,150);
}


x,y,z軸都會旋轉

week04-5_robot_arm_pushMatrix_T_R_T_box_popMatrix
// week04_5_robot_arm_pushMatrix_T_R_T_box_popMatrix
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2);
  pushMatrix();
    translate(0,100);
    box(50); // 台座
    pushMatrix();
      translate(0,-25);
      rotateZ(radians(mouseX));
      translate(0,-50);
      box(10,100,10); // 可轉動長條
    popMatrix();
  popMatrix();
}

中間的圓柱可以跟著鼠標畫個圓圈



week04-5a_分別註解
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2); // 移到畫面中心
      // 把以下2行,分別註解、排列組合觀察
      //rotateZ(radians(mouseX)); // 對z軸旋轉
      // translate(0,-50); // 把下端,移到中心
      box(10,100,10); // 可轉動長條
 
}
就一根長條不動
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2); // 移到畫面中心
      // 把以下2行,分別註解、排列組合觀察
      rotateZ(radians(mouseX)); // 對z軸旋轉
      // translate(0,-50); // 把下端,移到中心
      box(10,100,10); // 可轉動長條
 
}
對z軸旋轉

void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2); // 移到畫面中心
      // 把以下2行,分別註解、排列組合觀察
      // rotateZ(radians(mouseX)); // 對z軸旋轉
      translate(0,-50); // 把下端,移到中心
      box(10,100,10); // 可轉動長條
 
}
下端在中心
// week04_5a_rotateZ_translate_box
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2); // 移到畫面中心
      // 把以下2行,分別註解、排列組合觀察
      rotateZ(radians(frameCount)); // 對z軸旋轉
      translate(0,-50); // 把下端,移到中心
      box(10,100,10); // 可轉動長條
 
}
以中心點為圓心自動旋轉


week05_b_translate_mouseX_mouseY_rotateZ_translate
// week04_5b_translate_mouseX_mouseY_rotateZ_translate
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  // translate(width/2, height/2); // 移到畫面中心
      translate(mouseX,mouseY); // 加這行,位置會跟著鼠標移動
      rotateZ(radians(frameCount)); // 對z軸旋轉
      translate(0,-50); // 把下端,移到中心
      box(10,100,10); // 可轉動長條
 
}
長條自動旋轉
但位置是跟著鼠標移動

week04-6

會自轉的圓球
依樣可以跟著鼠標移動

week04-7_sun_earth
// week04-7_sun_earth
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2);
  sphere(50); // 太陽
  rotateY(radians(frameCount));
  pushMatrix();
    translate(150,0);
    rotateY(radians(frameCount));
    sphere(30); // 地球
  popMatrix();
}



很像地球繞著地球轉

week04-8_sun_earth_moon
// week04_8_sun_earth_moon
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2, height/2);
  sphere(50); // 太陽
  rotateY(radians(frameCount));
  pushMatrix();
    translate(150,0);
    rotateY(radians(frameCount));
    sphere(30); // 地球
    pushMatrix();
      translate(50,0);
      rotateY(radians(frameCount));
      sphere(10); // 月球
    popMatrix();
  popMatrix();
}
像是太陽、地球、月球在自轉、公轉

week04-9_earth_texture_image
// week04_9_earth_texture_image
// google:earth map texture 下載1張地球的地圖 earth
// 把圖檔拉到程式裡面
PImage img = loadImage("earth.jpg");
size(600,300);
image(img,0,0,600,300);

地球的素材

week04-10_earth_createShape_setTexture_shape
// week04_10_earth_createShape_setTexture_shape
// google:processing sphere texture 可找到程式
size(400,400,P3D);
PShape earth = createShape(SPHERE,100);
PImage img = loadImage("earth.jpg");
earth.setTexture(img);
shape(earth);
把材質貼上地球

week04-11_earth_setTexture_translate_rotate
// week04_11_earth_setTexture_translate_rotate
PShape earth;
void setup(){
  size(400,400,P3D);
  earth = createShape(SPHERE,100);
  PImage img = loadImage("earth.jpg");
  earth.setTexture(img);
}
void draw(){
  background(0);
  translate(width/2, height/2);
  rotateY(radians(frameCount));
  shape(earth);
}

一顆會自轉的地球

week04-12_moon_setTexture_translate_rotate
// week04_12_moon_setTexture_translate_rotate
PShape moon;
void setup(){
  size(400,400,P3D);
  moon = createShape(SPHERE,100);
  PImage img = loadImage("moon.jpg");
  moon.setTexture(img);
}
void draw(){
  background(0);
  translate(width/2, height/2);
  rotateY(radians(frameCount));
  shape(moon);
}
會自轉的月球

week04-13_sun_setTexture_translate_rotate
// week04_13_sun_setTexture_translate_rotate
PShape sun;
void setup(){
  size(400,400,P3D);
  sun = createShape(SPHERE,100);
  PImage img = loadImage("sun.jpg");
  sun.setTexture(img);
}
void draw(){
  background(0);
  translate(width/2, height/2);
  rotateY(radians(frameCount));
  shape(sun);
}
會自轉的太陽

week04-14_sun_earth_moon_setTexture
// week04_14_sun_earth_moon_setTexture
PShape sun, earth, moon;
// 樓下,剪貼自week04_8,樓上,是week04_11、12、13
void setup(){
  size(400,400,P3D);
  sun = createShape(SPHERE,50);
  PImage img = loadImage("sun.jpg");
  sun.setTexture(img);
  
  earth = createShape(SPHERE,30);
  img = loadImage("earth.jpg");
  earth.setTexture(img);
  
  moon = createShape(SPHERE,10);
  img = loadImage("moon.jpg");
  moon.setTexture(img);
  
  
}
void draw(){
  background(128);
  translate(width/2, height/2);
  rotateY(radians(frameCount));
  shape(sun); // sphere(50) // 太陽
  pushMatrix();
    translate(150,0);
    rotateY(radians(frameCount));
    shape(earth); // sphere(30) // 地球
    pushMatrix();
      translate(50,0);
      rotateY(radians(frameCount));
      shape(moon); // sphere(10) // 月球
    popMatrix();
  popMatrix();
}
太陽、地球、月球都會自轉+公轉





















































沒有留言:

張貼留言