How to line between to objects when interacting same distance?

in sayHello

if(intersect©)
line (c.xpos,c.ypos, xpos,ypos);

Honestly please post your entire code and not as image but as text

Is this part of a class?

When you call intersect() from outside the class you can also call sayHello and pass a car as a Parameter and use this for the line

Yes its a class i will send the full code

Car myCar1; 
Car myCar2; // Two objects! 

void setup() { 
  size(250, 250); // Parameters go inside the parentheses when the object is constructed. 
  myCar1 = new Car(color(255, 0, 0), 0, 100, 5); 
  myCar2 = new Car(color(0, 0, 255), 0, 10, 2);
} 
void draw() { 
  background(255); 
  myCar1.display(); 
  myCar2.display();

  if (myCar1.intersect(myCar2))
  {
    myCar2.sayHello(1.5);
    myCar1.sayHello1(2.5);
  }

  myCar1.drive(); 
  myCar2.drive();
}

// 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;
  int xspeed;
   
  Car(color tempC, float tempXpos, float tempYpos, float tempSpeed) { 
    c = tempC; 
    xpos = tempXpos; 
    ypos = tempYpos;
    xspeed += tempSpeed;
  } 
  void display() { 
    stroke(0); 
    fill(c); 
    rectMode(CENTER); 
    rect(xpos, ypos, 20, 10);
  } 
  void drive() 
  { 
    xpos += xspeed; 

    if (xpos > width) 
    { 
      xpos = 0;
    }
  }
  
  void sayHello(float s)
  {
    xpos-=s;
  }
  
  void sayHello1(float t)
  {
    line(xpos,ypos,xpos/1,ypos-90);
    xpos-=t;
  }

  boolean intersect(Car c)
  {
    float distance = dist(xpos, ypos, c.xpos, c.ypos);

    if (distance < 82 + c.ypos-1)
    {
      return true;
    } else
    {
      return false;
    }
  }
}

This code is very much like this 1 from 2013:

1 Like

thanks dude its working I did use this

line(myCar1.xpos,myCar1.ypos,myCar2.xpos,myCar2.ypos);

3 posts were merged into an existing topic: How to gradient this color preview