How to move more than one Object from a random startposition to a random endposition
in
Programming Questions
•
9 months ago
Hello guys,
I'm new here and I make this this little Script for the university:
- int anzahl = 50;
- Innen q;
- Kreis[] k;
- Kreis[] k2;
- Kreis[] k3;
- void setup() {
- frameRate(20);
- size(1000, 500);
- q = new Innen();
- k = new Kreis[anzahl];
- k2 = new Kreis[anzahl];
- k3 = new Kreis[anzahl];
- for (int i=0;i<anzahl;i++) {
- k[i] = new Kreis();
- k2[i] = new Kreis();
- k3[i] = new Kreis();
- }
- }
- void draw() {
- background(0);
- q.display();
- if ((mouseX>q.rx)&&(mouseX<q.rx+q.rb)) {
- if ((mouseY>q.ry)&&(mouseY<q.ry+q.rh)) {
- for (int i=0;i<anzahl;i++) {
- k[i].verfolgen(0.01, 7);
- k2[i].verfolgen(0.005, 57);
- k3[i].verfolgen(0.00125, 157);
- }
- }
- else {
- for (int i=0;i<anzahl;i++) {
- k[i].display();
- k2[i].display();
- k3[i].display();
- }
- }
- }
- else {
- for (int i=0;i<anzahl;i++) {
- k[i].display();
- k2[i].display();
- k3[i].display();
- }
- }
- }
- class Innen {
- int rx = (width/4);
- int ry = (height/4);
- int rb = (width/2);
- int rh = (height/2);
- void display() {
- rect(rx, ry, rb, rh);
- }
- }
- class Kreis {
- float ex = random(q.rx, q.rx+q.rb);
- float ey = random(q.ry, q.ry+q.rh);
- int d = 20; //Durchmesser
- float a = 25; //Abstand
- //Maus-Verfolgung
- void verfolgen(float easing, int z) {
- noCursor();
- float targetX = mouseX;
- float targetY = mouseY;
- if (dist(ex, ey, mouseX, mouseY)>45) {
- ex += (targetX - ex) * easing;
- ey += (targetY - ey) * easing;
- }
- else {
- ex -= (targetX - ex) * easing * z;
- ey -= (targetY - ey) * easing * z;
- }
- smooth();
- stroke(255);
- noFill();
- ellipse(ex, ey, d, d);
- stroke(100, 200, 200);
- ellipse(mouseX, mouseY, d, d);
- }
- void display() {
- noCursor();
- smooth();
- stroke(255);
- noFill();
- ellipse(ex, ey, d, d);
- stroke(100, 200, 200);
- ellipse(mouseX, mouseY, d, d);
- float ex = random(q.rx, q.rx+q.rb);
- float ey = random(q.ry, q.ry+q.rh);
- }
- }
I'm more or less fine with this code, but what I also want to do, is that the circles k move, if the mouse circle is outside from the rectangle, from their last position to another random position..
I was trying this thing with the random position with another Script and I really found a kind of solution, but only for ONE circle and not for more..
This is wath I try:
- Kreis[] k;
- int anzahl = 2;
- void setup() {
- size(1000, 500);
- k = new Kreis[anzahl];
- for (int i=0; i<anzahl; i++) {
- k[i] = new Kreis();
- }
- }
- void draw() {
- background(0);
- for (int i=0; i<anzahl; i++) {
- k[i].display();
- k[i].bewegen();
- }
- }
- class Kreis {
- float x;
- float y;
- float angle = 0.0; // Direction of motion
- float speed = 1.5; // Speed of motion
- int d = 10;
- Kreis() {
- x = 100; // X-coordinate
- y = 50; // Y-coordinate
- stroke(255);
- //random(121);
- }
- void bewegen() {
- angle += random(-0.2, 0.2);
- translate(x, y);
- rotate(angle);
- if ((x>0) && (x<width-d)) {
- if ((y>0) && (x<height-d)) {
- pushMatrix();
- x += cos(angle);// * speed; // Update x-coordinate
- y += sin(angle);// * speed; // Update y-coordinate
- popMatrix();
- }
- else {
- angle = PI;
- speed = 2.0;
- }
- }
- else {
- angle = PI;
- speed = 2.0;
- }
- }
- void display() {
- ellipse(0, -10, d, d);
- }
- }
know.. if you put the variable "anzahl = 10".. then you will get completly another thing.. and I dont know how to match this problem..
Thx a lot for reading my Quetion.. I hope someone can help me..
Patrick
1