Trying to make my game where all the balls go into the bottom right "basket then their x,y cord is reset 0. Or maybe the loop restarts, I’m really not sure how to approach it. Also, my collision is based on the distance of the ball from my “character” or mouseX and fixed Y cord. I can’t figure out collision using PVectors. Here are my main game loop and my Ball class, I also have classes for the world and my character if needed.
Ball b1;
Bill c;
World w;
Ball[] balls = new Ball[5];
float score =0;
void setup() {
size(640,480);
w = new World();
c = new Bill();
for(int i = 0; i < 5; i++) {
balls[i] = new Ball();
}
}
void draw() {
w.sky();
w.ground();
c.display();
for(int i = 0; i < 5; i++) {
if (score < 10){
balls[i].move();
balls[i].check();
balls[i].display();
} else { i = i - 1;}
}
c.cube();
w.basket();
}
class Ball {
PVector loc, vel;
float rad = 20;
float billX = mouseX + 60;
float rectheight = 100;
float rectwidth = 30;
float billY = 398;
float test;
float score = 0;
Ball() {
loc = new PVector(random(20,50),random(20,30));
vel = new PVector(2,random(1,3));
}
void display() {
stroke(255,0,0);
fill(255,0,0);
ellipse(loc.x,loc.y,rad,rad);
}
void move() {
loc.add(vel);
}
void check(){
test = dist(loc.x + vel.x,loc.y +vel.y ,mouseX,400);
if (loc.x < 0 + 10 || loc.x > width - rad) {
vel.x *= -1;
}
if(loc.y < 0 + rad|| loc.y > height - 35) {
vel.y *= -1;
}
if(test < 10 ) {
//vel.x = vel.x * -1;
vel.y = vel.y * -0.95;
}
if (loc.x > 540 && loc.y > 380) {
loc.x = 0;
loc.y = 0;
vel.x =2;
vel.y =4;
score +=6;
}
}
}