2025年4月17日 星期四

week09_11131301賴珮語

 2025-04-17 Week09
1. 複習: 繼續建出 Gundam 的關節結構
2. 主題: 鍵盤滑鼠切換關節、調整角度
3. 主題: 讀檔、寫檔
查看期中成績


課堂練習1
先小小複習一下上禮拜教的
//week09_1_gundam_head_body
PShape body,head;
void setup(){
  size(400,400,P3D);
  body=loadShape("body.obj");
  head=loadShape("head.obj");
}
void draw(){
  background(204);
  translate(200,300);
  sphere(10); //原點的球
  
  scale(10,-10,10); //y要上下再反過來
  shape(body,0,0);
  shape(head,0,0);
}

課堂練習2
加上TRT
//week09_2_gundam_head_body_push_trt_pop
PShape body,head;
void setup(){
  size(400,400,P3D);
  body=loadShape("body.obj");
  head=loadShape("head.obj");
}
void draw(){
  background(204);
  translate(200,300);
  sphere(10); //原點的球
  
  scale(10,-10,10); //y要上下再反過來
  shape(body,0,0);
  pushMatrix();
    translate(0,22.5);
    rotateY(radians(mouseX-200));
    rotateX(radians(mouseY-60));
    translate(0,-22.5);
    shape(head,0,0);
  popMatrix();
}


課堂練習3
複習完上週進度 接下來我們延續上周教的 現在繼續尋找下一個關節
//week09_3_gundam_uparm_upuparm_push_trt_pop
PShape uparm1,upuparm1;
void setup(){
  size(400,400,P3D);
  uparm1=loadShape("uparm1.obj");
  upuparm1=loadShape("upuparm1.obj");
}
void draw(){
  background(204);
  translate(200,300);
  sphere(3); //小一點比較清楚
  
  scale(10,-10,10);
  
  shape(upuparm1,0,0);//上上手臂
  pushMatrix();
    translate(-4.1,20); //再掛回去原來的位置
    rotateX(radians(mouseY));
    translate(4.1,-20); //把物體的旋轉中心放到座標中心
    
    //先用下面兩行找適合的數值
    //translate(mouseX/10.0,-mouseY/10.0); //一邊移動一邊找適合的數值
    //println(mouseX/10.0,-mouseY/10.0); //印出適合的數值 4.1,-19.9
    shape(uparm1,0,0); //上手臂
  popMatrix();
}

課堂練習4
剛剛做好了上手臂跟上上手臂 現在再加上一個手
//week09_4_gundam_uparm_upuparm_hand_push_trt_pop
PShape uparm1,upuparm1,hand1;
void setup(){
  size(400,400,P3D);
  uparm1=loadShape("uparm1.obj");
  upuparm1=loadShape("upuparm1.obj");
  hand1=loadShape("hand1.obj");
}
void draw(){
  background(204);
  translate(200,300);
  sphere(3); //小一點比較清楚
  
  scale(10,-10,10);
  
  shape(upuparm1,0,0);//上上手臂
  pushMatrix();
    translate(-4.1,20); //再掛回去原來的位置
    rotateZ(radians(mouseX));
    translate(4.1,-20); //把物體的旋轉中心放到座標中心
    
    shape(uparm1,0,0); //上手臂
    pushMatrix();
      translate(-4.5,16.9);
      rotateX(radians(mouseY));
      translate(4.5,-16.9);
      //translate(mouseX/10.0,-mouseY/10.0); //一邊移動一邊找適合的數值
      //println(mouseX/10.0,-mouseY/10.0); //印出適合的數值 4.5,-16.9
      shape(hand1,0,0);
    popMatrix();
  popMatrix();
}

課堂練習5
現在來用鍵盤加滑鼠控制關節
//week09_5_gundam_uparm_upuparm_hand_keyboard_mouse_angle
PShape uparm1,upuparm1,hand1;
void setup(){
  size(400,400,P3D);
  uparm1=loadShape("uparm1.obj");
  upuparm1=loadShape("upuparm1.obj");
  hand1=loadShape("hand1.obj");
}
float [] angle=new float[20]; //準備20個關節
int ID = 0; //關節的代碼 之後會用 angle[ID]來改變值
void keyPressed(){
  if(key=='1') ID = 1;
  if(key=='2') ID = 2;
}
void mouseDragged(){
  angle[ID] += mouseX-pmouseX; //X方向的移動 可改變某個關節的角度
}
void draw(){
  background(204);
  translate(200,300);
  sphere(3); //小一點比較清楚
  
  scale(10,-10,10);
  
  shape(upuparm1,0,0);//上上手臂
  pushMatrix();
    translate(-4.1,20); //再掛回去原來的位置
    rotateZ(radians(angle[1]));
    translate(4.1,-20); //把物體的旋轉中心放到座標中心
    
    shape(uparm1,0,0); //上手臂
    pushMatrix();
      translate(-4.5,16.9);
      rotateX(radians(angle[2]));
      translate(4.5,-16.9);
      //translate(mouseX/10.0,-mouseY/10.0); //一邊移動一邊找適合的數值
      //println(mouseX/10.0,-mouseY/10.0); //印出適合的數值 4.5,-16.96
      shape(hand1,0,0);
    popMatrix();
  popMatrix();
}

課堂練習6
讀檔(綠色螢光筆) 跟 存檔(黃色螢光筆)
//week09_6_save_saveStrings_loadStrings
void setup(){
  size(300,300);
  rect(10,10,80,80);
  rect(110,110,80,80);
  save("file.png"); //這個範例可以把某個畫面save
  another=loadStrings("lines.txt"); //如果順利讀到 就有陣列了
}
int x=10,y=10,ID=0;
void draw(){
  background(204);
  if(another!=null){
    int [] now =int(split(another[ID],' '));
    rect(now[0],now[1],80,80);
    ID = (ID+1) % another.length;
  }
  rect(x,y,80,80);
}
void mouseDragged(){
  x += mouseX-pmouseX;
  y += mouseY-pmouseY;
  String now = x + " " + y ; //現在座標的字串
  lines.add(now); //println(now)印出小黑
}
ArrayList<String> lines= new ArrayList<String>();
String [] another; //另外一個讀到的字串的資料結構
void keyPressed(){
  String [] arr=new String[lines.size()];
  lines.toArray(arr); //把ArrayList轉換成傳統的陣列 以便存檔
  if(key=='s') saveStrings("lines.txt" , arr);
}






沒有留言:

張貼留言