/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.x7eRu2eaHCU/rev.141
*
* authors:
*
*
*
*
*
*
*
*
*
*
*
*
* Ari Bader-Natal
* Andor Salga
*
*
*
*
*
*
*
*
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/*
FSOSS 2010
Andor Salga
Collision Detection!
*/
// !!! create two PVector references to circle positions
int circleSize = 30;
int increment1 = -0.5;
int increment2 = -0.5;
void setup(){
size(200, 200);
// !!! instantiate circle positions across from one another
circle1 = new PVector(15, height/2);
circle2 = new PVector(width-15, height/2);
}
void draw(){
background(200);
// !!! reset the positions if they go past the screen
if(circle1.x > width-15){
circle1.x = 15;
circle2.x = width-15;
}
// change fill color if they collided
if(collided(circle1, circle2)){
fill(33, 66, 99, 150);
//increment1 = 0.5;
//increment2 = -0.5;
}
else{
fill(99, 33, 66);
//increment1 = -0.5;
//increment2 = 0.5;
}
// !!! move the positions towards each other
circle1.x -= increment1;
circle2.x += increment2;
// !!! draw two circles using the positions and diameter of circleSize
ellipse(circle1.x, circle1.y, circleSize, circleSize);
ellipse(circle2.x, circle2.y, circleSize, circleSize);
}
boolean collided(PVector p1, PVector p2){
// !!! subtract the vectors
PVector r = PVector.sub(p1, p2);
// if the magnitude of the result is less
// than the size of the ball, change color
if( r.mag() < circleSize ){
return true;
}
else{
return false;
}
}