We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to get an array of rising bubbles to emit from two bouncing moving orb objects. In the past I was able to get it to work, but it only worked for one of the orbs no matter what I tried.
I have simplified the code so that bubbles emits at mouseX, mouseY coordinates. And I have reversed mouse to show a second bubble does actually emerge at the skewed coordinates.
Any idea where I'm going wrong here. I'd appreciate the tips. Been trying this all day. Many thanks.
int numberOfOrbs = 2;
Orb_Class[] many_Orbs = new Orb_Class[numberOfOrbs];
int numberOfBubbles = 30;
Bubble_Class[] many_Bubbles = new Bubble_Class[numberOfBubbles];
int timer;
void setup()
{
size(600, 900);
noStroke();
smooth();
for (int i=0; i<many_Bubbles.length; i++)
{
many_Bubbles[i] = new Bubble_Class(random(width), height, 10, 1);
}
for (int i=0; i<many_Orbs.length; i++)
{
many_Orbs[i] = new Orb_Class(64, random(4, 6), random(4, 6));
}
}
void draw()
{
background( 255 ); // erase the window to black
stroke( 255 ); // draw using a white stroke
if (millis() - timer >= 500)
{
for (int i=0; i<many_Bubbles.length; i++)
{
many_Bubbles[0] = new Bubble_Class(mouseX, mouseY, 10, 1);
many_Bubbles[1] = new Bubble_Class(mouseY, mouseX, 10, 1);
}
timer = millis();
}
for (int j=0; j<many_Orbs.length; j++)
{
many_Orbs[j].display();
many_Orbs[j].move(j);
}
for (int i=0; i<many_Bubbles.length; i++)
{
many_Bubbles[i].ascend();
many_Bubbles[i].display();
}
}
class Orb_Class
{
float radius; // radius
float x;
float y = random(300, 600); // location
float xspeed, yspeed; // speed
float xtime;
float ytime;
// Constructor
Orb_Class(float tempR, float tempXt, float tempYt)
{
radius = tempR;
xspeed = tempXt;
yspeed = tempYt;
}
void move(int arrayIndex) // Unused for the moment
{
//xspeed = incomingKickOSC;
x += xspeed; // Increment x
y += yspeed; // Increment y
// Check horizontal edges
if (x > width || x < 0) {
//popsound[arrayIndex].rewind();
//popsound[arrayIndex].play();
xspeed *= -1;
}
//Check vertical edges
if (y > height || y < 0) {
//popsound[arrayIndex].rewind();
//popsound[arrayIndex].play();
yspeed *= -1;
}
}
void display()
{
fill(255, 0, 0);
noStroke();
ellipse(x, y, radius, radius);
}
}
class Bubble_Class
{
float diameter;
float riseSpeed;
boolean popped = false;
float x, y;
Bubble_Class(float tempX, float tempY, float tempD, float tempRs) {
x = tempX;
y = tempY;
diameter = tempD;
riseSpeed = tempRs;
}
void ascend() {
y = y - riseSpeed;
x = x + random(-2, 2);
}
void display() {
if (!popped) {
stroke(0);
fill(127, 100);
ellipse(x, y, diameter, diameter);
}
}
}
I have also tried to move code within the Orb_Class and failed. I think I need to be doing something like this but my efforts are failing:
if (millis() - timer >= 500)
{
for (int i=0; i<many_Bubbles.length; i++)
{
for (int j=0; j<many_Orbs.length; j++)
{
many_Bubbles[0] = new Bubble_Class(somehow_Access_Array_For_Orb_PosX[j], somehow_Access_Array_For_Orb_PosY[j], 10, 1);
}
}
timer = millis();
}
Answers
i think you could
many_Bubbles[0] = new Bubble_Class(mouseX, mouseY, 10, 1); many_Bubbles[1] = new Bubble_Class(mouseY, mouseX, 10, 1);
into the class and use x,y instead of mouseX,mouseY
Thanks for tip, Chrisir. I had done that already and same result. Only one ball will emit bubbles.
you start the for loop with 0
instead try to monitor how many you put in the array and then say
for ( int i=numBubbles; i<numBubbles+ 20
numBubbles+= 20 ;
make the array much bigger
an arrayList instead of array would be easier - it works with .size() and .add(new Bubble....
best when the bubles leave the screen to remove() them from arrayList
I just started learning processing as well. I think you should initialize all the variables for best practice. Also,
It's inside a for loop, but the index doesn't change. You're just re-writing that position in the index 30 times and not drawing anything until the loop that runs each frame goes through all the bubbles. The bubbles in the rest of the array are randomly populated and you're drawing and moving them all at the same time. You could use a naturally looping array and check if the bubble is off the screen or is the first time it has been called and set it's location back to the emitter x and y. That's how I would approach it.
Also it's not a good idea to reference mouse locations in a for loop. Store the mouse location to another variable and use that in the for loop. If you want bubbles to spawn from another object, create an emitter class and reference it's x and y value in your bubble and orb objects. Or just make a global variable for the mouse position and use that in your objects.
Also, are you coming from Daniel Shiffman tuts?
ps. a super simple shortcut would be getting rid of those for loops and just use an increment variable in your draw function and spawn a bubble each frame. That way you can just pass the mouseX and mouseY location or wherever you want directly into the function when it's emitted. That's the easiest way to do it.
Thanks guys, and sorry but I really have no idea how to put into practice what you are suggesting. At this point my brain is not working. Perhaps someone could point to a working example of my scenario in action, if they know of one. Yes I have been following along with those tuts, BGADII. I'm looking through his tuts how and can't quite find what I need.
here is a version without ArrayList
it will crash after some minutes / hours
numBubbles
is the current number of bubbles(
numberOfBubbles
is size of array)Chrisir
Thanks Chrisir. As you said, that crashes after around a minute when it gets to end of array. Only one red orb of the 5 emits bubbles? Maybe that was your intension and you just wanted to show the array method.
I decided to come up with my own solution, maybe it can help you out. Keep in mind I just started java/processing last week. My big problem is figuring out how to do things efficiently. There are a many things that could be improved here. Because I just reset the location back to the origin as soon as it leaves the screen eventually patterns will emerge. You can also make it a looping array instead of just running through a large array of particle objects. Also the speed of emission, movement, collisions etc. could be added. But I hope it just serves as one simple example for how arrays and loops work.
View the result: https://gfycat.com/BigheartedMeaslyBeaver
here is a version with
ArrayList
since the number of bubbles varies so much.This version doesn't crash anymore. (The last one crashed because we were adding 30 bubbles every time to an array with only 1130 slots. And we didn't have a function to free slots with bubbles that already left the screen. We have that now.)
note that the
timer
variable belongs into the orb class, I think. I fixed that.Wow BGADII... that will take some studying indeed,And you're a beginner??? Thank you very much. I'm sure I can learn new ways of doing things... and it's there's certainly some inspiration in the code for some whacky labelling techniques ;)
Chrisir, thank you. I have never used an array list before to be honest. This does the exactly the job I wanted.
Gentelmen, I am indebted to you both. I really spent too much time on this and, by looking at both examples it's clear I was going in the wrong direction.
I have a hard time coming up with sensible variable names so I just have fun with it. Better to keep entertained and when you look back on your code you can tell how frustrated you were by how bad the language gets.
I tweaked it a bit and came up with a very questionable collision checking method (at least it works... lol). But it's all the same code with some small changes and additions as I posted. Pretty flexible. (not sure why gfycat always cuts off the beginning of my video)
https://gfycat.com/DecimalFearfulDingo
edit: and here it is with 3 colors/emitters:
https://gfycat.com/LegalFeistyBluegill
Added a gravity toggle:
https://gfycat.com/OrderlyAdvancedArmadillo
And finally I was able to give the particles a cloud like effect.
I'm a total beginner in Java/processing, the thing I love about processing is you only need to know a few functions and then it's just your logic and time. Unfortunately the solutions I come up with at this point really verbose, I'm sure there are more efficient ways to do things. I'm about 1500 lines of code into coding my first game from scratch, started last week, which is actually the first script I started in processing.
Have you heard the word of our lord an savior Daniel Shiffman? I literally just watched the first few videos in his series where he goes through the basic functions like arrays and objects and started coding my app. He's amazing.
Very very cool! The gravity up down is a great idea.... collision check even better. I'll look into a collision checking system.
Yes, I also worship at the church of Schiffman :)
Thanks again!
https://gfycat.com/KindThoseBobolink
Improved: https://gfycat.com/ForsakenAnimatedEasternnewt
Based on your challenge, I think I came up with my own particle based smoke algorithm and I also came up with a way to render to what I'm calling "boxel" like objects which I might be able to use to improve performance in my app as well. This smoke is based off 500 particles, runs at 7fps. I can increase the speed by adjusting the sample size/ rate. The next step blending the boxels to improve the effect at lower sample sizes.
I'll move this over here. It's been a learning experience for me as well. Thanks!
Crikey. This sounds even cooler again! Haha... you're thanking me???
Hi again.
I have worked a bit further on my inital patch by adding an array of swimming fish and some OSC connectivity for audio visual reactions. After some effort, OSC panning and other effects I added seem to work. Each orb now vibrates to it’s own separate audio loop playing from a MAX Msp patch.
I have been attempting two things with the array of fish; #1 to vibrate and #2 change their color when either or both orbs plunge beneath the water. Again I seem to have hit a wall here. The fish only react to one orb, but not both at once. I can chose which orb by referring to its index number. I figured this should be pretty simple, according to everything else I have working similarly. I have tried the method that BGADII suggested with my first query, and several others, but to no avail.
The array for the two orbs is proving problematic. It might seem a bit overkill, but the reason for that array is that I will eventually add many more orbs(sound sources). The script overall is becoming quite confusing. I have pulled it apart several times to solve issues.
I'd be really grateful if anyone would share some tips on my fish issue... and also general ways to clean up this sketch?
I have been looking at the native object/class examples in the processing examples folder, but I'm not sure what methods work best for classes with values that interconnect. I am worried that if the patch gets any bigger it'll become wildly confusing (to me;).
Many thanks!
Phew. I think I have solved it, finally. I'll post my code shortly.