xxxxxxxxxx
90
let velocity;
let gravity;
let position;
let acceleration;
let wind;
let drag = 0.99;
let mass = 50;
let ground;
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);
fill("black")
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;
print(position.y);
ground = true;
}
else{
ground = false;
}
}
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 (keyCode==LEFT_ARROW){
wind.x=-1;
}
if (keyCode==RIGHT_ARROW){
wind.x=1;
}
if (key==' '){
mass=random(15,80);
position.y=-mass;
velocity.mult(0);
position.x=width/2
}
if (key == "s") {
// Calls a function to set up the serial connection
setUpSerial();
}
}
function readSerial(data) {
// Check if data received is not null
if (data != null) {
wind.x= map(int(data), 0,1023,-1, 1)
let sendToArduino;
if (ground) {
sendToArduino = "1"+"\n";
}
else{
sendToArduino = "0"+"\n";
}
print(sendToArduino)
writeSerial(sendToArduino);
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
}
}