xxxxxxxxxx
75
class blob
{
constructor(x, y)
{
this.pos = new p5.Vector(x, y);
this.vel = new p5.Vector(null);
this.r = 10;
this.friction = 0;
}
draw () {
this.update();
square(this.pos.x, this.pos.y, this.r * 2);
if (mouseIsPressed) {
line(this.pos.x, this.pos.y, mouseX, mouseY);
}
}
update() {
this.pos.add(this.vel);
this.vel.y += 0.1;
if (this.vel.x > 0)
{
this.vel.x -= this.friction;
} else if (this.vel.x < 0)
{
this.vel.x += this.friction;
}
if (this.vel.y > 0)
{
this.vel.y -= this.friction;
} else if (this.vel.y < 0)
{
this.vel.y += this.friction;
}
if ((this.pos.x < 0 && this.vel.x < 0) || (this.pos.x > width && this.vel.x > 0))
{
this.vel.x = -this.vel.x;
}
if ((this.pos.y < 0 && this.vel.y < 0) || (this.pos.y > height && this.vel.y > 0))
{
this.vel.y = -this.vel.y;
}
}
mouseReleased()
{
this.vel.setHeading(atan2((mouseY - this.pos.x) - height / 2, (mouseX - this.pos.y) - width / 2));
this.vel.setMag(20);
}
}
var bob;
function setup() {
createCanvas(windowWidth, windowHeight);
bob = new blob(width / 2, height / 2);
}
function draw() {
background(220);
bob.draw();
}
function mouseReleased() {
bob.mouseReleased();
}