class Ball {
PVector loc;
PVector vel;
PVector acc;
color c;
int radio;
float bounce = 0.8;
boolean colliding = false;
PFont f = createFont("Arial",10,true);
Ball(float setX, float setY, color setColor, int setRadio ){
loc = new PVector(setX,setY);
vel = new PVector(random(-2,2),random(-2,0));
acc = new PVector(1,2);
radio = setRadio;
c = setColor;
}
void applyForce(PVector force){
force.div(radio/2);
acc.add(force);
}
void go(){
update();
bordes();
render();
}
void update(){
vel.add(acc);
loc.add(vel);
acc.mult(0);
}
void bordes(){
if (loc.y + radio >= height){
loc.y = height - radio;
vel.y *= -bounce;
}
if (loc.y - radio <= 0) {
loc.y = radio;
vel.y *= -bounce;
}
if(loc.x + radio >= width){
loc.x = width - radio;
vel.x *= -bounce;
}
if (loc.x - radio <= 0){
loc.x = radio;
vel.x *= -bounce;
}
}
PVector calcGravForce(Ball b){
PVector dir = PVector.sub(loc,b.loc);
float d = dir.mag();
//d = constrain(d,5.0,25.0);
dir.normalize();
float force = (9.8 * radio * b.radio) / (d * d);
dir.mult(force);
return dir;
}
void collideEqualMass(Ball b) {
float d = PVector.dist(loc,b.loc);
float sumR = radio + b.radio;
// Are they colliding?
if (!colliding && d < sumR) {
// Yes, make new velocities!
colliding = true;
// Direction of one object to another
PVector n = PVector.sub(b.loc,loc);
n.normalize();
// Difference of velocities so that we think of one object as stationary
PVector u = PVector.sub(vel,b.vel);
// Separate out components -- one in direction of normal
PVector un = componentVector(u,n);
// Other component
u.sub(un);
// These are the new velocities plus the velocity of the object we consider as stastionary
vel = PVector.add(u,b.vel);
b.vel = PVector.add(un,b.vel);
} else if (d > sumR) {
colliding = false;
}
}
void render(){
fill(c);
noStroke();
ellipse(loc.x,loc.y,radio*2,radio*2);
if (showVectors) {
drawVector(vel,loc,10);
drawVector(G,loc,100);
}
if (showPosition) {
textFont(f,8);
fill(255,0,0,127);
text("("+ int(loc.x) + "," + int(loc.y) + ")",loc.x + radio, loc.y + radio);
}
}
}
void drawVector(PVector v, PVector loc, float scayl) {
pushMatrix();
float arrowsize = 4;
// Translate to location to render vector
translate(loc.x,loc.y);
stroke(0);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0,0,len,0);
line(len,0,len-arrowsize,+arrowsize/2);
line(len,0,len-arrowsize,-arrowsize/2);
popMatrix();
}
PVector componentVector (PVector vector, PVector directionVector) {
//--! ARGUMENTS: vector, directionVector (2D vectors)
//--! RETURNS: the component vector of vector in the direction directionVector
//-- normalize directionVector
directionVector.normalize();
directionVector.mult(vector.dot(directionVector));
return directionVector;
}
//import lll.Loc.*;
//import lll.wrj4P5.*;
//Wrj4P5 wii;
Ball balls[] = new Ball[1];
boolean showVectors = true;
boolean showPosition = false;
PVector G;
void setup(){
size(400, 400,P3D);
//smooth();
//wii=new Wrj4P5(this);
//wii.connect();
balls[0] = new Ball(random(0,190), random(0,190),color(random(0,255),random(0,255),random(0,255)),int(random(10,30)));
}
void draw(){
background(255);
for(int i = 0; i < balls.length ; i++){
for(int a = 0; a < balls.length; a++){
if(a != i){
//PVector f = balls[i].calcGravForce(balls[a]); // Use the function we wrote above
//balls[i].applyForce(f);
balls[i].collideEqualMass(balls[a]);
}
}
//G = new PVector(0,9.8);
G = new PVector(mouseX - width/2,mouseY - height/2);
//G = new PVector(wii.rimokon.sensed.x,wii.rimokon.sensed.y);
G.normalize();
balls[i].applyForce(G);
balls[i].go();
}
}
void mousePressed(){
Ball b = new Ball(mouseX,mouseY,color(random(0,255),random(0,255),random(0,255)),int(random(10,30)) );
balls = (Ball[]) append(balls,b);
}
void keyPressed() {
showVectors = !showVectors;
}
Ballons Colision & Gravity affected
- Mouse position: tels where is the floor.
- Mouse click: make new balls
- Spacebar: hide/show vector forces arrows