Floor / wall bounce with mouseclicked objects
in
Programming Questions
•
1 month ago
Creating a simple interactive program for a class. I'm trying to figure out how to make the circles when created with the mouseClicked bounce off the walls and floor. I've gotten the circles to shoot out but I need help with creating the borders.
Here is my code thus far:
Here is my code thus far:
- //Circle creation clicker Array//
//allow the user to place a circle of radius R at a location on the canvas
//with a mouse click.
float R;
ArrayList<Circle> circles;
//int maxNumCircles;
//int circleCounter;
void setup(){
size (500,500);
//maxNumCircles = 10;
R = 15;
// circleCounter = 0;
circles = new ArrayList<Circle>();
}
void draw() {
background(255);
fill(random(255),random(255),random(255));
noStroke();
for(int i=0; i<circles.size(); i++) {
circles.get(i).render();
circles.get(i).move();
}
}
void mouseClicked() {
circles.add(new Circle(mouseX, mouseY,R));
println("Number of circles; " + circles.size());
}
//define the circle class
class Circle {
//class data variables
float centerX, centerY;
float vx, vy, ax, ay;
float radius;
//constructor - builds the object when it is declared.
Circle(float x, float y, float r) {
centerX = x;
centerY = y;
vx = 0;
vy = -2;
ax = 0;
ay = 0.3;
radius = r;
}
//render the circle object
void render() {
noStroke();
for(int i=(int)radius; i>=0; i--){
fill(0,255,0);
ellipse(centerX, centerY, 2*i, 2*i);
fill(255,255,255,25);
ellipse(centerX,centerY,2*radius,2*radius);
}
}
// update position according to Newtonian motion
void move() {
centerX += vx; //equivalent to x = x+vx
centerY += vy;
vx += ax;
vy += ay;
}
}
1