2023年11月2日 星期四

SangMo 互動技術 Week08

碰撞偵測

Step1 牛頓力學

程式碼:

///碰撞偵測

void setup(){
  size(400,400);
}
float x=200,y=200,vx=1,vy=1;
void draw(){
  ellipse(x,y,10,10);
  x+=vx;
  y+=vy;
}
執行圖:

Step2 靜者恆靜,動者恆動, f=ma
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float x=50,y=250,vx=2,vy=-20;
void draw(){
  ellipse(x,y,10,10);
  x+=vx;
  y+=vy;
  vy += 0.98;//重力加速度
}
執行圖:
Step3 if判斷落點
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float x=50,y=250,vx=2,vy=-20;
void draw(){
  ellipse(x,y,10,10);
  x+=vx;
  y+=vy;
  vy += 0.98;//重力加速度
  if(y>350){
    y=350;
  }
  ellipse(x,y,10,10);
}
執行圖:

Step3-2 給予能量耗損
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float x=50,y=250,vx=2,vy=-20;
void draw(){
  ellipse(x,y,10,10);
  x+=vx;
  y+=vy;
  vy += 0.98;//重力加速度
  if(y>350){
    y=350;
    vy = -vy * 0.5; //能量耗損、速度變慢
  }
  ellipse(x,y,10,10);
}
執行圖:

Step4 馬力歐移動製作  keyPressed
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
void draw(){
 background(108,137,255);
 marioX += vx;
 fill(255,0,0);ellipse(marioX,marioY,15,25);
 fill(229,119,42);rect(0,260,400,150);
}
void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
}
執行圖

Step5 馬力歐移動製作  Released 往上
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
void draw(){
 background(108,137,255);
 marioX += vx;
 marioY += vy;
 fill(255,0,0);ellipse(marioX,marioY,15,25);
 fill(229,119,42);rect(0,260,400,150);
}
void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
  if(keyCode==UP)vy=-20;
}
void keyReleased(){
  if(keyCode==LEFT || keyCode == RIGHT)vx=0;
}
執行圖:

Step6 馬力歐跳躍
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
void draw(){
 background(108,137,255);
 marioX += vx;
 if(flying){
   marioY += vy;//上下的位置會改變
   vy+=0.98;
   if(marioY >=250){
     marioY=250;
     flying = false;
   }
 }
 fill(255,0,0);ellipse(marioX,marioY,15,25);
 fill(229,119,42);rect(0,260,400,150);
}
boolean flying=false;
void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
  if(keyCode==UP){
    vy=-20;
    flying = true;
  } 
}
void keyReleased(){
  if(keyCode==LEFT || keyCode == RIGHT)vx=0;
}
執行圖:
Step7 馬力歐跳躍
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
void draw(){
 background(108,137,255);
 marioX += vx;
 if(flying){
   marioY += vy;//上下的位置會改變
   vy+=0.98;
   if(marioY >=250){
     marioY=250;
     flying = false;
   }
 }
 fill(255,0,0);ellipse(marioX,marioY,15,20);
 fill(229,119,42);rect(0,260,400,150);
 fill(229,119,42);rect(200,150,20,20);
 if(hitBox(200,150,20,20)){
   vy = 0;
   marioY= 150+20;
 }
}
boolean hitBox(int x,int y,int w,int h){
  if(x<marioX && marioX < x+w && y<marioY && marioY<y+h)return true;
  else return false;
}
boolean flying=false;
void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
  if(keyCode==UP && flying == false){
    vy=-20;
    flying = true;
  } 
}
void keyReleased(){
  if(keyCode==LEFT || keyCode == RIGHT)vx=0;
}
執行圖:

