Collision with objects in an array?
in
Programming Questions
•
1 year ago
I made a simple array of circles and i would like the objects to bounce off one another :P
i love this site btw XD
here is the code:
- Circles[] circles = new Circles[255];
- void setup() {
- size(800,600);
- for(int i = 0; i < circles.length; i++) {
- circles[i] = new Circles(color(random(255),random(255),random(255)), random(800),
- random(600), random(25), random(25),random(9),random(9));
- }
- }
- void draw() {
- background(255);
- for(int i = 0; i < circles.length; i++) {
- circles[i].display();
- circles[i].move();
- }
- }
- class Circles {
- float xsize;
- float ysize;
- float xpos;
- float ypos;
- float xspeed;
- float yspeed;
- color c;
- Circles(color tempC, float tempxpos, float tempypos, float tempxsize, float tempysize, float tempxspeed, float tempyspeed) {
- xpos = tempxpos;
- ypos = tempypos;
- xsize = tempxsize;
- ysize = tempxsize;
- xspeed = tempxspeed;
- yspeed = tempyspeed;
- c = tempC;
- }
- void display() {
- fill(c);
- ellipse(xpos, ypos, xsize, ysize);
- }
- void move() {
- xpos = xpos + xspeed;
- ypos = ypos + yspeed;
- if (xpos >= width || xpos <=0) {
- xspeed *= -1;
- }
- if (ypos >= width || ypos <= 0) {
- yspeed *=-1;
- }
- }
- }
i dont understand the example collision provided by processing program here is the code from processing example bouncy bubbles :
- void collide() {
- for (int i = id + 1; i < numBalls; i++) {
- float dx = others[i].x - x;
- float dy = others[i].y - y;
- float distance = sqrt(dx*dx + dy*dy);
- float minDist = others[i].diameter/2 + diameter/2;
- if (distance < minDist) {
- float angle = atan2(dy, dx);
- float targetX = x + cos(angle) * minDist;
- float targetY = y + sin(angle) * minDist;
- float ax = (targetX - others[i].x) * spring;
- float ay = (targetY - others[i].y) * spring;
- vx -= ax;
- vy -= ay;
- others[i].vx += ax;
- others[i].vy += ay;
- }
- }
- }
can someone please explain to me how i can implement this into my program. It doesn't have to be the best way just the simplest way.
thanks :)
1