xxxxxxxxxx
161
let serial;
let portName = '/dev/cu.usbserial-14130';
let star;
let starNum = 12;
let stars = [];
let circlex;
let circley;
let video;
let poseNet;
let poses = [];
let offsetX=300;
let offsetY=200;
let capture = false;
function preload() {
star = loadImage('star.png');
star.resize(200,200);
}
function setup() {
createCanvas(1200, 720);
//serial communication
serial = new p5.SerialPort();
serial.open(portName);
//crystal ball
circlex = width / 2;
circley = height - 200;
//video
video = createCapture(VIDEO);
video.size(windowWidth, windowHeight);
video.hide();
//posenet
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', function(results) {
poses = results;
});
//stars
for (i = 0; i < starNum; i++) {
stars[i] = new Star(i * width / 12 + random(-60, 60), 160 + random(-60, 60));
}
}
function modelReady() {
console.log('model ready');
}
function draw() {
translate(width, 0)
scale(-1, 1);
image(video, 0, 0, width, height);
background(0);
// stroke(255);
// strokeWeight(5);
stroke(0);
noFill();
circle(circlex, circley, 100);
for (i = 0; i < starNum; i++) {
stars[i].draw();
}
if (poses[0]) {
fill(255);
circle(poses[0].pose.rightWrist.x+offsetX, poses[0].pose.rightWrist.y+offsetY, 20);
for (let i = 0; i < starNum; i++) {
dis1 = dist(poses[0].pose.rightWrist.x+offsetX, poses[0].pose.rightWrist.y+offsetY, stars[i].x, stars[i].y);
dis2 = dist(stars[i].x, stars[i].y, circlex, circley);
// if(dis1<30&& dis2 > 25){stars[i].x+=50;stars[i].y+=50;stars[i].escape++;}
if (dis1 < 25 && dis2 > 25) {
stars[i].capture = true;
}
if (stars[i].capture) {
stars[i].x = poses[0].pose.rightWrist.x+offsetX;
stars[i].y = poses[0].pose.rightWrist.y+offsetY
}
if (dis2 < 25) {
stars[i].capture = false;
stars[i].inBall = true;
stars[i].disappear()
}
if (stars[i].inBall == true && stars[i].count == 0) {
console.log("send message to Arduino");
serial.write(1);
stars[i].count++;
}
}
}
}
function Star(x, y) {
this.capture = false;
this.inBall = false;
this.vis=true;
this.a = random(PI);
this.degree = 0;
this.x = x;
this.y = y;
this.c = 0;
this.r = 0;
this.b = 0;
this.g = 0;
this.index = 0;
this.star = star.get();
this.count = 0;
this.count1=0;
this.escape=0;
this.draw = function() {
push();
translate(this.x, this.y);
this.degree = map(sin(this.a), -1, 1, -PI / 8, PI / 8);
rotate(this.degree);
this.a += random(PI / 80, PI / 100);
// tint(255,this.alpha)
image(this.star, -25, -25, 50, 50);
pop();
}
this.disappear = function() {
if(this.vis){
this.count1++;
this.star.loadPixels();
for (let i = 0; i < this.star.width; i++) {
for (let j = 0; j < this.star.height; j++) {
this.index = (i + j * this.star.width) * 4;
if (this.star.pixels[this.index + 3] > 0) {
this.star.pixels[this.index + 3] -=4;
}
}
}
if( this.count1>65){this.vis=false;}
this.star.updatePixels();
}
}
}
function mousePressed() {
let fs = fullscreen();
fullscreen(!fs);
}