Some adjustments needed (MODIFICATION)

Hello!

I don’t know if you already saw this tutorial but check it for your reference (link:Objects / Processing.org)

In the tutorial, you created the car object in two instances, each at a different speed of driving. Now, to modify it, imagine them being real cars and the drivers knowing one another. The cars could catch the eye of each other and wave a polite hello every time they drive by.

Can you guys assist me in modifying the Two Objects software of the tutorial, so that if the cars are 8 pixels in parallel, the cars would slow down to 50% speed and a red vertical line between the two cars is drawn to show that the drivers are making a contact with each other?

It should look something like this: https://youtu.be/dIGr9RprfoE

I tried and watched a lot of video tutorials but I still can’t get it. (*insert frustrated moan here)

Thanks!

Here is an attempt to do what you describe.

There might be better ways than this.

General concept in the below is keeping a list of cars and for each car checking to see if cars can see a friend.
You would want to make sure you are not checking for friends more than you have to. Especially if you have many cars on the road. So, keep track of cars that already see friends and avoid checking both ways.

Also, the code below does look across the edges of the screen. So, you would need to ensure that the connecting line and speed reduction takes into account if that cars might be exiting the screen.

Friend Spotting

Car myCar1;
Car myCar2; // Two objects!
Car[] cars;

void setup() {
  size(200,200);
  // Parameters go inside the parentheses when the object is constructed.
  cars = new Car[2];
  cars[0] = new Car(color(255,0,0),0,100,2); 
  cars[1] = new Car(color(0,0,255),0,10,1);
}

void draw() {
  background(255);
  for(int i = 0; i < cars.length; i++) {
    Car c = cars[i];
    PVector thisCar = new PVector(cars[i].xpos, cars[i].ypos);
    for(int j = 0; j < cars.length; j++) {
      if(cars[j] != cars[i]) {
        PVector thatCar = new PVector(cars[j].xpos, cars[j].ypos);
        if(thisCar.dist(thatCar) < 91)
        {
          c.friendInSight = true;
          c.friend = cars[j];
          cars[j].friendInSight = true;
        }
      }
    }
    c.drive();
    c.display();
  }
}

// Even though there are multiple objects, we still only need one class. 
// No matter how many cookies we make, only one cookie cutter is needed.
class Car { 
  color c;
  float xpos;
  float ypos;
  float xspeed;
  boolean friendInSight;
  Car friend;

  // The Constructor is defined with arguments.
  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
    friendInSight = false;
    friend = null;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,20,10);
    stroke(255,0,0);
    if(friend != null) {
      line(xpos, ypos, friend.xpos, friend.ypos);
    }
    friendInSight = false;
    friend = null;
  }

  void drive() {
    if(this.friendInSight == true) {
      xpos = xpos + xspeed * 0.5;
    }
    else {
      xpos = xpos + xspeed;
    }
    if (xpos > width) {
      xpos = 0;
    }
  }
  
}
2 Likes

Wow, thank you very much!

I didn’t think of PVector, mostly I used delay, which of course, didn’t work.

Thank youuuuuu!!!