xxxxxxxxxx
335
let video;
let poseNet;
let poses = [];
let isPlaying = false;
let keyNum = 0;
let tpos,cpos;
let smoothf = .04;
const squareSize = 160;
let smooth_dist = 1;
let prev_pos;
let osc, envelope;
let triggered = false; // threshold bool
let acc_smooth = .1;
let acc_theshold = 40;
// for debugging
let last_trigger_str = "move";
let enable_tracking = true;
let ballpos;
let ballvel;
let speed = 1;
let me_img, enemy_img;
function setup() {
createCanvas(640,480);
video = createCapture(VIDEO);
video.size(320,240);
if(enable_tracking){
// Create a new poseNet method with a single detection
poseNet = ml5.poseNet(video, 'single', modelReady);
// This sets up an event that fills the global variable "poses"
// with an array every time new poses are detected
poseNet.on('pose', function(results) {
poses = results;
});
}
// Hide the video element, and just show the canvas
video.hide();
tpos = createVector(0,0);
cpos = tpos;
prev_pos = cpos;
textAlign(CENTER);
textStyle(BOLD);
setupSound();
smooth_acc = createVector(0,0);
noStroke();
noSmooth();
ballvel = createVector(speed,speed);
ballpos = createVector(111,111);
me_img = loadImage("imgs/168.png");
enemy_img = loadImage("imgs/161.png");
tpos = createVector(0,0);
}
function setupSound(){
osc = new p5.SinOsc();
// Instantiate the envelope
envelope = new p5.Env();
// set attackTime, decayTime, sustainRatio, releaseTime
envelope.setADSR(0.1, 0.8, 0.2, 0.8);
// set attackLevel, releaseLevel
envelope.setRange(1, 0);
}
let smooth_acc;
function modelReady() {
// select('#status').html('Model Loaded');
osc.start();
playNote(180);
}
function draw() {
let diff_v = createVector(0,0); //cpos.sub(prev_pos);
diff_v.x = cpos.x - tpos.x;
diff_v.y = cpos.y - tpos.y;
// Flip the video from left to right, mirror the video
translate(width, 0)
scale(-2, 2);
//background(44);
image(video, 0, 0 );
updateBall();
collisionCalc();
let tdisti = dist(cpos.x,cpos.y,tpos.x,tpos.y)*.4+1.;
smooth_dist = lerp(smooth_dist,tdisti,smoothf);
//fill(244);
cpos.x = lerp(cpos.x,tpos.x,smoothf);
cpos.y = lerp(cpos.y,tpos.y,smoothf);
// ellipse( cpos.x, cpos.y, health*.6,health*.6);
image( me_img,
(cpos.x-health*.3) ,
(cpos.y-health*.3),
health*.6,health*.6);
// We can call both functions to draw all keypoints and the skeletons
drawKeypoints();
//drawSkeleton();
}
function updateBall(){
if(ballpos.x<0 || ballpos.x>video.width){
ballvel.x *=-1;
//playNote(322);
speed += .004;
}
if(ballpos.y<0 || ballpos.y>video.height){
ballvel.y *=-1;
// playNote(362);
speed += .004;
}
ballpos.x += ballvel.x*speed;
ballpos.y += ballvel.y*speed;
ballvel.x += (noise(ballpos.x*.01,ballpos.y*.01,millis()*.82)-.5)*.1;
ballvel.y += (noise(ballpos.x*.03,ballpos.y*.02,millis()*.81)-.5)*.1;
//fill(255,11,88);
// ellipse( ballpos.x, ballpos.y,55,55);
image(enemy_img,ballpos.x-22, ballpos.y-22,44,44);
health +=.1;
if(health<1){
exit;
}
if(health>100){
health = 100;
}
}
let health = 100;
function collisionCalc(){
let cdist = dist (ballpos.x, ballpos.y,cpos.x,cpos.y);
if(cdist<health/2){
playNote(82);
//console.log("cpollide");
health -=1;
}
}
// A function to draw ellipses over the detected keypoints
function drawKeypoints() {
// Loop through all the poses detected
//for (let i = 0; i < poses.length; i++) {
// For each pose detected, loop through all the keypoints
if(poses[0]==null){return;}
let pose = poses[0].pose;
let conf = .3;
if(pose.keypoints[0].score > conf){
let npx = pose.keypoints[0].position.x;
let npy = pose.keypoints[0].position.y;
// ellipse( npx, npy,3,3);
tpos.x = npx;
tpos.y = npy;
return;
}
if(pose.keypoints[1].score > conf){
let n2px = pose.keypoints[1].position.x;
let n2py = pose.keypoints[1].position.y;
//ellipse( n2px, n2py,3,3);
tpos.x = n2px;
tpos.y = n2py;
return;
}
if(pose.keypoints[2].score > conf){
let n3px = pose.keypoints[2].position.x;
let n3py = pose.keypoints[2].position.y;
// ellipse( n3px, n3py,3,3);
tpos.x = n3px;
tpos.y = n3py;
return;
}
if(pose.keypoints[3].score > conf){
let n4px = pose.keypoints[3].position.x;
let n4py = pose.keypoints[3].position.y;
ellipse( n4px, n4py,3,3);
tpos.x = n4px ;
tpos.y = n4py ;
return;
}
if(pose.keypoints[4].score > conf){
let n5px = pose.keypoints[4].position.x;
let n5py = pose.keypoints[4].position.y;
ellipse( n5px, n5py,3,3);
tpos.x = n5px ;
tpos.y = n5py ;
return;
}
/*
for (let j = 0; j < pose.keypoints.length; j++) {
// A keypoint is an object describing a body part (like rightArm or leftShoulder)
let keypoint = pose.keypoints[j];
/*
// Only draw an ellipse is the pose probability is bigger than 0.2
if (keypoint.score > 0.2) {
fill(0, 0, 255);
noStroke();
//ellipse(keypoint.position.x, keypoint.position.y, 2, 2);
}
// keypoint[0] is the nose point
// Only check the first pose
if ( j === 0 ) {
//tpos = createVector(keypoint.position.x, keypoint.position.y);
}
}
*/
//}
}
function playNote(_f){
osc.freq(_f);
envelope.play(osc, 0, 0.1);
}