Step8 馬力歐碰撞
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
void draw(){
 background(108,137,255);
 marioX += vx;
 if(flying){
   marioY += vy;//上下的位置會改變
   vy+=0.98;
   if(marioY >=250){
     marioY=250;
     flying = false;
   }
 }
 fill(255,0,0);ellipse(marioX,marioY,15,20);
 fill(229,119,42);rect(0,260,400,150);
 fill(229,119,42);rect(200,150,20,20);
 if(hitBox(200,150,20,20)){
   if(vy>0){
     marioY=150-10;
     flying=false;
   }else{
     vy = 0;
     marioY= 150+20+10;
   }
 }
}
boolean hitBox(int x,int y,int w,int h){
  if(x-7<marioX && marioX < x+w+7 && y-10<marioY && marioY<y+h+10)return true;
  else return false;
}
boolean flying=false;
void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
  if(keyCode==UP && flying == false){
    vy=-20;
    flying = true;
  } 
}
void keyReleased(){
  if(keyCode==LEFT || keyCode == RIGHT)vx=0;
}
執行圖:


Step9 讓馬力歐跳到方塊後能掉落
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
void draw(){
 background(108,137,255);
 marioX += vx;
 if(flying){
   marioY += vy;//上下的位置會改變
   vy+=0.98;
   if(marioY >=250){
     marioY=250;
     flying = false;
   }
 }
 fill(255,0,0);ellipse(marioX,marioY,15,20);
 fill(229,119,42);rect(0,260,400,150);
 fill(229,119,42);rect(200,150,20,20);
 if(hitBox(200,150,20,20)){
   if(vy>0){
     marioY=150-10;
     flying = false;
     stand_box = true;
   }else{
     vy = 0;
     marioY= 150+20+10;
   }
 }
 if(stand_box == true && flying == false && leaveBox(200,150,20,20)){
   stand_box = false;
   flying = true;
   vy=0;
  }
}
boolean leaveBox(int x,int y,int w,int h){
  if(x-7>marioX || marioX > x+w+7)return true;
  else return false;
}
boolean hitBox(int x,int y,int w,int h){
  if(x-7<marioX && marioX < x+w+7 && y-10<marioY && marioY<y+h+10)return true;
  else return false;
}
boolean flying=false,stand_box=false;
void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
  if(keyCode==UP && flying == false){
    vy=-20;
    flying = true;
  } 
}
void keyReleased(){
  if(keyCode==LEFT || keyCode == RIGHT)vx=0;
}
執行圖:
Step10 製造更多 碰撞箱方塊
程式碼:
///碰撞偵測

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
void draw(){
 background(108,137,255);
 marioX += vx;
 if(flying){
   marioY += vy;//上下的位置會改變
   vy+=0.98;
   if(marioY >=250){
     marioY=250;
     flying = false;
   }
 }
 fill(255,0,0);ellipse(marioX,marioY,15,20);
 fill(229,119,42);rect(0,260,400,150);
 drawAndTextBox(200,150,20,20);
 drawAndTextBox(150,100,20,20);
}
void drawAndTextBox(int x,int y,int w,int h){
  fill(229,119,42);rect(x,y,w,h);
 if(hitBox(x,y,w,h)){
   if(vy>0){
     marioY=y-10;
     flying = false;
     stand_box = true;
   }else{
     vy = 0;
     marioY= y+h+10;
   }
 }
 if(stand_box == true && flying == false && leaveBox(x,y,w,h)){
   stand_box = false;
   flying = true;
   vy=0;
  }
}
boolean leaveBox(int x,int y,int w,int h){
  if(x-7>marioX || marioX > x+w+7)return true;
  else return false;
}
boolean hitBox(int x,int y,int w,int h){
  if(x-7<marioX && marioX < x+w+7 && y-10<marioY && marioY<y+h+10)return true;
  else return false;
}
boolean flying=false,stand_box=false;
void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
  if(keyCode==UP && flying == false){
    vy=-20;
    flying = true;
    stand_box = false;///現在有2個以上變數處理 !
  } 
}
void keyReleased(){
  if(keyCode==LEFT || keyCode == RIGHT)vx=0;
}
執行圖:


沒有留言:

張貼留言