Hi all. I’ve made the basic bouncing ball, but I’m trying to make it so that it maintains the velocity of the cursor. is there something I did wrong?
PVector location;
PVector velocity;
PVector gravity;
int sz = 100;
color bgcolor;
void setup() {
size (900, 700);
location = new PVector(100, 100);
velocity = new PVector(1.5, 2.1);
gravity = new PVector(0, 0.2);
}
void draw() {
background (#151515);
//so i can see what i'm doing
text("x: "+mouseX+" y: "+mouseY, 10, 15);
//add velocity to location.
location.add(velocity);
//add gravity to velocity
velocity.add(gravity);
if (dist(location.x, location.y, mouseX, mouseY) < sz / 2) {
cursor(HAND);
if (mousePressed) {
location.x = mouseX;
location.y = mouseY;
strokeWeight(5);
} else {
strokeWeight(2);
}
stroke(255);
} else {
cursor(ARROW);
noStroke();
}
//bounce off edges
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if (location.y > height) {
//reduce the velocity when it hits the bottom of the window
velocity.y = velocity.y * -0.95;
location.y = height;
}
//the ball
stroke(255);
strokeWeight(2);
fill(127);
ellipse(location.x, location.y, 75, 75);
}