課堂作業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
//用push pop 讓頭能轉動
//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-70)); //讓頭上下轉動
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, 19.9); //再掛回去原本的位置
rotateX(radians(mouseY));
translate(4.1, -19.9);//把物體的旋轉值,放到座標中心
//translate(mouseX/10.0, -mouseY/10.0); //一邊移動、一邊找到數值
//println(mouseX/10.0, -mouseY/10.0); //印出適合的數值 ex.. 4.1 -19.9
shape(uparm1, 0, 0); //上手臂
popMatrix();
}
課堂作業4
//加上手
//week09_4_gundam_uparm_upuparm_head_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, 19.9); //再掛回去原本的位置
rotateZ(radians(mouseX));
translate(4.1, -19.9);//把物體的旋轉值,放到座標中心
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); //印出適合的數值 ex.. 4.5 -16.9
shape(hand1,0,0);
popMatrix();
popMatrix();
}
課堂作業5
//鍵盤滑鼠切換關節、調整角度
//week09_5_gundam_uparm_upuparm_head_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, 19.9); //再掛回去原本的位置
rotateZ(radians(angle[1])); //按下鍵盤1 滑鼠按著可以移動關節
translate(4.1, -19.9);//把物體的旋轉值,放到座標中心
shape(uparm1, 0, 0); //上手臂
pushMatrix();
translate(-4.5, 16.9);
rotateX(radians(angle[2]));//按下鍵盤2 滑鼠按著可以移動關節
translate(4.5, -16.9); //剛剛把手移到座標中心的移動量
//translate(mouseX/10.0, -mouseY/10.0); //一邊移動、一邊找到數值
//println(mouseX/10.0, -mouseY/10.0); //印出適合的數值 ex.. 4.5 -16.9
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");//這個範例, 可把某個畫面,szve(),存到硬碟裡
another = loadStrings("lines.tex"); //如果順利讀到, 就有陣列了
}
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>(); //ArrayList 資料結構
String [] another; //另外一個讀到的字串的資料結構(一開始沒有東西)
void keyPressed(){
String[] arr = new String[lines.size()]; //傳統的 Java 陣列的資料結構
lines.toArray(arr); //把ArrayList轉換成傳統的 陣列,以便存檔
if(key=='s') saveStrings("lines.tex", arr); //按下英文小寫 s 會存檔
}
沒有留言:
張貼留言