How to program cursor/mousepress to click to remove objects on screen?
in
Programming Questions
•
2 years ago
Okay so first off...I am horrible at programming. Just getting that out now. This is my first semester programming anything...ever. Okay so I have this sketch consisting of 10 bubbles/balls bouncing off each other and off the screen. I wanted to know how I would code it so that when I click on one any the bubbles with my cursor, they just disappear, like i popped them. sounds simple but I have no idea where to start. Here is my code:
PImage img;
ArrayList Bubbles;
void setup()
{
//Create the size of the sketch
size(600,400);
smooth();
Bubbles = new ArrayList();
for(int i=0; i <10; i++)
{
Bubbles.add(new Bubble());
}
// Load in image file
img = loadImage("gradient_blue.png");
}
void draw()
{
//Set the background
background(255);
// Display the image
image(img, 0, 0);
//Check for collisions
for(int i=0; i < Bubbles.size(); i++) {
Bubble BubbleA = (Bubble) Bubbles.get(i);
for(int j=0; j < Bubbles.size(); j++) {
Bubble BubbleB = (Bubble) Bubbles.get(j);
if(!BubbleA.equals(BubbleB) && BubbleA.pos.dist(BubbleB.pos) < BubbleA.rad + BubbleB.rad){
bounce(BubbleA,BubbleB);
}
}
}
for (int i =0; i<Bubbles.size(); i++)
{
Bubble theBubble = (Bubble) Bubbles.get (i);
theBubble.update();
theBubble.display();
}
}
Next Tab: Bounce
void bounce(Bubble BubbleA, Bubble BubbleB) {
// first, if the Bubbles are overlapping,
// move them away from each other until they're not.
PVector ab = new PVector();
ab.set(BubbleA.pos);
ab.sub(BubbleB.pos);
ab.normalize();
while(BubbleA.pos.dist(BubbleB.pos) < BubbleA.rad + BubbleB.rad) {
BubbleA.pos.add(ab);
}
//This section taken from cadjunkie's tutorial
// based on formulas found here:
PVector impact = new PVector();
PVector impulse = new PVector();
float impactSpeed;
impact.set(BubbleA.pos);
impact.sub(BubbleB.pos);
impulse.set(ab);
impactSpeed = impulse.dot(impact);
impulse.mult(sqrt(impactSpeed));
BubbleA.Spd.add(impulse);
BubbleB.Spd.sub(impulse);
}
Last tab: Bubble
class Bubble
{
PVector pos = new PVector();
PVector Spd = new PVector();
float rad = 30;
Bubble() {
pos.x = random(0,width);
pos.y = random(0,height);
Spd.x = random(0,5);
Spd.y = random(0,5);
}
void update()
{
//Ellipse hits walls
if ( pos.y + rad > height ) Spd.y = abs(Spd.y) * -1;
if ( pos.y - rad < 0 ) Spd.y = abs(Spd.y);
if ( pos.x + rad > width ) Spd.x = abs(Spd.x) * -1;
if ( pos.x - rad < 0 ) Spd.x = abs(Spd.x);
//Moving Ellipse
pos.add(Spd);
}
void display(){
//Create ellipse color
fill(22,205,240,60);
//Create ellipse stroke color
stroke(255);
//Create ellipse stroke weight (thickness)
strokeWeight(1);
//Create ellipse
ellipseMode(RADIUS);
ellipse(pos.x,pos.y,rad,rad);
}
}
1