Using an ArrayList Within a Class
in
Programming Questions
•
3 years ago
Hello everyone!
I'm trying to build an osc controller that will communicate with puredata based on the collisions of a number of objects (Planets) following a circular path, but each set at a different speed. I have this working correctly, happy with the results etc. But now I want to make it interactive - ie with the mousePressed() I want to add a new planet, with a new speed at a specified radius etc, and with a maximum of say 10 planets - after that, or with a keypress, the first one in the list can be removed. It seems to me that an arraylist is ideal for this - but I don't know how I should go about changing my current code to suit this. Should I put the arraylist inside the Planet class, or should I create a kind of particle system? I've tried a few different ways but I'm stumped. (It's also very hot here at the moment and my Irish brain isn't used to it :P )
My code is attached, and it works in a basic form. It creates an array of 5 objects that revolve around the center of the canvas, and sends an osc message if the pass over one another. Also, if I'm doing anything else silly, please let me know :)
I'm trying to build an osc controller that will communicate with puredata based on the collisions of a number of objects (Planets) following a circular path, but each set at a different speed. I have this working correctly, happy with the results etc. But now I want to make it interactive - ie with the mousePressed() I want to add a new planet, with a new speed at a specified radius etc, and with a maximum of say 10 planets - after that, or with a keypress, the first one in the list can be removed. It seems to me that an arraylist is ideal for this - but I don't know how I should go about changing my current code to suit this. Should I put the arraylist inside the Planet class, or should I create a kind of particle system? I've tried a few different ways but I'm stumped. (It's also very hot here at the moment and my Irish brain isn't used to it :P )
My code is attached, and it works in a basic form. It creates an array of 5 objects that revolve around the center of the canvas, and sends an osc message if the pass over one another. Also, if I'm doing anything else silly, please let me know :)
- import oscP5.*;
- import netP5.*;
- OscP5 oscP5;
- NetAddress netAd;
- int num = 5;
- Planet[] planets = new Planet[num];
- void setup(){
- size(500, 500);
- oscP5 = new OscP5(this, 12000);
- netAd = new NetAddress("127.0.0.1", 12000);
- for(int i=0; i<num; i++){
- planets[i] = new Planet(width/2, height/2, random(0.1, 0.300), 100, int(random(50, 80)), i, planets);
- }
- }
- void draw(){
- background(0);
- for(int i=0; i<num; i++){
- planets[i].run();
- }
- }
- class Planet{
- float xpos, ypos, x, y;
- float speed;
- int radius;
- int sz = 10;
- int id;
- int freq;
- Planet[] others;
- Planet(float _xpos, float _ypos, float _speed, int r, int _freq, int _id, Planet[] _others){
- xpos = _xpos;
- ypos = _ypos;
- speed = _speed;
- radius = r;
- freq = _freq;
- id = _id;
- others = _others;
- }
- void move(){
- float m = millis()*speed;
- x = sin(radians(m))*radius+xpos;
- y = cos(radians(m))*radius+ypos;
- }
- void display(){
- stroke(255);
- noFill();
- ellipseMode(CENTER);
- ellipse(x, y, 10, 10);
- }
- void collide(){
- OscMessage message = new OscMessage("/"+id);
- for(int i = id+1; i<5; i++){
- float dx = others[i].x-x;
- float dy = others[i].y-y;
- float distance = sqrt(dx*dx + dy*dy);
- float minDist = others[i].sz/2 +sz/2;
- if(distance<minDist){
- message.add(freq);
- oscP5.send(message, netAd);
- println("collision!"+i);
- fill(255);
- ellipse(x, y, 10, 10);
- }
- }
- }
- void run(){
- move();
- display();
- collide();
- }
- }
1