Here is a sketch i am working on. Im wondering if someone could give me a clue as to how to get ball thats caught to act more naturally. I guess it would need to be dragged a bit or maybe bounce off the edge of the other ball to prevent dragging.
EDIT: Im going to try to add in these functions using the circle collision with swapping velocities example.
Quote:Ball ball1;
Ballcaught ballcaught1;
void setup() {
size(1000,700);
smooth();
ball1 = new Ball(150);
ballcaught1 = new Ballcaught(25);
}
void draw() {
background(50);
ball1.move();
ballcaught1.moveBallcaught();
if (ballcaught1.hitEdge(ball1)) {
ballcaught1.turnBack();
}
ball1.display();
ballcaught1.displayBallcaught();
}
class Ball {
float r;
float x,y;
float xspeed,yspeed;
color c = color(200,200);
Ball(float tempR) {
r = tempR;
x = random(width);
y = random(height);
xspeed = random(-3,5);
yspeed = random(-3,5);
}
void move() {
x += xspeed;
y += yspeed;
if (x > width || x < 0) {
xspeed *= -1;
}
if (y > height || y < 0) {
yspeed *= -1;
}
}
void highlight() {
c = color(200,0,100,99);
}
void display() {
stroke(0);
strokeWeight(15);
fill(c);
ellipse(x,y,r*2,r*2);
c = color(0,150,0);
}
}
class Ballcaught {
float r;
float x,y;
float xspeed,yspeed;
color c = color(200,200);
Ballcaught(float tempR) {
r = tempR;
x = ball1.x;
y = ball1.y;
xspeed = random(-3,4);
yspeed = random(-3,4);
}
void moveBallcaught() {
x += xspeed;
y += yspeed;
}
boolean hitEdge(Ball b ) {
float distance = dist(x,y,b.x,b.y);
if (distance > b.r) {
return true;
}
else {
return false;
}
}
void turnBack() {
x = ball1.x;
y = ball1.y;
xspeed *= -1;
yspeed *= -1;
}
void displayBallcaught() {
stroke(0);
strokeWeight(7);
fill(c);
ellipse(x,y,r*2,r*2);
c = color(170,200,0);
}
}