How to draw an orange line between cars when they are 4 pixels horizontally of each other

Hello Processing masters! Newbie here! I would like to ask for your help on this on this particular concern.

There’s a particular code from the Processing.org website tutorial on OOP that I am working on regarding a Two Car Objects program. The specific code is written below.

The problem here is that every time that the car passes by each other, the cars have to slow down to 33% of their speed and an orange line must be drawn between the centers of the two cars (to indicate that the drivers are making eye contact). The example of how to do this is shown in this 12-second video -

From the original code at the Processing website, I needed to alter the initial speed of the first car from 2 to 3. Now my problems are:

(1) How do I make the cars slow down once they are 4 pixels of each other (slow down to 33% from their original speed);

(2) How do I draw an orange line between the centers of the two cars once they pass by each other (line color to be used has a value of 255 for red and 128 for green).

Your help will be truly appreciated. Thank you again for accommodating me.

Here's the initial code:

myCar1 = new Car(color(255,0,0),0,100,3); 
  myCar2 = new Car(color(0,0,255),0,10,1);
}

void draw() {
  background(255);
  myCar1.drive();
  myCar1.display();
  myCar2.drive();
  myCar2.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;

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

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,20,10);
  }

  void drive() {
    xpos = xpos + xspeed;
    if (xpos > width) {
      xpos = 0;
    }
  }
}

Answers

  • edited April 2020 Answer ✓

    Initial part of your posted code is missing! [-( Anyways, I've managed to guess the rest! [..]

    In order for class Car to have more features, you gotta create more methods &/or fields to do that! :-B

    So, besides drive() & display() methods already present there, I've implemented checkHozNear() & drawContactLine().

    Plus more constants and variables. Especially isNear to determine which spd to use!

    Check it out how it turned out: 3:-O

    /** 
     * Cars Saying Hello! (v2.2.1)
     * by  LightPost101 (2013/Nov)
     * mod GoToLoop
     * 
     * Forum.Processing.org/two/discussion/1250/
     * how-to-draw-an-orange-line-between-cars-
     * when-they-are-4-pixels-horizontally-of-each-other#Item_1
     *
     * OpenProcessing.org/sketch/870522
     */
    
    final Car car1 = new Car(#FF0000, 150, 30, 3);
    final Car car2 = new Car(#00A0FF, 0, 120, 1.5);
    
    void setup() {
      size(400, 150);
      rectMode(CENTER);
      strokeWeight(1.5);
    }
    
    void draw() {
      background(0300);
      stroke(0);
    
      car1.drive();
      car1.display();
    
      car2.drive();
      car2.display();
    
      if (car1.checkHozNear(car2))  car1.drawContactLine(car2);
    }
    
    final class Car {
      static final short W = 20, H = 10, D = 8;
      static final color LINE_C = #FFA500;
    
      color c;
      float x, y;
      final float spd, slow;
      boolean isNear;
    
      Car(final color cc, final float xx, final float yy, final float ss) { 
        c = cc;
    
        x = xx;
        y = yy;
    
        spd  = ss;
        slow = ss/3;
      }
    
      void drive() {
        if ((x += isNear? slow : spd) > width + W)  x = -W;
      }
    
      void display() { 
        fill(c);
        rect(x, y, W, H);
      }
    
      boolean checkHozNear(final Car other) {
        return other.isNear = isNear = 
          x > other.x - W - D & x < other.x + W + D;
      }
    
      void drawContactLine(final Car other) {
        stroke(LINE_C);
        line(x, y, other.x, other.y);
      }
    }
    
  • Wow! This is amazing GoToLoop...thank you so much...I am studying right now every detail of your code so that I can become better with Processing. Again, a million thanks. :D

  • void drive() {
        // move car (different speeds depending on distance)
        if (isNear) {
            x += slow;
        } else {
            x += spd;
        }
        // if offscreen then restart
        if (x > width + W) {
            x = -W;
        }
    }
    

    same as drive() above, only clearer.

  • thank you as well koogs...I'll try this one as well :-*

  • edited August 2014

    @GoToLoop, @koogs: You should know that this question by @lightpost101 is a graded university assignment; I am the instructor for the class. Your answer has then been copied by a number of students and submitted to me without attribution to you, nor is it ever mentioned by these students that they did not arrive at their answers independently. I am dealing with these students on a case-by-case basis; in the meantime, I would like to request that you take down your answers. Cheers.

  • edited August 2014

    @diegomaranan I think you need to explain why after 9 months you are asking for this information to be removed. Surely the assignment has been and gone so the information is no longer of any value to your current students also it is evidence for any action you are taking against the students involved.

  • edited August 2014

    @quark: Fair point. I have already kept an archive of this discussion, but keeping this up does serve as further evidence. Thanks. I retract my position on taking down this post.

  • Hey, I'm a beginning programmer also, yes this is the real TechWiz, and i like to use a graph when I program. It sort of helps me envision where everything is going to go, so when I make my program it is not a sort of guessing game. :-h

Sign In or Register to comment.