/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://friendscentral.sketchpad.cc/sp/pad/view/ro.9s-xPPzhhEH/rev.111
*
* authors:
* Colin Angevine
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
Projectile x;
void setup() {
size(500, 500);
x = new Projectile();
}
void draw() {
background(255);
x.update();
PVector gravity = new PVector(0, 0.5 * x.mass);
x.applyForce(gravity);
x.display();
}
void mouseClicked() {
x.launch();
}
class Projectile {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Projectile() {
mass = random(1, 4);
location = new PVector(50, 50);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
void launch() {
location = new PVector(0, 400);
velocity = new PVector(random(7, 10), random(-7, -15));
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
PVector calculateFriction() {
float c = 0.01;
PVector friction = velocity.get();
friction.mult(-1);
friction.normalize();
friction.mult(c);
return friction;
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
if (location.x < 0) {
location.x = 0;
velocity.x = velocity.x * -.9;
PVector friction = calculateFriction();
applyForce(friction);
}
if (location.x > width) {
location.x = width;
velocity.x = velocity.x * -.9;
PVector friction = calculateFriction();
applyForce(friction);
}
if (location.y > height) {
location.y = height;
velocity.y = velocity.y * -.9;
PVector friction = calculateFriction();
applyForce(friction);
}
}
void display() {
stroke(0);
fill(175);
rect(location.x, location.y, mass * 15, mass * 15);
}
}