2020年12月21日 星期一

喵~PVector~

 Proecssing~PVector~

  • 比較:
    • 以前:(僅宣告)
      • float uX=10,uY=20,uZ=0;
      • float u2X=30,u2Y=40,u2Z=0;
    • 現在:(可利用PVector裡寫好的函式庫)
      • PVector u=new PVector(10,20,0);
      • PVector u2=new PVector(30,40,0);
    • 注意:檔名不可取到函數名(如:PVector、int、PImage......)
  • 憤怒鳥(angry brid)
    • Processing程式
      • PVector bird;
      • PVector Y,v=null;
      • void setup(){
      •   size(600,400);
      •   bird=new PVector(150,300);//只在setup() new一次
      •   Y=new PVector(150,300);
      • }
      • void draw(){
      •   background(255);
      •   textSize(40);
      •   fill(255,0,200);
      •   text("Y",Y.x-10,Y.y+30);
      •   fill(255);
      •   ellipse(bird.x,bird.y,20,20);
      •   line(bird.x,bird.y,Y.x,Y.y);
      •   if(v!=null){
      •     bird.add(v);
      •     v.y+=0.98;
      •   }
      • }
      • void mouseDragged(){//滑鼠拖曳
      •   bird.x=mouseX;
      •   bird.y=mouseY;
      •   v=null;
      • }
      • void mouseReleased(){
      •   PVector diff=PVector.sub(Y,bird);//減法
      •   v=diff.div(4);//除法
      • }
  • 搖桿(x,y,sw)結合Processing
    • 搖桿
      • GND - GND
      • +5V - 5V
      • URX - A0
      • URY - A1
      • SW - 2
    • Arduino
      • void setup() {
      •   pinMode(2,INPUT_PULLUP);
      •   Serial.begin(9600);
      • }
      • void loop() {//1000Hz(1000fps)
      •   int x=analogRead(A0);//0~1023
      •   int y=analogRead(A1);//0~1023
      •   int sw=digitalRead(2);//HIGH放開
      •   Serial.write(x/4);//第1個byte
      •   Serial.write(y/4);//第2個byte
      •   Serial.write(sw);//第3個byte
      •   delay(20);//1000ms/20ms => 50fps,變慢
      • }
    • Processing程式
      • import processing.serial.*;
      • Serial myPort;
      • void setup(){
      •   size(256, 256);
      •   myPort = new Serial(this,"COM4",9600);
      • }
      • int ballX=0,ballY=0;
      • void draw(){
      •   if(myPort.available()>=3){//延遲少一點 if 改 while
      •     int x=myPort.read();//0~255變成-128~+127
      •     int y=myPort.read();
      •     int sw=myPort.read();
      •     ballX=x;
      •     ballY=y;
      •   }
      •   ellipse(ballX,ballY,10,10);
      • }
  • ↑憤怒鳥結合搖桿↑(Arduino同上↑)
    • import processing.serial.*;
    • Serial myPort;
    • PVector bird;
    • PVector Y,v=null;
    • void setup(){
    •   size(600,400);
    •   myPort = new Serial(this,"COM4",9600);
    •   bird=new PVector(150,300);
    •   Y=new PVector(150,300);
    • }
    • void draw(){
    •   background(255);
    •   if(myPort.available()>=3){
    •     int x=myPort.read();
    •     int y=myPort.read();
    •     int sw=myPort.read();
    •     bird.x=100+x-128;
    •     bird.y=900+x-128;
    •   }
    •   textSize(40);
    •   fill(255,0,200);
    •   text("Y",Y.x-10,Y.y+30);
    •   fill(255);
    •   ellipse(bird.x,bird.y,20,20);
    •   if(v!=null){
    •     bird.add(v);
    •     v.y+=0.98;
    •   }
    • }

                                  沒有留言:

                                  張貼留言