Processing(二)鼠标跟随+小球碰撞

float x1, y1, r1; // The coordinates and radius of the first circle
float x2, y2, r2; // The coordinates and radius of the second circle
float vx1, vy1;   // The speed of the first circle
float vx2, vy2;   // The speed of the seconde circle
//random color of two circles
color randColor1 = color(random(255), random(255), random(255));
color randColor2 = color(random(255), random(255), random(255));
float lengthCat;  // The length of the cat's head
// table for csv
Table xy;
int index = 0;

void setup(){
  size(1400, 900);
  // initialize the first circle
  x1 = width * 0.2;
  y1 = height * 0.5;
  r1 = random(30,150);
  vx1 = random(2,12);
  vy1 = random(2,12);
  
  // initialize the second circle
  x2 = width * 0.5;
  y2 = height * 0.7;
  r2 = random(30,150);
  vx2 = random(2,12);
  vy2 = random(2,12);
  
  //number from csv
  xy = loadTable("https://eif-research.feit.uts.edu.au/api/csv/?rFromDate=2024-08-20T08%3A00&rToDate=2024-08-20T22%3A00&rFamily=wasp&rSensor=ES_B_11_428_3EA4&rSubSensor=TCA","csv");
  float y = xy.getInt(index,1);
  lengthCat = 10 * y;
  println(y);
}

void draw(){
  background(128,216,252);
  //cat's head
  head(lengthCat);
  // move circles
  x1 += vx1;
  y1 += vy1;
  x2 += vx2;
  y2 += vy2;
  
  // check collision of boundaries
  checkBoundaryCollision();
  // check collision of circles
  checkCircleCollision();
  //check collision between mouse and circles
  checkMouseCollision();
  
  // draw circles
  fill(randColor1);
  ellipse(x1, y1, r1*2, r1*2);
  fill(randColor2);
  ellipse(x2, y2, r2*2, r2*2);
}

void checkBoundaryCollision(){
  // first circle
  if (x1 - r1 < 0 || x1 + r1 > width) {
    vx1 *= -1;
  }
  if (y1 - r1 < 0 || y1 + r1 > height) {
    vy1 *= -1;
  }
  
  // second circle
  if (x2 - r2 < 0 || x2 + r2 > width) {
    vx2 *= -1;
  }
  if (y2 - r2 < 0 || y2 + r2 > height) {
    vy2 *= -1;
  }
}

void checkMouseCollision(){
  // distant of two circles
  float dx1 = x1 - mouseX;
  float dy1 = y1 - mouseY;
  float distance1 = sqrt(dx1 * dx1 + dy1 * dy1);
  float dx2 = x2 - mouseX;
  float dy2 = y2 - mouseY;
  float distance2 = sqrt(dx2 * dx2 + dy2 * dy2);
  // condition
  if (distance1 <= r1 + lengthCat/2) {
    // reverse the speed
    vx1 *= -1;
    vy1 *= -1;
  }
  if (distance2 <= r2 + lengthCat/2) {
    // reverse the speed
    vx2 *= -1;
    vy2 *= -1;
  }
}

void checkCircleCollision(){
  // distant of two circles
  float dx = x2 - x1;
  float dy = y2 - y1;
  float distance = sqrt(dx * dx + dy * dy);
  
  // condition
  if (distance <= r1 + r2) {
    // reverse the speed
    vx1 *= -1;
    vy1 *= -1;
    vx2 *= -1;
    vy2 *= -1;
  }
}

void head(float a){
  //translate coordinates
  pushMatrix();
  float centreX = mouseX;
  float centreY = mouseY;
  translate(centreX, centreY);
  //unit a
  float headlength = a;
  float headheight = a;
  //ears
  //reference point
  float earpoint_1X = 0.1*a;
  float earpoint_1Y = 0.3*a;
  float eartipX = 0.5*a;
  float eartipY = 0.7*a;
  float earpoint_2X = 0.25*a;
  float earpoint_2Y = 0.3*a;
  //reference point for outer ear
  float earouter_X = 0.4*a;
  float earouter_Y = 0.1*a;
  stroke(0);
  strokeWeight(2);
  fill(180,153,154);
  //ear_triangle
  beginShape();
  vertex(-earpoint_1X, -earpoint_1Y);
  vertex(-eartipX, -eartipY);
  vertex(-earpoint_2X, -earpoint_2Y);
  endShape(CLOSE);
  beginShape();
  vertex(earpoint_1X, -earpoint_1Y);
  vertex(eartipX, -eartipY);
  vertex(earpoint_2X, -earpoint_2Y);
  endShape(CLOSE);
  //ear_outer
  stroke(0);
  strokeWeight(2);
  noFill();
  line(-eartipX, -eartipY, -earouter_X, -earouter_Y);
  line(eartipX, -eartipY, earouter_X, -earouter_Y);
  //outer contour
  stroke(0);
  strokeWeight(2);
  fill(180,153,154);
  ellipse(0, 0, headlength, headheight);
  //lower face
  stroke(255);
  strokeWeight(0);
  fill(255);
  arc(0,0,headlength, headheight, 0,PI);
  //Contour of upper face
  strokeWeight(0);
  beginShape();
  vertex(-0.5*headlength, 0);
  bezierVertex(-0.25*headlength, 0, -0.25*headlength, -0.2*headheight, 0, -0.4*headheight);
  bezierVertex(0.25*headlength, -0.2*headheight, 0.25*headlength, 0, 0.5*headlength, 0);
  endShape(CLOSE);
  //eyes
  stroke(0);
  strokeWeight(2);
  fill(255);
  ellipse(-0.25*a,-0.05*a,0.35*a,0.35*a); //left eye
  ellipse(0.25*a,-0.05*a,0.35*a,0.35*a); //right eye
  fill(0);
  ellipse(-0.23*a,-0.04*a,0.2*a,0.2*a); //left eyeball
  ellipse(0.23*a,-0.04*a,0.2*a,0.2*a); //right eyeball
  //nose
  stroke(0);
  strokeWeight(2);
  fill(0);
  arc(0, 0.11*a, 0.08*a, 0.08*a, radians(225), radians(315));
  //mouth
  stroke(0);
  strokeWeight(3);
  noFill();
  beginShape();
  curveVertex(-0.25*a, 0.2*a);
  curveVertex(-0.12*a, 0.28*a);
  curveVertex(0, 0.2*a);
  curveVertex(0.12*a, 0.28*a);
  curveVertex(0.25*a, 0.2*a);
  endShape();
  // Whiskers
  stroke(180,153,154);
  strokeWeight(3);
  float x = 0.23*a;
  float y = 0.18*a;
  float whiskerlength = 0.35*a;
  line(-x,y,-x-whiskerlength,y-0.1*a);
  line(x,y,x+whiskerlength,y-0.1*a);
  line(-x,y+0.02*a,-x-whiskerlength,y+0.02*a);
  line(x,y+0.02*a,x+whiskerlength,y+0.02*a);
  line(-x,y+0.04*a,-x-whiskerlength,y+0.14*a);
  line(x,y+0.04*a,x+whiskerlength,y+0.14*a);
  popMatrix();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值