# How to line between to objects when interacting same distance?

**URL:** <https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 8, 2020, 1:56am UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518 "2020-04-08T01:56:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbietot](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@newbietot](https://discourse.processing.org/u/newbietot)\
**Post date:** [April 8, 2020, 1:56am UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/1 "2020-04-08T01:56:14Z")

</div>

![Screenshot_2020_0408_084126](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/44f09d1a7353de611eee156b3d684d2cde104268.png)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 8, 2020, 11:49am UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/2 "2020-04-08T11:49:03Z")

</div>

in sayHello

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 8, 2020, 12:07pm UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/3 "2020-04-08T12:07:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newbietot](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@newbietot](https://discourse.processing.org/u/newbietot)\
**Post date:** [April 8, 2020, 12:37pm UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/4 "2020-04-08T12:37:42Z")

</div>

Yes its a class i will send the full code

---

<div class="post-metadata">

**Author:** ![newbietot](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@newbietot](https://discourse.processing.org/u/newbietot)\
**Post date:** [April 8, 2020, 12:39pm UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/5 "2020-04-08T12:39:24Z")

</div>

```auto
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;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 8, 2020, 12:55pm UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/6 "2020-04-08T12:55:57Z")

</div>

This code is very much like this 1 from 2013:  
https://www.openprocessing.org/sketch/870522/embed/?

---

<div class="post-metadata">

**Author:** ![newbietot](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@newbietot](https://discourse.processing.org/u/newbietot)\
**Post date:** [April 8, 2020, 12:56pm UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/7 "2020-04-08T12:56:52Z")

</div>

thanks dude its working I did use this

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

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 17, 2020, 3:34am UTC](https://discourse.processing.org/t/how-to-line-between-to-objects-when-interacting-same-distance/19518/8 "2020-05-17T03:34:31Z")

</div>

3 posts were merged into an existing topic: [How to gradient this color preview](https://discourse.processing.org/t/how-to-gradient-this-color-preview/19526/2)
