This is my code:
Circle[] circles = {
new Circle(40,color(0),color(255,0,0),0,2,0,0.5,new PVector(250,300),new PVector(0,0)),
new Circle(200,color(0),color(255,0,0),0,10,0,0.5,new PVector(500,300),new PVector(0,0)),
new Circle(20,color(0),color(255,0,0),0,0.1,0,0.5,new PVector(750,300),new PVector(0,0)),
};
PVector wind = (PVector.random2D()).div(20);
PVector gravity = new PVector(0,0.2);
void setup(){
size(1000,600);
}
void draw(){
background(0);
circles[0].applyForce(wind);
circles[0].applyForce(gravity);
circles[0].update();
circles[1].applyForce(wind);
circles[1].applyForce(gravity);
circles[1].update();
circles[2].applyForce(wind);
circles[2].applyForce(gravity);
circles[2].update();
}
class Circle{
float radius;
color colorStroke;
color colorFill;
float thickness;
float mass;
float friction;
float restitution;
PVector position,velocity;
Circle(float r,color cs,color cf,float t,float m,float f,float re,PVector p,PVector v){
radius = r;
colorStroke = cs;
colorFill = cf;
thickness = t;
mass = m;
friction = f;
restitution = re;
position = p;
velocity = v;
}
void update(){
if(thickness == 0){
noStroke();
}else{
strokeWeight(thickness);
}
stroke(colorStroke);
fill(colorFill);
ellipse(position.x,position.y,radius,radius);
position.add(velocity);
if(position.y > (height - (radius / 2)) && velocity.y > 0){
velocity.y = velocity.y * -restitution;
position.y = height - (radius / 2);
}
if(position.y < (radius / 2) && velocity.y < 0){
velocity.y = velocity.y * -restitution;
position.y = radius / 2;
}
if(position.x > (width - (radius / 2)) && velocity.x > 0){
velocity.x = velocity.x * -restitution;
position.x = width - (radius / 2);
}
if(position.x < (radius / 2) && velocity.x < 0){
velocity.x = velocity.x * -restitution;
position.x = radius / 2;
}
}
void applyForce(PVector force){
velocity.x = velocity.x + (force.x / mass);
velocity.y = velocity.y + (force.y);
}
}
The edge bouncing is finished, but I want the balls to bounce off each other. So how do I make them detect collision and bounce?