2020年12月21日 星期一

:D~week15

<Processing>

利用 PVector秀出圖片

size(1200,675);

PImage img=loadImage("img.jpg");

PVector pt=new PVector(10,20,0);

image(img,0,0);

println(pt.x);









利用PVector秀出一顆小球

PVector user;

void setup()

{

   size(600,600);

   user=new PVector(10,20);

}

void draw()

{

   background(255);

   ellipse(user.x,user.y,20,20);

}













利用PVector秀出類似彈弓

PVector user;

PVector Y;

void setup()

{

   size(600,400);

   user=new PVector(100,300);

   Y=new PVector(100,300);

}

void draw()

{

   background(255);

   line(user.x,user.y,Y.x,Y.y);

   textSize(40);

   fill(255,0,0);

   text("Y",Y.x,Y.y);

   fill(255);

   ellipse(user.x,user.y,20,20);

}

void mouseDragged()

{

   user.x=mouseX;

   user.y=mouseY;

}

void mouseReleased()

    

}










利用滑鼠球可以發射出去

PVector user;

PVector Y,v=null;

void setup()

{

   size(600,400);

   user=new PVector(100,300);

   Y=new PVector(100,300);

}

void draw()

{

   background(255);

   line(user.x,user.y,Y.x,Y.y);

   textSize(40);

   fill(255,0,0);

   text("Y",Y.x,Y.y);

   fill(255);

   ellipse(user.x,user.y,20,20);

   if(v!=null)user.add(v);

}

void mouseDragged()

{

   user.x=mouseX;

   user.y=mouseY;

}

void mouseReleased()

   PVector diff=PVector.sub(Y,user);

   v=diff.div(10);

}











給球重力

PVector user;

PVector Y,v=null;

void setup()

{

   size(600,400);

   user=new PVector(100,300);

   Y=new PVector(100,300);

}

void draw()

{

   background(255);

   if(mousePressed)line(user.x,user.y,Y.x,Y.y);

   textSize(40);

   fill(255,0,0);

   text("Y",Y.x,Y.y);

   fill(255);

   ellipse(user.x,user.y,20,20);

   if(v!=null)

   {

     user.add(v);

     v.y+=0.98;

   }

}

void mouseDragged()

{

   user.x=mouseX;

   user.y=mouseY;

   v=null;

}

void mouseReleased()

   PVector diff=PVector.sub(Y,user);

   v=diff.div(4);

}












搖桿移動

<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);

}












利用搖桿控制球

<Processing>

import processing.serial.*;

Serial myPort;

PVector user;

PVector Y,v=null;

void setup()

{

   size(600,400);

   myPort=new Serial(this,"COM4",9600);

   user=new PVector(100,300);

   Y=new PVector(100,300);

}

void draw()

{

   background(255);

   while(myPort.available()>=3)

   {

      int x=myPort.read();

      int y=myPort.read();

      int sw=myPort.read();

      user.x=100+x-128;

      user.y=300+y-128;

   }

   textSize(40);

   fill(255,0,0);

   text("Y",Y.x,Y.y);

   fill(255);

   ellipse(user.x,user.y,20,20);

   if(v!=null)

   {

     user.add(v);

     v.y+=0.98;

   }

}




沒有留言:

張貼留言