Two Ellipses as One - Objects as Arguments
in
Programming Questions
•
1 year ago
My friend & I were working through this code but we're stumped.
We're trying to get 2 ellipses to become one. We know that it's easy to 'fake' it (say, two ellipses collide then create a new bigger ellipse) but we'd like to figure out how to actually make them join. Eventually we'd like to have an array of various sized ellipses that when they collide will stick together for a certain amount of time then break apart. It's sticking two objects together into one that has us stumped (lines 52-54). We're working it through with basic code now, & we have pseudocode at the part that's stumping us. Any help would be appreciated!
- Ellipse myBob;
- Ellipse myStan;
- void setup() {
- size(400, 400);
- smooth();
- myBob = new Ellipse(50, 50, 25);
- myStan = new Ellipse(random(width), random(height), 50);
- }
- void draw() {
- background(255);
- myBob.display();
- myBob.move();
- myStan.display();
- if(myBob.intersect(myStan)) {
- myStan.highlight();
- myBob.highlight();
- }else {
- myStan.display();
- }
- }
- class Ellipse {
- float r;
- float x;
- float y;
- color c;
- Ellipse(float tempX, float tempY, float tempR) {
- r = tempR;
- x = tempX;
- y = tempY;
- c = color(124, 0, 55);
- }
- void move() {
- x = mouseX;
- y = mouseY;
- x = constrain(x,0,width);
- y = constrain(y,0,height);
- }
- void highlight() {
- c = color(0, 100, 100);
- ellipse(x, y, r*4, r*2);
- }
- void appear() {
- // make timer start;
- //ellipse(leftmost side of bob, y, rightmost side of stan, y*r2);
- }
- void display() {
- noStroke();
- fill(c);
- ellipseMode(CENTER);
- ellipse(x, y, r*2, r*2);
- c = color(124, 0, 55);
- }
- boolean intersect(Ellipse myStan) {
- float distance = dist(myBob.x,myBob.y,myStan.x,myStan.y);
- if(distance < r + myStan.r) {
- //connect them
- return true;
- }
- else {
- //change it back
- return false;
- }
- }
- }
1