Hello everyone!
I’m trying to make it so the “fireflies” in my code, while in the cup, move slower.
So far, the fireflies bounce off the walls, and once the player presses ‘g’, a cup is grabbed, and if the cup makes contact with a firefly, the firefly goes in the cup and bounces around in there, until released when the player pressed ‘p’.
However, while the fireflies are in the cup, they bounce around really fast. I’ve been trying to make it so they slow down (or even go the speed they are while outside the cup) but I can’t seem to do it.
I’ve tried to slow them by lowering the speed they move while in the cup, however, this either just doesn’t work, or only slows them after ‘p’ is pressed. I listed below some code I’ve tried.
I appreciate all the help I could get! Thank you in advance!
ArrayList <Fireflies> firefly;
float fireflies = 15; //num of fireflies
float cx = 200; //sitting cup position x
float cy = 200; //sitting cup position y
float cupW = 100; //cup width
float cupH = 135; //cup height
boolean inCup = false;
void setup() {
size(600, 600);
background(0);
firefly = new ArrayList <Fireflies>();
for (int f = 0; f < fireflies; f++) {
firefly.add(new Fireflies());
}
}
void draw() {
background(0);
//fireflies
for (int f = 0; f < fireflies; f++) {
firefly.get(f).display();
}
}
class Fireflies {
float size = random(1, 20);
float xpos = random(0, width);
float ypos = random(0, height);
float xspeed = 1;
float yspeed = 1;
float xdir = random(-1, 1);
float ydir = random(-1, 1);
Fireflies() {
}
void update() {
xpos = xpos + (xspeed * xdir);
ypos = ypos + (yspeed * ydir);
}
void checkInCup() {
if (inCup == true) { //changes the position of the fireflies to inside the cup
xpos = random(cx, cx + cupW) + (xspeed * xdir);
ypos = random(cy, cy + cupH) + (yspeed * ydir);
//I tried to slow them down by incrementing the speed in small amounts- this didn't stop them
//while inside the jar
//xpos = random(cx, cx + cupW) + (xspeed/16* xdir/16);
//ypos = random(cy, cy + cupH) + (yspeed/16 * ydir/16);
}
}
void checkEdges() {
if (inCup == false) {//if not in cup, bounces off sides of wall
if (xpos > width-size || xpos < size) {
xdir *= -1 ;
}
if (ypos > height-size || ypos < size) {
ydir *= -1;
} else if (inCup == true) { //if in cup, bounces off sides of cup
if (xpos > cx-size || xpos < cx) {
xdir *= -1;
//I also tried to slow them down here by making xdir a smaller number
//xdir *= -.001
//this also didn't slow them down in the cup
}
if (ypos > cy-size || ypos < cy) {
ydir *= -1;
}
}
}
}
void display() {
update();
checkEdges();
checkInCup();
//draw fireflies
for (int f = 0; f < 1; f++) {
fill(100, 100, 0);
circle(xpos, ypos, size);
}
//grab cup
if (key == 'g') {
cx = mouseX;
cy = mouseY;
if (xpos > cx && ypos > cy) {
inCup = true;
//if the x&y pos. of the fireflies collide w/ the cup's x&y,
//the fireflies go in the cup
//i know this collision detection is... bad but it's not my focus right now :/
}
}
//put cup down
if (key == 'p') {
cx = 200;
cy = 200;
inCup = false;
}
//cup
fill(0, 230, 255, 50);
rect(cx, cy, cupW, cupH);
}
}