xxxxxxxxxx
83
let led;
let velocity;
let gravity;
let position;
let acceleration;
let wind;
let drag = 0.99;
let mass = 50;
function setup() {
createCanvas(640, 360);
noFill();
position = createVector(width/2, 0);
velocity = createVector(0,0);
acceleration = createVector(0,0);
gravity = createVector(0, 0.5*mass);
wind = createVector(0,0);
}
function draw() {
background(255);
applyForce(wind);
applyForce(gravity);
velocity.add(acceleration);
velocity.mult(drag);
position.add(velocity);
acceleration.mult(0);
ellipse(position.x,position.y,mass,mass);
if (position.y > height-mass/2) {
velocity.y *= -0.9; // A little dampening when hitting the bottom
position.y = height-mass/2;
if(serialActive){
writeSerial("bounced\n");
}
}
// Check for collisions with the left wall
if (position.x < mass / 2) {
velocity.x =0; // Reverse horizontal velocity (bounce)
position.x = mass / 2; // Correct position
}
// Check for collisions with the right wall
if (position.x > width - mass / 2) {
velocity.x =0; // Reverse horizontal velocity (bounce)
position.x = width - mass / 2; // Correct position
}
}
function applyForce(force){
// Newton's 2nd law: F = M * A
// or A = F / M
let f = p5.Vector.div(force, mass);
acceleration.add(f);
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
if (keyCode==DOWN_ARROW){
//mass=random(15,80);
position.y=-mass;
velocity.mult(0);
}
}
function readSerial(data){
if (data != null){
wind.x=int(data);
console.log("working");
}
}