xxxxxxxxxx
184
///Player 1 use ARROW KEYS
///Player 2 use WASD KEYS
///Push the opposing player out of the canvas
let bob;
let anchor;
let spring;
let gravity;
function setup() {
createCanvas(700, 600);
bob = new Particle(400, 190);
anchor = new Anchor(200, 210);
spring = new Spring(0.01, 200, bob, anchor);
gravity = createVector(0, 0.1);
textAlign(CENTER, CENTER);
}
function draw() {
background(5, 195, 221);
spring.show();
spring.update();
bob.show();
bob.control();
bob.update();
anchor.show();
anchor.control();
anchor.update();
}
class Particle {
constructor(x, y) {
this.acceleration = createVector(0, 0);
this.velocity = createVector(0, 0);
this.position = createVector(x, y);
this.mass = 4;
}
applyForce(force) {
let f = force.copy();
f.div(this.mass);
this.acceleration.add(f);
}
update() {
this.velocity.mult(0.99);
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
show() {
let rad1 = 30;
noStroke();
fill(255);
ellipse(this.position.x, this.position.y, rad1);
fill(0);
textSize(16);
text('1',this.position.x, this.position.y);
if (this.position.x > width - rad1 || this.position.x < rad1) {
noLoop();
fill(0);
textSize(30);
text('Player 2 won', width/2, height/2);
}
if (this.position.y > height - rad1 || this.position.y < rad1) {
noLoop();
textSize(30);
text('Player 2 won', width/2, height/2);
}
}
control(){
if (keyIsDown(38)){
this.velocity.add(0.06, -0.06);
} else if (keyIsDown(40)){
this.velocity.add(-0.06, 0.06);
}
if (keyIsDown(37)){
this.velocity.add(-0.06);
} else if (keyIsDown(39)){
this.velocity.add(0.06);
}
}
}
class Anchor {
constructor(x, y) {
this.acceleration = createVector(0, 0);
this.velocity = createVector(0, 0);
this.position = createVector(x, y);
this.mass = 4; // Let's do something better here!
}
applyForce(force) {
let f = force.copy();
f.div(this.mass);
this.acceleration.add(f);
}
update() {
this.velocity.mult(0.99);
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
show() {
let rad2 = 30;
noStroke();
fill(255);
ellipse(this.position.x, this.position.y, rad2);
fill(0);
textSize(16);
text('2',this.position.x, this.position.y);
if (this.position.x > width - rad2 || this.position.x < rad2) {
noLoop();
fill(0);
textSize(30);
text('Player 1 won', width/2, height/2);
}
if (this.position.y > height - rad2 || this.position.y < rad2) {
noLoop();
fill(0);
textSize(30);
text('Player 1 won', width/2, height/2);
}
}
control(){
if (keyIsDown(87)){
this.velocity.add(0.06, -0.06);
} else if (keyIsDown(83)){
this.velocity.add(-0.06, 0.06);
}
if (keyIsDown(65)){
this.velocity.add(-0.06);
} else if (keyIsDown(68)){
this.velocity.add(0.06);
}
}
}
class Spring {
constructor(k, restLength, a, b) {
this.k = k;
this.restLength = 80;
this.a = a;
this.b = b;
}
update() {
let force = p5.Vector.sub(this.b.position, this.a.position);
let x = force.mag() - this.restLength;
force.normalize();
force.mult(this.k * x);
this.a.applyForce(force);
force.mult(-1);
this.b.applyForce(force);
}
show() {
strokeWeight(4);
stroke(255);
line(
this.a.position.x,
this.a.position.y,
this.b.position.x,
this.b.position.y
);
}
}