課堂作業1
//week11_1_postman_saveStrings_loadStrings_
//修改自week10_6_postman_many_angle_ID_saveStrings_loadStrings
//頭、身體、手臂、手肘、腳
PImage postman, head, body, uparm1, hand1, uparm2, hand2;
float [] angle = new float[20]; //準備20個關節的變數
int ID = 0; //現在要處理的關節ID(第幾個關節ID)
void mouseDragged(){
angle[ID] += mouseX - pmouseX;
}
void keyPressed(){ //按下按鈕可以轉動關節
if(key=='1') ID = 1; //左臂
if(key=='2') ID = 2; //左手
if(key=='3') ID = 3; //右臂
if(key=='4') ID = 4; //友手
if(key=='5') ID = 5; //(待用)
if(key=='6') ID = 6; //(待用)
if(key=='0') ID = 0; //頭
if(key=='s'){ //從這行開始 每次按一次 's' 就存一組angle
String now = ""; //要放現在全部關節的值
for(int i=0; i<20 ;i++){ //利用foe迴圈
now += angle[i] + " "; //全部塞到now裡,記得空格
}
lines.add(now); //把現在的這行 加到lines裡
String [] arr = new String [lines.size()];
lines.toArray(arr);
saveStrings("angles.txt", arr);
}
if(key=='r'){ //replay, 照之前案's'存檔的lines重播一次
if(R==0){ //如果目前的沒有任何東西
String [] file = loadStrings("angles.txt");
if(file!=null){ //如果有讀到檔案
for(int i=0; i<file.length; i++){ //就檔案內容逐行
lines.add(file[i]); //加到lines資料結構裡
}
}
}
if(R<lines.size()){
float [] now = float (split( lines.get(R), ' '));
for(int i=0; i<20; i++) angle[i] = now[i];
R = (R+1) % lines.size();
}
}
}
int R = 0; //一開始沒有任何東西
ArrayList<String> lines = new ArrayList<String>(); //放移動的結果
//到這裡結束
void setup(){
size(560, 560);
postman = loadImage("postman.png");
head = loadImage("head.png");
body = loadImage("body.png");
uparm1 = loadImage("uparm1.png");
hand1 = loadImage("hand1.png");
uparm2 = loadImage("uparm2.png");
hand2 = loadImage("hand2.png");
}
void draw(){
background(#FFFFF2);
image(postman, 0, 0); //基礎的郵差先生(全身)
fill(255, 0, 255, 128); //半透明的紫色
rect(0, 0, 560, 560); //蓋上去
pushMatrix();//左邊上手臂、手肘
translate(+185, +261); //再放回去正確的位置
rotate(radians(angle[1]));
translate(-185, -261);
image(uparm1, 0, 0); //再畫上手臂
pushMatrix();
translate(+116, +265); //放回去
rotate(radians(angle[2]));
translate(-116, -265);
image(hand1, 0, 0); //畫左手
popMatrix();
popMatrix();
pushMatrix(); //要畫右邊的上手臂、手肘
translate(+290, +262);
rotate(radians(angle[3]));
translate(-290, -262);
image(uparm2, 0 ,0); //上手臂
pushMatrix();
translate(+357, +259);
rotate(radians(angle[4]));
translate(-357, -259);
image(hand2, 0, 0); //右手
popMatrix();
popMatrix();
pushMatrix();
translate(+236, +231); //再放回去正確的位置
rotate(radians(angle[0]));
translate(-236, -231);//把頭的旋轉中心,放到(0, 0)
image(head, 0, 0); //再畫頭
popMatrix();
image(body, 0, 0); //再畫身體
}
課堂作業2
//week11_2_alpha_interpolation
//修改自week11_1_postman_saveStrings_loadStrings_
PImage postman, head, body, uparm1, hand1, uparm2, hand2;
float [] angle = new float[20]; //準備20個關節的變數
int ID = 0; //現在要處理的關節ID(第幾個關節ID)
void mouseDragged(){
angle[ID] += mouseX - pmouseX;
}
void keyPressed(){ //按下按鈕可以轉動關節
if(key=='1') ID = 1; //左臂
if(key=='2') ID = 2; //左手
if(key=='3') ID = 3; //右臂
if(key=='4') ID = 4; //友手
if(key=='5') ID = 5; //(待用)
if(key=='6') ID = 6; //(待用)
if(key=='0') ID = 0; //頭
if(key=='s'){ //從這行開始 每次按一次 's' 就存一組angle
String now = ""; //要放現在全部關節的值
for(int i=0; i<20 ;i++){ //利用foe迴圈
now += angle[i] + " "; //全部塞到now裡,記得空格
}
lines.add(now); //把現在的這行 加到lines裡
String [] arr = new String [lines.size()];
lines.toArray(arr);
saveStrings("angles.txt", arr);
}
if(key=='r'){ //replay, 照之前案's'存檔的lines重播一次
if(R==0){ //如果目前的沒有任何東西
String [] file = loadStrings("angles.txt");
if(file!=null){ //如果有讀到檔案
for(int i=0; i<file.length; i++){ //就檔案內容逐行
lines.add(file[i]); //加到lines資料結構裡
}
}
}
if(R<lines.size()){
float [] now = float (split( lines.get(R), ' '));
for(int i=0; i<20; i++) angle[i] = now[i];
R = (R+1) % lines.size();
}
}
if(key=='p'){ //按了r之後, 想要內插中間的結果
int R2 = (R+1)%lines.size();
float [] oldAngle = float (split( lines.get(R), ' '));
float [] newAngle = float (split( R2 ), ' '));
float alpha = (frameCount%30)/30.0; //介於0.0~1.0中間的值
for(int i=0; i<20; i++) angle[i] = oldAngle[i]*(1-alpha) + newAngle[i]*alpha;
}
}
int R = 0; //一開始沒有任何東西
ArrayList<String> lines = new ArrayList<String>(); //放移動的結果
//到這裡結束
void setup(){
size(560, 560);
postman = loadImage("postman.png");
head = loadImage("head.png");
body = loadImage("body.png");
uparm1 = loadImage("uparm1.png");
hand1 = loadImage("hand1.png");
uparm2 = loadImage("uparm2.png");
hand2 = loadImage("hand2.png");
}
void draw(){
background(#FFFFF2);
image(postman, 0, 0); //基礎的郵差先生(全身)
fill(255, 0, 255, 128); //半透明的紫色
rect(0, 0, 560, 560); //蓋上去
pushMatrix();//左邊上手臂、手肘
translate(+185, +261); //再放回去正確的位置
rotate(radians(angle[1]));
translate(-185, -261);
image(uparm1, 0, 0); //再畫上手臂
pushMatrix();
translate(+116, +265); //放回去
rotate(radians(angle[2]));
translate(-116, -265);
image(hand1, 0, 0); //畫左手
popMatrix();
popMatrix();
pushMatrix(); //要畫右邊的上手臂、手肘
translate(+290, +262);
rotate(radians(angle[3]));
translate(-290, -262);
image(uparm2, 0 ,0); //上手臂
pushMatrix();
translate(+357, +259);
rotate(radians(angle[4]));
translate(-357, -259);
image(hand2, 0, 0); //右手
popMatrix();
popMatrix();
pushMatrix();
translate(+236, +231); //再放回去正確的位置
rotate(radians(angle[0]));
translate(-236, -231);//把頭的旋轉中心,放到(0, 0)
image(head, 0, 0); //再畫頭
popMatrix();
image(body, 0, 0); //再畫身體
}
//week11_2_alpha_interpolation_better
//修改自week11_1_postman_saveStrings_loadStrings_
PImage postman, head, body, uparm1, hand1, uparm2, hand2;
float [] angle = new float[20]; //準備20個關節的變數
int ID = 0; //現在要處理的關節ID(第幾個關節ID)
void mouseDragged(){
angle[ID] += mouseX - pmouseX;
}
void keyPressed(){ //按下按鈕可以轉動關節
if(key=='1') ID = 1; //左臂
if(key=='2') ID = 2; //左手
if(key=='3') ID = 3; //右臂
if(key=='4') ID = 4; //友手
if(key=='5') ID = 5; //(待用)
if(key=='6') ID = 6; //(待用)
if(key=='0') ID = 0; //頭
if(key=='s'){ //從這行開始 每次按一次 's' 就存一組angle
String now = ""; //要放現在全部關節的值
for(int i=0; i<20 ;i++){ //利用foe迴圈
now += angle[i] + " "; //全部塞到now裡,記得空格
}
lines.add(now); //把現在的這行 加到lines裡
String [] arr = new String [lines.size()];
lines.toArray(arr);
saveStrings("angles.txt", arr);
}
if(key=='r'){ //replay, 照之前案's'存檔的lines重播一次
if(R==0){ //如果目前的沒有任何東西
String [] file = loadStrings("angles.txt");
if(file!=null){ //如果有讀到檔案
for(int i=0; i<file.length; i++){ //就檔案內容逐行
lines.add(file[i]); //加到lines資料結構裡
}
}
}
if(R<lines.size()){
float [] now = float (split( lines.get(R), ' '));
for(int i=0; i<20; i++) angle[i] = now[i];
R = (R+1) % lines.size();
}
}
// if(key=='p'){ //按了p之後, 想要內插中間的結果
// float alpha = (frameCount%30)/30.0; //介於0.0~1.0中間的值
// if(alpha==0.0) R = (R+1)%lines.size(); //如果變到
// int R2 = (R+1)%lines.size();
// float [] oldAngle = float(split( lines.get(R), ' '));
// float [] newAngle = float(split( lines.get( R2 ), ' '));
// for(int i=0; i<20; i++) angle[i] = oldAngle[i]*(1-alpha) + newAngle[i]*alpha;
// }
}
int R = 0; //一開始沒有任何東西
ArrayList<String> lines = new ArrayList<String>(); //放移動的結果
//到這裡結束
void setup(){
size(560, 560);
postman = loadImage("postman.png");
head = loadImage("head.png");
body = loadImage("body.png");
uparm1 = loadImage("uparm1.png");
hand1 = loadImage("hand1.png");
uparm2 = loadImage("uparm2.png");
hand2 = loadImage("hand2.png");
}
void myInterpolate(){
if(lines.size()>0){
float alpha = (frameCount%30)/30.0; //介於0.0~1.0中間的值
if(alpha==0.0) R = (R+1)%lines.size(); //如果變到0.0就換下一組
int R2 = (R+1)%lines.size();
float [] oldAngle = float(split( lines.get(R), ' '));
float [] newAngle = float(split( lines.get( R2 ), ' '));
for(int i=0; i<20; i++) angle[i] = oldAngle[i]*(1-alpha) + newAngle[i]*alpha;
}
}
void draw(){ //在void draw() 裡,加一行 myInterpolate();
myInterpolate(); //我的內插
background(#FFFFF2);
image(postman, 0, 0); //基礎的郵差先生(全身)
fill(255, 0, 255, 128); //半透明的紫色
rect(0, 0, 560, 560); //蓋上去
pushMatrix();//左邊上手臂、手肘
translate(+185, +261); //再放回去正確的位置
rotate(radians(angle[1]));
translate(-185, -261);
image(uparm1, 0, 0); //再畫上手臂
pushMatrix();
translate(+116, +265); //放回去
rotate(radians(angle[2]));
translate(-116, -265);
image(hand1, 0, 0); //畫左手
popMatrix();
popMatrix();
pushMatrix(); //要畫右邊的上手臂、手肘
translate(+290, +262);
rotate(radians(angle[3]));
translate(-290, -262);
image(uparm2, 0 ,0); //上手臂
pushMatrix();
translate(+357, +259);
rotate(radians(angle[4]));
translate(-357, -259);
image(hand2, 0, 0); //右手
popMatrix();
popMatrix();
pushMatrix();
translate(+236, +231); //再放回去正確的位置
rotate(radians(angle[0]));
translate(-236, -231);//把頭的旋轉中心,放到(0, 0)
image(head, 0, 0); //再畫頭
popMatrix();
image(body, 0, 0); //再畫身體
}
課堂作業3
//week11_3_postman_again
//重新來一次
PImage postman, head, body, uparm1, hand1, uparm2, hand2, foot1, foot2;
void setup(){ //把上面的圖,都拉進來
size(560, 560);
postman = loadImage("postman.png");
head = loadImage("head.png");
body = loadImage("body.png");
uparm1 = loadImage("uparm1.png");
hand1 = loadImage("hand1.png");
uparm2 = loadImage("uparm2.png");
hand2 = loadImage("hand2.png");
foot1 = loadImage("foot1.png"); //增加腳 旋轉中心 220,375
foot2 = loadImage("foot2.png"); //增加腳 旋轉中心 260,372
}
float [] angleX = new float[10]; //在3D的世界裡, 我們的旋轉,
float [] angleY = new float[10];
int ID = 0; //一開始是頭的關節
ArrayList<String> lines = new ArrayList<String>();
void keyPressed(){
if(key=='s'){ //現在的動作的所有關節,都存起來
String now = ""; //空字串
for(int i=0; i<10; i++){
now += angleX[i] + " "; //後面有空格
now += angleY[i] + " "; //後面有空格
}
lines.add(now); //把現在的動作這行,加到lines裡
String [] arr = new String[lines.size()];
lines.toArray(arr);
saveStrings("angles.txt", arr);
println("現在存檔:" + now);
}
if(key=='r'){
String [] file = loadStrings("angles.txt");
if(file != null){
for(int i=0; i<file.length; i++){
lines.add(file[i]);
println("現在讀檔:" +file[i]);
}
}
}
if(key=='p') playing = !playing; //播動畫 <=> 不播動畫
if(key=='1') ID = 1; //左臂
if(key=='2') ID = 2; //左手
if(key=='3') ID = 3; //右臂
if(key=='4') ID = 4; //友手
if(key=='5') ID = 5; //(待用)
if(key=='6') ID = 6; //(待用)
if(key=='0') ID = 0; //頭
}
boolean playing = false; //一開始,不播放動畫, 按下'p'可切換
void mouseDragged(){
angleX[ID] += mouseX - pmouseX;
angleY[ID] += mouseY - pmouseY; //多了這一行
}
int R=0;
void myInterpolate(){
if(lines.size()>=2){ //要有2行以上, 才能做內插
float alpha = (frameCount%30)/30.0; //介於0.0~1.0中間的值
if(alpha==0.0) R = (R+1)%lines.size(); //如果變到0.0就換下一組
int R2 = (R+1)%lines.size();
float [] oldAngle = float(split( lines.get(R), ' '));
float [] newAngle = float(split( lines.get(R2), ' '));
for(int i=0; i<10;i++){
angleX[i] = oldAngle[i*2+0]*(1-alpha) + newAngle[i*2+0]*alpha;
angleY[i] = oldAngle[i*2+0]*(1-alpha) + newAngle[i*2+0]*alpha;
}
}
}
void draw(){
background(#FFFFF2);
if(playing) myInterpolate();
image(body, 0, 0); //先畫身體
pushMatrix();//畫頭
translate(+236, +231); //再放回去正確的位置
rotate(radians(angleX[0]));
translate(-236, -231);//把頭的旋轉中心,放到(0, 0)
image(head, 0, 0); //再畫頭
popMatrix();
pushMatrix(); //腳foot1
translate(220, 375);
rotate(radians(angleX[5]));
translate(-220, -375);
image(foot1, 0, 0);
popMatrix();
pushMatrix(); //腳foot2
translate(260, 372);
rotate(radians(angleX[6]));
translate(-260, -372);
image(foot2, 0, 0);
popMatrix();
pushMatrix();//左邊上手臂、手肘
translate(+185, +261); //再放回去正確的位置
rotate(radians(angleX[1]));
translate(-185, -261);
image(uparm1, 0, 0); //再畫上手臂
pushMatrix();
translate(+116, +265); //放回去
rotate(radians(angleX[2]));
translate(-116, -265);
image(hand1, 0, 0); //畫手
popMatrix();
popMatrix();
pushMatrix(); //要畫右邊的上手臂、手肘
translate(+290, +262);
rotate(radians(angleX[3]));
translate(-290, -262);
image(uparm2, 0 ,0);
pushMatrix();
translate(+357, +259);
rotate(radians(angleX[4]));
translate(-357, -259);
image(hand2, 0, 0);
popMatrix();
popMatrix();
}




沒有留言:
張貼留言