xxxxxxxxxx
105
//Luca Turretti 15/02/2019
let x;
let y;
let z;
let q;
let xspeed;
let yspeed;
let zspeed;
let qspeed;
let dvd;
let offset;
let tolerance;
let rate;
let time_drop_sound;
function preload() {
dvd = loadImage("320px-DVD_video_logo.png");
soundFormats('mp3', 'ogg');
mySound = loadSound('thanos.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight);
rate=30;
time_drop_sound=2.7;
frameRate(rate)
xspeed = 3;
yspeed = 3;
zspeed = xspeed
qspeed = yspeed;
offset = (sqrt(xspeed*xspeed+yspeed*yspeed))*rate*time_drop_sound;//this offset determines ho much distant the predictor
//must be to be timed to the sound ,if "time_drop_sound" i set to 0
//the sound will reproduce as soon as the logo bounces, else you can
//set this variable with the exact second of the drop of the song,
//in my case the drop is at at the 2.7 second from the start
tolerance =2;//the tolerance defines how much close to the angle you'll need to bounce to trigger
x = random(0,width-dvd.width/2-offset) ;
y = random(0,height-dvd.height/2-offset);
z=x+offset;
q=y+offset;
}
function draw() {
background(220);
move();
image(dvd, x, y, dvd.width / 2, dvd.height / 2);
movea ();
//if you put a copy of the image with z and q insted of x and y you'll see the predictor of the hit
checkthanos()
bounce ();
}
function move() {
x += xspeed;
y += yspeed;
}
function movea (){
z+= zspeed;
q+= qspeed;}
function bounce (){
if (x >= width - dvd.width / 2 || x <= 0) {
xspeed *= -1;
}
if (y >= height - dvd.height / 2 || y<= 0) {
yspeed *= -1;
}
if (z >= width - dvd.width / 2 || z <= 0) {
zspeed *= -1;
}
if (q >= height - dvd.height / 2 || q <= 0) {
qspeed *= -1;
}
}
function checkthanos(){
//top left
if(z<=tolerance && q<=tolerance)
thanos();
//top right
if(z>=((width - dvd.width / 2)-tolerance) && q<=tolerance )
thanos();
//bottom left
if(z<=tolerance && q>=((height - dvd.height / 2)-tolerance))
thanos();
//bottom right
if(z>=((width - dvd.width / 2)-tolerance) && q>=((height - dvd.height / 2)-tolerance))
thanos()
}
function thanos(){
if(!mySound.isPlaying())
mySound.play();
}